Skip to content

Attributify JSX Transformer

Unterstützt wertloses Attributify in JSX/TSX: @unocss/transformer-attributify-jsx.

Präsentation

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

Wird transformiert zu:

jsx
export function Component() {
  return (
    <div text-red="" text-center="" text-5xl="" animate-bounce="">
      unocss
    </div>
  )
}
Ohne diesen Transformer behandelt JSX wertlose Attribute als Boolean-Attribute.
jsx
export function Component() {
  return (
    <div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
      unocss
    </div>
  )
}

Installation

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

Dieses Preset ist im unocss Paket enthalten, Sie können es auch von dort importieren:

ts
import { transformerAttributifyJsx } from 'unocss'

Einschränkungen

WARNING

Die Regeln sind fast dieselben wie die des Attributify Presets, aber es gibt mehrere Vorsichtsmaßnahmen.

html
<div translate-x-100% />
<!-- kann nicht mit `%` enden -->

<div translate-x-[100px] />
<!-- kann nicht `[` oder `]` enthalten -->

Stattdessen möchten Sie möglicherweise bewertete Attribute verwenden:

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

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

Blocklist

Dieser Transformer transformiert nur Attribute, die gültige UnoCSS-Utilities sind. Sie können auch blocklist verwenden, um einige Attribute von der Transformation zu umgehen.

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

Wird kompiliert zu:

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

Released under the MIT License.