Skip to content

Attributify preset

This enables the attributify mode for other presets.

Source Code

Installation

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

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

TIP

此预设已包含在 unocss 包中,你也可以从那里导入:

ts
import { presetAttributify } from 'unocss'

Attributify Mode

想象一下,你有一个使用 Tailwind CSS 工具类的按钮。当工具类列表越来越长时,阅读和维护就变得越来越困难。

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>

使用 attributify 模式,你可以将工具类分离到属性中:

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

Prefix self-referencing

对于像 flexgridborder 这样的工具类,其工具类名与前缀相同,可使用特殊的 ~ 值。

例如:

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

可以写成:

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

Valueless attributify

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

例如,

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

如果属性模式的名称与元素或组件的属性冲突,您可以添加 un- 前缀来指定 UnoCSS 的 attributify 模式。

例如:

html
<a text="red">这与链接的 `text` 属性冲突</a>
<!-- 转换为 -->
<a un-text="red">文本颜色为红色</a>

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

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

您还可以通过以下方式禁用某些属性的扫描:

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

TypeScript support (JSX/TSX)

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

默认情况下,类型包含 @unocss/preset-uno 的常见属性。如果您需要自定义属性,请参考 类型源 实现您自己的类型。

Vue

由于 Volar 0.36,它现在严格要求未知属性。要退出,请在项目中添加以下文件:

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

Options

strict

  • type: boolean
  • default: false

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

prefix

  • type: string
  • default: 'un-'

属性模式的前缀。

prefixedOnly

  • type: boolean
  • default: false

仅匹配前缀属性。

nonValuedAttribute

  • type: boolean
  • default: true

支持匹配无值属性。

ignoreAttributes

  • type: string[]

要从提取中忽略的属性列表。

trueToNonValued

  • type: boolean
  • default: false

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

Credits

Initial idea by @Tahul and @antfu. Prior implementation in Windi CSS by @voorjaar.

Released under the MIT License.