Skip to content

Transformador Attributify JSX

Suporte para attributify sem valor em JSX/TSX: @unocss/transformer-attributify-jsx.

Apresentação

jsx
export function Component() {
  return (
    <div text-red text-center text-5xl animate-bounce>
      unocss
    </div>
  )
}

Será transformado em:

jsx
export function Component() {
  return (
    <div text-red="" text-center="" text-5xl="" animate-bounce="">
      unocss
    </div>
  )
}
Sem este transformador, o JSX trata atributos sem valor como atributos booleanos.
jsx
export function Component() {
  return (
    <div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
      unocss
    </div>
  )
}

Instalação

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á incluído no pacote unocss, você também pode importá-lo de lá:

ts
import { transformerAttributifyJsx } from 'unocss'

Observações

WARNING

As regras são quase as mesmas do Preset Attributify, mas existem algumas precauções.

html
<div translate-x-100% />
<!-- não pode terminar com `%` -->

<div translate-x-[100px] />
<!-- não pode conter `[` ou `]` -->

Em vez disso, você pode querer usar atributos com valor:

html
<div translate="x-100%" />

<div translate="x-[100px]" />

Lista de bloqueio

Este transformador só transformará atributos que são utilitários válidos do UnoCSS. Você também pode usar blocklist para ignorar alguns atributos da transformação.

js
transformerAttributifyJsx({
  blocklist: [/text-[a-zA-Z]*/, 'text-5xl']
})
jsx
<div text-red text-center text-5xl animate-bounce>
  unocss
</div>

Será compilado para:

html
<div text-red text-center text-5xl animate-bounce="">unocss</div>

Released under the MIT License.