Transformer Attributify JSX
Soporte para attributify sin valor en JSX/TSX: @unocss/transformer-attributify-jsx
.
Presentación
jsx
export function Component() {
return (
<div text-red text-center text-5xl animate-bounce>
unocss
</div>
)
}
Se transformará a:
jsx
export function Component() {
return (
<div text-red="" text-center="" text-5xl="" animate-bounce="">
unocss
</div>
)
}
Sin este transformer, JSX trata los atributos sin valor como atributos booleanos.
jsx
export function Component() {
return (
<div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
unocss
</div>
)
}
Instalación
bash
pnpm add -D @unocss/transformer-attributify-jsx
bash
yarn add -D @unocss/transformer-attributify-jsx
bash
npm install -D @unocss/transformer-attributify-jsx
bash
bun add -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(), // <--
],
})
TIP
Este preset está incluido en el paquete unocss
, también puedes importarlo desde ahí:
ts
import { transformerAttributifyJsx } from 'unocss'
Advertencias
WARNING
Las reglas son casi las mismas que las del preset Attributify, pero hay varias precauciones.
html
<div translate-x-100% />
<!-- no puede terminar con `%` -->
<div translate-x-[100px] />
<!-- no puede contener `[` o `]` -->
En su lugar, puedes querer usar atributos con valor:
html
<div translate="x-100%" />
<div translate="x-[100px]" />
Lista de bloqueo
Este transformer solo transformará atributos que sean utilidades válidas de UnoCSS. También puedes usar blocklist
para omitir algunos atributos de ser transformados.
js
transformerAttributifyJsx({
blocklist: [/text-[a-zA-Z]*/, 'text-5xl']
})
jsx
<div text-red text-center text-5xl animate-bounce>
unocss
</div>
Se compilará a:
html
<div text-red text-center text-5xl animate-bounce="">unocss</div>