Skip to content

属性化模式

这将为其他预设启用属性化模式

源代码

安装

bash
pnpm add -D @unocss/preset-attributify
bash
yarn add -D @unocss/preset-attributify
bash
npm install -D @unocss/preset-attributify
bash
bun add -D @unocss/preset-attributify
uno.config.ts
ts
import presetAttributify from '@unocss/preset-attributify'

export default defineConfig({
  presets: [
    presetAttributify({ /* preset options */ }),
    // ...
  ],
})

提示

此预设包含在 unocss 包中,你也可以从该包中导入它:

ts
import { presetAttributify } from 'unocss'

属性化模式

想象一下,你有一个使用 Tailwind CSS 工具类的按钮。当工具类列表变长时,代码会变得非常难以阅读和维护。

html
<button
  class="px-4 py-2 font-mono text-sm font-light text-white bg-blue-400 rounded border-2 border-blue-200 hover:bg-blue-500 dark:bg-blue-500 dark:hover:bg-blue-600"
>
  Button
</button>

使用属性化模式,你可以将工具类拆分为属性:

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",而无需重复相同的前缀。

前缀自引用

对于像 flexgridborder 这类工具类名与前缀相同的情况,提供了一个特殊的 ~ 值。

例如:

html
<button class="border border-red">Button</button>

可以写为:

html
<button border="~ red">Button</button>

无值属性化

除了 Windi CSS 的属性化模式外,此预设还支持无值属性。

例如,

html
<div class="m-2 text-teal-400 rounded" />

现在可以

html
<div m-2 rounded text-teal-400 />

信息

注意:如果你正在使用 JSX,<div foo> 可能会被转换为 <div foo={true}>,这会导致 UnoCSS 生成的 CSS 无法匹配这些属性。为了解决这个问题,你可以尝试将 transformer-attributify-jsx 与这个预设一起使用。

属性冲突

如果属性化模式的属性名与元素或组件的属性发生冲突,你可以添加 un- 前缀来明确表示这是 UnoCSS 的属性化模式。

例如:

html
<a text="red">This conflicts with links' `text` prop</a>
<!-- to -->
<a un-text="red">Text color to red</a>

默认情况下,前缀是可选的。如果你想强制使用前缀,请设置

ts
presetAttributify({
  prefix: 'un-',
  prefixedOnly: true, // <--
})

你也可以通过以下方式禁用对某些属性的扫描:

ts
presetAttributify({
  ignoreAttributes: [
    'text'
    // ...
  ]
})

TypeScript 支持 (JSX/TSX)

创建一个 shims.d.ts 文件,内容如下:

默认情况下,该类型包含来自 @unocss/preset-wind3 的常用属性。如果你需要自定义属性,请参考 类型源代码 来实现你自己的类型。

Vue

自 Volar 0.36 版本起,对未知属性的检查变得严格。若要取消这种严格检查,你可以在项目中添加以下文件:

html.d.ts
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

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 {}
  }
}

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>> {}

选项

strict

  • 类型: boolean
  • 默认值: false

仅为属性化模式或类生成 CSS。

prefix

  • 类型: string
  • 默认值: 'un-'

属性化模式的前缀。

prefixedOnly

  • 类型: boolean
  • 默认值: false

仅匹配带有前缀的属性。

nonValuedAttribute

  • 类型: boolean
  • 默认值: true

支持匹配无值属性。

ignoreAttributes

  • 类型: string[]

提取时要忽略的属性列表。

trueToNonValued

  • 类型: boolean
  • 默认值: false

如果 DOM 中表示的实际值为 true,无值属性也会匹配。此选项用于支持将无值属性编码为 true 的框架。启用此选项将破坏以 true 结尾的规则。

鸣谢

最初的想法由 @Tahul@antfu 提出。之前在 Windi CSS 中的实现@voorjaar 完成。