docs/transformers/attributify-jsx.md
Support valueless attributify in JSX/TSX: @unocss/transformer-attributify-jsx.
export function Component() {
return (
<div text-red text-center text-5xl animate-bounce>
unocss
</div>
)
}
Will be transformed to:
export function Component() {
return (
<div text-red="" text-center="" text-5xl="" animate-bounce="">
unocss
</div>
)
}
::: details Without this transformer, JSX treats valueless attributes as boolean attributes.
export function Component() {
return (
<div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
unocss
</div>
)
}
:::
::: code-group
pnpm add -D @unocss/transformer-attributify-jsx
yarn add -D @unocss/transformer-attributify-jsx
npm install -D @unocss/transformer-attributify-jsx
bun add -D @unocss/transformer-attributify-jsx
:::
import { defineConfig, presetAttributify } from 'unocss'
import transformerAttributifyJsx from '@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:
import { transformerAttributifyJsx } from 'unocss'
:::
::: warning The rules are almost the same as those of Attributify preset, but there are several precautions. :::
<div translate-x-100% />
<!-- cannot end with `%` -->
<div translate-x-[100px] />
<!-- cannot contain `[` or `]` -->
Instead, you may want to use valued attributes instead:
<div translate="x-100%" />
<div translate="x-[100px]" />
This transformer will only transform attributes that are valid UnoCSS utilities.
You can also blocklist bypass some attributes from been transformed.
transformerAttributifyJsx({
blocklist: [/text-[a-zA-Z]*/, 'text-5xl']
})
<div text-red text-center text-5xl animate-bounce>
unocss
</div>
Will be compiled to:
<div text-red text-center text-5xl animate-bounce="">unocss</div>