主题
属性化预设
🌐 Attributify preset
这为其他预设启用属性模式。
🌐 This enables the attributify mode for other presets.
安装
🌐 Installation
bash
pnpm add -D @unocss/preset-attributifybash
yarn add -D @unocss/preset-attributifybash
npm install -D @unocss/preset-attributifybash
bun add -D @unocss/preset-attributifyts
import presetAttributify from '@unocss/preset-attributify'
export default defineConfig({
presets: [
presetAttributify({ /* preset options */ }),
// ...
],
})TIP
此预设包含在 unocss 包中,你也可以从那里导入它:
ts
import { presetAttributify } from 'unocss'属性化模式
🌐 Attributify Mode
想象一下你使用 Tailwind CSS 的工具类制作了这个按钮。当列表变长时,会变得非常难以阅读和维护。
🌐 Imagine you have this button using Tailwind CSS's utilities. When the list gets longer, it becomes really hard to read and maintain.
html
<button
class="bg-blue-400 hover:bg-blue-500 text-sm text-white font-mono font-light py-2 px-4 rounded border-2 border-blue-200 dark:bg-blue-500 dark:hover:bg-blue-600"
>
Button
</button>使用属性模式,你可以将工具分成属性:
🌐 With attributify mode, you can separate utilities into attributes:
html
<button
bg="blue-400 hover:blue-500 dark:blue-500 dark:hover:blue-600"
text="sm white"
font="mono light"
p="y-2 x-4"
border="2 rounded blue-200"
>
Button
</button>例如,text-sm text-white 可以被分组到 text="sm white" 中,而不会重复相同的前缀。
🌐 For example, text-sm text-white could be grouped into text="sm white" without duplicating the same prefix.
前缀自引用
🌐 Prefix self-referencing
对于像 flex、grid、border 这样的工具,它们的工具与前缀相同,会提供一个特殊的 ~ 值。
🌐 For utilities like flex, grid, border, that have the utilities same as the prefix, a special ~ value is provided.
例如:
🌐 For example:
html
<button class="border border-red">Button</button>可以写成:
🌐 Can be written as:
html
<button border="~ red">Button</button>无值的属性化
🌐 Valueless attributify
除了 Windi CSS 的属性模式外,此预设还支持无值属性。
🌐 In addition to Windi CSS's attributify mode, this preset also supports valueless attributes.
例如,
🌐 For example,
html
<div class="m-2 rounded text-teal-400" />现在可以
🌐 now can be
html
<div m-2 rounded text-teal-400 />INFO
注意:如果你正在使用 JSX,<div foo> 可能会被转换为 <div foo={true}>,这会导致 UnoCSS 生成的 CSS 无法匹配这些属性。为了解决这个问题,你可以尝试将 transformer-attributify-jsx 与此预设一起使用。
属性冲突
🌐 Properties conflicts
如果 attributes 模式的名称与元素或组件的属性发生冲突,你可以添加 un- 前缀来专门用于 UnoCSS 的 attributify 模式。
🌐 If the name of the attributes mode ever conflicts with the elements' or components' properties, you can add un- prefix to be specific to UnoCSS's attributify mode.
例如:
🌐 For example:
html
<a text="red">This conflicts with links' `text` prop</a>
<!-- to -->
<a un-text="red">Text color to red</a>默认情况下,前缀是可选的,如果要强制使用前缀,请设置
🌐 Prefix is optional by default, if you want to enforce the usage of prefix, set
ts
presetAttributify({
prefix: 'un-',
prefixedOnly: true, // <--
})你还可以通过以下方式禁用某些属性的扫描:
🌐 You can also disable the scanning for certain attributes by:
ts
presetAttributify({
ignoreAttributes: [
'text'
// ...
]
})TypeScript 支持 (JSX/TSX)
🌐 TypeScript support (JSX/TSX)
使用以下内容创建 shims.d.ts:
🌐 Create shims.d.ts with the following content:
默认情况下,该类型包含来自
@unocss/preset-wind3的常用属性。如果你需要自定义属性,请参考 类型源 来实现你自己的类型。
Vue
自 Volar 0.36 起,对未知属性现在更严格。要选择退出,你可以将以下文件添加到你的项目中:
🌐 Since Volar 0.36, it's now strict to unknown attributes. To opt-out, you can add the following file to your project:
ts
declare module '@vue/runtime-dom' {
interface HTMLAttributes {
[key: string]: any
}
}
declare module '@vue/runtime-core' {
interface AllowedComponentProps {
[key: string]: any
}
}
export {}React
ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare module 'react' {
interface HTMLAttributes<T> extends AttributifyAttributes {}
}Vue 3
ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare module '@vue/runtime-dom' {
interface HTMLAttributes extends AttributifyAttributes {}
}SolidJS
ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare module 'solid-js' {
namespace JSX {
interface HTMLAttributes<T> extends AttributifyAttributes {}
}
}Svelte 和 SvelteKit
🌐 Svelte & SvelteKit
ts
declare namespace svelteHTML {
import type { AttributifyAttributes } from '@unocss/preset-attributify'
type HTMLAttributes = AttributifyAttributes
}Astro
ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare global {
namespace astroHTML.JSX {
interface HTMLAttributes extends AttributifyAttributes { }
}
}Preact
ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare module 'preact' {
namespace JSX {
interface HTMLAttributes extends AttributifyAttributes {}
}
}Marko
ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare global {
namespace Marko {
interface HTMLAttributes extends AttributifyAttributes {}
}
}使用前缀进行属性化
🌐 Attributify with Prefix
ts
import type { AttributifyNames } from '@unocss/preset-attributify'
type Prefix = 'uno:' // change it to your prefix
interface HTMLAttributes extends Partial<Record<AttributifyNames<Prefix>, string>> {}选项
🌐 Options
strict
- 类型:
boolean - 默认:
false
仅生成属性或类的 CSS。
🌐 Only generate CSS for attributify or class.
prefix
- 类型:
string - 默认:
'un-'
属性模式的前缀。
🌐 The prefix for attributify mode.
prefixedOnly
- 类型:
boolean - 默认:
false
仅匹配前缀属性。
🌐 Only match for prefixed attributes.
nonValuedAttribute
- 类型:
boolean - 默认:
true
支持匹配非值属性。
🌐 Support matching non-valued attributes.
ignoreAttributes
- 类型:
string[]
提取时要忽略的属性列表。
🌐 A list of attributes to be ignored from extracting.
trueToNonValued
- 类型:
boolean - 默认:
false
如果 DOM 中表示的实际值为 true,未赋值的属性也将匹配。此选项用于支持将未赋值属性编码为 true 的框架。启用此选项将破坏以 true 结尾的规则。
🌐 Non-valued attributes will also match if the actual value represented in DOM is true. This option exists for supporting frameworks that encodes non-valued attributes as true. Enabling this option will break rules that ends with true.
致谢
🌐 Credits
初始创意由 @Tahul 和 @antfu 提出。之前在 Windi CSS 中的实现由 @voorjaar 完成。
🌐 Initial idea by @Tahul and @antfu. Prior implementation in Windi CSS by @voorjaar.