Skip to content

Trasformatore Attributify JSX

Supporta attributify senza valore in JSX/TSX: @unocss/transformer-attributify-jsx.

Presentazione

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

Verrà trasformato in:

jsx
export function Component() {
  return (
    <div text-red="" text-center="" text-5xl="" animate-bounce="">
      unocss
    </div>
  )
}
Senza questo trasformatore, JSX tratta gli attributi senza valore come attributi booleani.
jsx
export function Component() {
  return (
    <div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
      unocss
    </div>
  )
}

Installazione

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

Questo preset è incluso nel pacchetto unocss, puoi anche importarlo da lì:

ts
import { transformerAttributifyJsx } from 'unocss'

Avvertenze

WARNING

Le regole sono quasi le stesse di quelle del preset Attributify, ma ci sono diverse precauzioni.

html
<div translate-x-100% />
<!-- non può terminare con `%` -->

<div translate-x-[100px] />
<!-- non può contenere `[` o `]` -->

Invece, potresti voler usare attributi con valore:

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

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

Blocklist

Questo trasformatore trasformerà solo gli attributi che sono utility UnoCSS valide. Puoi anche usare blocklist per bypassare alcuni attributi dalla trasformazione.

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

Verrà compilato in:

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

Released under the MIT License.