主题
主题
🌐 Theme
UnoCSS 还支持你可能在 Tailwind CSS / Windi CSS 中熟悉的主题系统。在用户层面,你可以在配置中指定 theme 属性,它将与默认主题进行深度合并。
🌐 UnoCSS also supports the theming system that you might be familiar with in Tailwind CSS / Windi CSS. At the user level, you can specify the theme property in your config, and it will be deep-merged to the default theme.
用法
🌐 Usage
ts
theme: {
// ...
colors: {
veryCool: '#0000ff', // class="text-very-cool"
brand: {
primary: 'hsl(var(--hue, 217) 78% 51%)', //class="bg-brand-primary"
DEFAULT: '#942192' //class="bg-brand"
},
},
}TIP
在解析过程中,theme 总是会存在于 context 中。
rules中的用法
🌐 Usage in rules
要在规则中使用主题:
🌐 To consume the theme in rules:
ts
rules: [
[/^text-(.*)$/, ([, c], { theme }) => {
if (theme.colors[c])
return { color: theme.colors[c] }
}],
]variants中的用法
🌐 Usage in variants
要使用变体中的主题:
🌐 To consume the theme in variants:
ts
variants: [
{
name: 'variant-name',
match(matcher, { theme }) {
// ...
},
},
]shortcuts中的用法
🌐 Usage in shortcuts
要在动态快捷方式中使用主题:
🌐 To consume the theme in dynamic shortcuts:
ts
shortcuts: [
[/^badge-(.*)$/, ([, c], { theme }) => {
if (Object.keys(theme.colors).includes(c))
return `bg-${c}4:10 text-${c}5 rounded`
}],
]断点
🌐 Breakpoints
WARNING
当提供自定义 breakpoints 对象时,默认设置将被覆盖而不是合并。
通过以下示例,你将只能使用 sm: 和 md: 断点变体:
🌐 With the following example, you will be able to only use the sm: and md: breakpoint variants:
ts
theme: {
// ...
breakpoints: {
sm: '320px',
md: '640px',
},
}TIP
在 presetWind4 中,键被更改为 breakpoint。
有关 presetWind4 主题的文档,请参阅 https://unocss.nodejs.cn/presets/wind4#theme。
🌐 For the presetWind4 theme docs see https://unocss.nodejs.cn/presets/wind4#theme. :::
如果你想继承 original 主题的断点,你可以使用 extendTheme:
🌐 If you want to inherit the original theme breakpoints, you can use the extendTheme:
ts
extendTheme: (theme) => {
return {
...theme,
breakpoints: {
...theme.breakpoints,
sm: '320px',
md: '640px',
},
}
}INFO
verticalBreakpoints 与 breakpoints 相同,只是用于垂直布局。
另外,我们将按大小对屏幕点进行排序(相同单位)。对于不同单位的屏幕点,为了避免错误,请在配置中使用统一的单位。
🌐 In addition we will sort screen points by size (same unit). For screen points in different units, in order to avoid errors, please use unified units in the configuration.
ts
theme: {
// ...
breakpoints: {
sm: '320px',
// Because uno does not support comparison sorting of different unit sizes, please convert to the same unit.
// md: '40rem',
md: `${40 * 16}px`,
lg: '960px',
},
}ExtendTheme
ExtendTheme 允许你编辑深度合并的主题以获取完整的主题对象。
自定义函数会改变主题对象。
🌐 Custom functions mutate the theme object.
ts
extendTheme: (theme) => {
theme.colors.veryCool = '#0000ff' // class="text-very-cool"
theme.colors.brand = {
primary: 'hsl(var(--hue, 217) 78% 51%)', // class="bg-brand-primary"
}
}还可以返回一个新的主题对象来完全替换原始主题对象。
🌐 It's also possible to return a new theme object to completely replace the original one.
ts
extendTheme: (theme) => {
return {
...theme,
colors: {
...theme.colors,
veryCool: '#0000ff', // class="text-very-cool"
brand: {
primary: 'hsl(var(--hue, 217) 78% 51%)', // class="bg-brand-primary"
},
},
}
}