Robot Admin UnoCSS 使用详解
⚡ 强大的原子 CSS 引擎
提供实用为先的样式、属性化模式和图标功能的高性能 CSS 解决方案
UnoCSS 是一个强大的原子 CSS 引擎,集成在 Robot Admin 项目中,提供以实用为先的样式、属性化模式和图标功能。本文档将详细指导您如何配置 UnoCSS 以及如何在组件中有效使用它。
🎯 核心特性
该项目中的 UnoCSS 配置了以下几个关键特性:
特性 | 说明 | 功能 |
---|---|---|
实用优先 CSS | 通过 presetWind3 实现类似 Tailwind | 快速样式编写 |
属性化模式 | 允许基于属性的样式设置 | 更干净的标记 |
图标集成 | 与 Material Design Icons 集成 | 丰富的图标资源 |
自定义快捷方式 | 常用实用组合的快捷方式 | 提高开发效率 |
指令转换器 | 用于直接 CSS 操作 | 灵活的样式控制 |
💡 集成优势
UnoCSS 提供了比传统 CSS 框架更好的性能和灵活性,支持按需生成样式,减少最终打包体积。
🛠️ 基础设置
全局导入
UnoCSS 在项目的入口文件中全局导入:
typescript
// 导入 UnoCSS 样式
import "virtual:uno.css";
// 其他导入...
import { createApp } from "vue";
import App from "./App.vue";
const app = createApp(App);
app.mount("#app");
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
此导入包含了所有 UnoCSS 功能,包括实用类、属性化模式和图标。
核心配置
UnoCSS 配置定义了可用的实用类、快捷方式和安全列表项:
typescript
import {
defineConfig,
presetAttributify,
presetIcons,
presetWind,
transformerDirectives,
} from "unocss";
import { shortcutsArr } from "./src/config/shortcuts-arr";
import { iconSafelist } from "./src/config/icon-safelist";
export default defineConfig({
presets: [
// Wind 预设(类似 Tailwind)
presetWind(),
// 属性化模式
presetAttributify(),
// 图标预设
presetIcons({
scale: 1.2,
warn: true,
extraProperties: {
display: "inline-block",
"vertical-align": "middle",
},
}),
],
// 指令转换器
transformers: [transformerDirectives()],
// 自定义快捷方式
shortcuts: shortcutsArr,
// 图标安全列表
safelist: iconSafelist,
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
🚀 自定义快捷方式
快捷方式是预定义的实用类组合,创建可重用的样式模式:
typescript
export const shortcutsArr = {
// 图标相关快捷方式
"icon-container": "flex flex-wrap items-center w-full",
"icon-container-item":
"w-1/5 flex flex-col justify-center items-center mb-8 cursor-pointer transition-all duration-300 hover:rounded hover:opacity-60 hover:scale-120 box-border",
"icon-container-item-info": "mt-2",
// 布局快捷方式
"flex-center": "flex items-center justify-center",
"flex-col-center": "flex flex-col items-center justify-center",
// 按钮快捷方式
btn: "px-4 py-2 rounded transition-colors duration-200",
"btn-primary": "btn bg-blue-500 text-white hover:bg-blue-600",
"btn-secondary": "btn bg-gray-200 text-gray-800 hover:bg-gray-300",
// 文本快捷方式
"text-header": "text-2xl font-bold text-gray-900",
"text-body": "text-base text-gray-700",
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vue
<template>
<div class="icon-container">
<div class="icon-container-item">
<div class="i-mdi:home text-24"></div>
<div class="icon-container-item-info">首页</div>
</div>
<div class="flex-center mt-4">
<button class="btn-primary">主要按钮</button>
<button class="btn-secondary ml-2">次要按钮</button>
</div>
<h1 class="text-header">页面标题</h1>
<p class="text-body">页面内容描述</p>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
🎨 TypeScript 集成
UnoCSS 包含 TypeScript 定义,为实用类提供自动补全和类型检查:
typescript
declare module "unocss" {
interface UserShortcuts {
/** 垂直居中的 flex 布局 */
"flex-col-center": string;
/** 标题文本样式 */
"text-header": string;
/** 按钮基础样式 */
btn: string;
/** 水平垂直居中的 flex 布局 */
"flex-center": string;
/** 图标容器样式 */
"icon-container": string;
/** 图标项目样式 */
"icon-container-item": string;
// 可以继续添加其他自定义快捷方式
}
}
export {};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
🎭 图标系统使用
Robot Admin 中的 UnoCSS 通过 presetIcons
集成为使用图标提供了强大的方式,主要使用 Material Design Icons (MDI)。
图标安全列表
为确保图标在生产构建过程中不被树摇移除,项目使用图标安全列表:
typescript
import { icons as IconsIconifyMdi } from "@iconify-json/mdi";
export const iconSafelist = [
// 动态生成的图标(前100个常用图标)
...Object.entries(IconsIconifyMdi.icons)
.slice(0, 100)
.map(([name]) => `i-mdi:${name}`),
// 静态图标类名
"i-mdi:home-assistant",
"i-mdi:monitor-dashboard",
"i-mdi:account-circle",
"i-mdi:settings",
"i-mdi:menu",
"i-mdi:close",
"i-mdi:chevron-down",
"i-mdi:chevron-up",
"i-mdi:chevron-left",
"i-mdi:chevron-right",
"i-mdi:plus",
"i-mdi:minus",
"i-mdi:delete",
"i-mdi:edit",
"i-mdi:search",
"i-mdi:filter",
"i-mdi:refresh",
"i-mdi:download",
"i-mdi:upload",
"i-mdi:eye",
"i-mdi:eye-off",
"i-mdi:heart",
"i-mdi:star",
"i-mdi:bookmark",
"i-mdi:share",
"i-mdi:copy",
"i-mdi:check",
"i-mdi:alert-circle",
"i-mdi:information",
"i-mdi:help-circle",
];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
在组件中使用图标
项目中使用图标主要有两种方式:
vue
<template>
<div>
<!-- 直接使用 i-mdi: 前缀 -->
<div class="i-mdi:home text-24 text-blue-500"></div>
<div class="i-mdi:settings text-20 text-gray-600"></div>
<div class="i-mdi:user text-16 text-green-500"></div>
<!-- 结合其他样式 -->
<button
class="flex items-center gap-2 px-4 py-2 bg-blue-500 text-white rounded"
>
<div class="i-mdi:plus text-16"></div>
添加项目
</button>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vue
<template>
<div>
<!-- 基础用法 -->
<C_Icon type="unocss" name="i-mdi-home" />
<!-- 指定大小和颜色 -->
<C_Icon type="unocss" name="i-mdi-settings" size="24" />
<C_Icon type="unocss" name="i-mdi-user" color="#42b883" size="32" />
<!-- 支持交互 -->
<C_Icon type="unocss" name="i-mdi-heart" clickable @click="handleClick" />
<!-- 结合其他属性 -->
<C_Icon
type="unocss"
name="i-mdi-star"
size="20"
color="orange"
class="mr-2"
/>
</div>
</template>
<script setup lang="ts">
const handleClick = () => {
console.log("图标被点击");
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
🎯 实用类使用
UnoCSS 提供了类似于 Tailwind CSS 的广泛实用类:
布局相关
vue
<template>
<div>
<!-- Flex 布局 -->
<div class="flex flex-col gap-4">
<div class="flex items-center justify-between">
<span>左侧内容</span>
<span>右侧内容</span>
</div>
</div>
<!-- Grid 布局 -->
<div class="grid grid-cols-3 gap-4 mt-6">
<div class="bg-gray-100 p-4 rounded">Grid 项目 1</div>
<div class="bg-gray-100 p-4 rounded">Grid 项目 2</div>
<div class="bg-gray-100 p-4 rounded">Grid 项目 3</div>
</div>
<!-- 响应式布局 -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<!-- 响应式网格项目 -->
</div>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
间距和尺寸
vue
<template>
<div>
<!-- 外边距和内边距 -->
<div class="m-4 p-6 bg-white rounded shadow">
<div class="mt-2 mb-4 px-4 py-2 bg-blue-50">
带有顶部外边距、底部外边距和水平内边距的内容
</div>
</div>
<!-- 尺寸控制 -->
<div class="w-full h-32 bg-gray-200 rounded">
<div class="w-1/2 h-full bg-blue-500 rounded-l"></div>
</div>
<!-- 最大最小宽高 -->
<div class="max-w-md min-h-20 mx-auto p-4 bg-green-100">
最大宽度中等,最小高度20的居中容器
</div>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
排版样式
vue
<template>
<div class="space-y-4">
<!-- 标题样式 -->
<h1 class="text-3xl font-bold text-gray-900">主标题</h1>
<h2 class="text-2xl font-semibold text-gray-800">副标题</h2>
<h3 class="text-xl font-medium text-gray-700">三级标题</h3>
<!-- 段落文本 -->
<p class="text-base text-gray-700 leading-relaxed">
这是一段普通的段落文本,使用了基础字体大小和舒适的行高。
</p>
<!-- 特殊文本样式 -->
<div class="space-y-2">
<p class="text-sm text-gray-500">小号文本</p>
<p class="text-lg font-semibold text-blue-600">重要信息</p>
<p class="text-red-500 font-medium">错误提示</p>
<p class="text-green-600 italic">成功消息</p>
</div>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
颜色和背景
vue
<template>
<div class="space-y-4">
<!-- 背景和文本颜色 -->
<div class="bg-blue-500 text-white p-4 rounded">蓝色背景配白色文本</div>
<div
class="bg-gradient-to-r from-purple-500 to-pink-500 text-white p-4 rounded"
>
渐变背景
</div>
<!-- 状态颜色 -->
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
<div class="bg-green-100 text-green-800 p-3 rounded text-center">
成功状态
</div>
<div class="bg-yellow-100 text-yellow-800 p-3 rounded text-center">
警告状态
</div>
<div class="bg-red-100 text-red-800 p-3 rounded text-center">
错误状态
</div>
<div class="bg-blue-100 text-blue-800 p-3 rounded text-center">
信息状态
</div>
</div>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
交互状态
vue
<template>
<div class="space-y-4">
<!-- 悬停效果 -->
<button
class="bg-blue-500 hover:bg-blue-600 text-white px-6 py-2 rounded transition-colors"
>
悬停变色按钮
</button>
<!-- 聚焦效果 -->
<input
type="text"
placeholder="聚焦时显示边框"
class="w-full px-3 py-2 border border-gray-300 rounded focus:border-blue-500 focus:ring-2 focus:ring-blue-200 outline-none transition-all"
/>
<!-- 活动状态 -->
<button
class="bg-green-500 hover:bg-green-600 active:bg-green-700 text-white px-4 py-2 rounded transition-colors"
>
点击效果按钮
</button>
<!-- 禁用状态 -->
<button
class="bg-gray-300 text-gray-500 px-4 py-2 rounded cursor-not-allowed"
disabled
>
禁用按钮
</button>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
🎨 属性化模式
项目通过 presetAttributify()
启用属性化模式,允许您使用属性而不是类进行样式设置:
vue
<template>
<div>
<!-- 使用常规类 -->
<div
class="flex items-center justify-between p-4 bg-gray-100 rounded shadow"
>
常规类方式
</div>
<!-- 使用属性化模式 -->
<div
flex
items-center
justify-between
p="4"
bg="gray-100"
rounded
shadow
mt="4"
>
属性化模式
</div>
<!-- 复杂样式的属性化模式 -->
<div
grid
grid-cols="1 md:2 lg:3"
gap="4"
p="6"
bg="white"
rounded="lg"
shadow="xl"
border="1 gray-200"
>
<div
bg="blue-50"
p="4"
rounded="md"
text="center blue-700"
hover="bg-blue-100"
transition="colors"
>
项目 1
</div>
</div>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
这可以使您的标记更干净、更易读,尤其是在样式复杂的情况下。
📋 最佳实践
开发建议
实践 | 说明 | 重要性 |
---|---|---|
使用快捷方式 | 为常用模式创建快捷方式 | 🟡 重要 |
图标安全列表 | 动态使用的图标添加到安全列表 | 🔴 关键 |
属性化模式 | 复杂样式使用属性化模式 | 🟡 重要 |
组件抽象 | 复杂模式封装为 Vue 组件 | 🟡 重要 |
使用规范
vue
<template>
<div>
<!-- ✅ 推荐:使用快捷方式 -->
<div class="flex-center">
<button class="btn-primary">
<div class="i-mdi:plus mr-2"></div>
添加
</button>
</div>
<!-- ✅ 推荐:语义化类名组合 -->
<div class="card">
<h3 class="card-title">卡片标题</h3>
<p class="card-content">卡片内容</p>
</div>
<!-- ✅ 推荐:响应式设计 -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<!-- 响应式内容 -->
</div>
<!-- ❌ 避免:过度复杂的类名 -->
<!-- <div class="flex items-center justify-between w-full h-16 px-4 py-2 bg-white border border-gray-200 rounded-lg shadow-sm hover:shadow-md transition-shadow duration-200"> -->
<!-- ✅ 更好:使用快捷方式或组件 -->
<div class="header-bar">
<!-- 内容 -->
</div>
</div>
</template>
<style scoped>
/* 当 UnoCSS 不够用时,可以结合 SCSS */
.card {
@apply bg-white rounded-lg shadow-md p-6;
}
.card-title {
@apply text-xl font-semibold text-gray-900 mb-2;
}
.card-content {
@apply text-gray-700 leading-relaxed;
}
.header-bar {
@apply flex items-center justify-between w-full h-16 px-4 py-2 bg-white border border-gray-200 rounded-lg shadow-sm hover:shadow-md transition-shadow duration-200;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
故障排除
如果 UnoCSS 类未应用,请检查:
- 导入检查: 确保
virtual:uno.css
在主入口文件中导入 - 语法验证: 验证类名是否符合 UnoCSS 语法规则
- 安全列表: 对于动态类,确保它们在安全列表中
- 控制台检查: 查看浏览器控制台是否有 UnoCSS 警告或错误
🎯 开发建议
遵循这些指南,您将能够在组件中有效使用 UnoCSS,从而在整个 Robot Admin 项目中创建一致且可维护的 UI 样式。UnoCSS 的按需生成特性和丰富的功能集使其成为现代 Web 开发的理想选择。