Transformator Attributify JSX
Suportă attributify fără valoare în JSX/TSX: @unocss/transformer-attributify-jsx.
Prezentare
jsx
export function Component() {
return (
<div text-red text-center text-5xl animate-bounce>
unocss
</div>
)
}Va fi transformat în:
jsx
export function Component() {
return (
<div text-red="" text-center="" text-5xl="" animate-bounce="">
unocss
</div>
)
}Fără acest transformator, JSX tratează atributele fără valoare ca atribute booleene.
jsx
export function Component() {
return (
<div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
unocss
</div>
)
}Instalare
bash
pnpm add -D @unocss/transformer-attributify-jsxbash
yarn add -D @unocss/transformer-attributify-jsxbash
npm install -D @unocss/transformer-attributify-jsxbash
bun add -D @unocss/transformer-attributify-jsxts
import { defineConfig, presetAttributify } de la 'unocss'
import transformerAttributifyJsx de la '@unocss/transformer-attributify-jsx'
export default defineConfig({
// ...
presets: [
// ...
presetAttributify(),
],
transformers: [
transformerAttributifyJsx(), // <--
],
})TIP
This preset is included in the unocss package, you can also import it from there:
ts
import { transformerAttributifyJsx } from 'unocss'Caveats
WARNING
The rules are almost the same as those of Attributify preset, but there are several precautions.
html
<div translate-x-100% />
<!-- cannot end with `%` -->
<div translate-x-[100px] />
<!-- cannot contain `[` or `]` -->Instead, you may want to use valued attributes instead:
html
<div translate="x-100%" />
<div translate="x-[100px]" />Blocklist
This transformer will only transform attributes that are valid UnoCSS utilities. You can also blocklist bypass some attributes from been transformed.
js
transformerAttributifyJsx({
blocklist: [/text-[a-zA-Z]*/, 'text-5xl']
})jsx
<div text-red text-center text-5xl animate-bounce>
unocss
</div>Will be compiled to:
html
<div text-red text-center text-5xl animate-bounce="">unocss</div>