Attributify JSX 轉換器
在 JSX/TSX 中支援 無值 Attributify:@unocss/transformer-attributify-jsx
。
呈現
jsx
export function Component() {
return (
<div text-red text-center text-5xl animate-bounce>
unocss
</div>
)
}
將會被轉換成
jsx
export function Component() {
return (
<div text-red="" text-center="" text-5xl="" animate-bounce="">
unocss
</div>
)
}
如果沒有這個轉換器,JSX 會將無值的屬性視為布林屬性。
jsx
export function Component() {
return (
<div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
unocss
</div>
)
}
安裝
bash
pnpm add -D @unocss/transformer-attributify-jsx
bash
yarn add -D @unocss/transformer-attributify-jsx
bash
npm install -D @unocss/transformer-attributify-jsx
ts
import { defineConfig, presetAttributify } from 'unocss'
import transformerAttributifyJsx from '@unocss/transformer-attributify-jsx'
export default defineConfig({
// ...
presets: [
// ...
presetAttributify(),
],
transformers: [
transformerAttributifyJsx(), // <--
],
})
提示
這個預設集包含在 unocss
套件中,您也可以從那裡導入它。
ts
import { transformerAttributifyJsx } from 'unocss'
注意事項
警告
這些規則與 Attributify 預設集 的規則幾乎相同,但有一些注意事項。
html
<div translate-x-100% /> <!-- cannot end with `%` -->
<div translate-x-[100px] /> <!-- cannot contain `[` or `]` -->
您可以改用帶值的屬性
html
<div translate="x-100%" />
<div translate="x-[100px]" />
黑名單
此轉換器只會轉換有效的 UnoCSS 工具程式屬性。您也可以使用 blocklist
繞過某些屬性的轉換。
js
transformerAttributifyJsx({
blocklist: [/text-[a-zA-Z]*/, 'text-5xl']
})
jsx
<div text-red text-center text-5xl animate-bounce>
unocss
</div>
將會被編譯成
html
<div text-red text-center text-5xl animate-bounce="">
unocss
</div>