Skip to content

Attributify JSX transformer

Podpora valueless attributify v JSX/TSX: @unocss/transformer-attributify-jsx.

Prezentácia

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

Bude transformované na:

jsx
export function Component() {
  return (
    <div text-red="" text-center="" text-5xl="" animate-bounce="">
      unocss
    </div>
  )
}
Bez tohto transformeru, JSX zaobchádza s valueless atribútmi ako boolean atribúty.
jsx
export function Component() {
  return (
    <div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
      unocss
    </div>
  )
}

Inštalácia

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

Tento preset je zahrnutý v balíku unocss, môžete ho tiež importovať odtiaľ:

ts
import { transformerAttributifyJsx } from 'unocss'

Obmedzenia

WARNING

Pravidlá sú takmer rovnaké ako pre Attributify preset, ale existuje niekoľko upozornení.

html
<div translate-x-100% />
<!-- nemôže končiť na `%` -->

<div translate-x-[100px] />
<!-- nemôže obsahovať `[` alebo `]` -->

Namiesto toho môžete použiť hodnotené atribúty:

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

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

Blocklist

Tento transformer bude transformovať iba atribúty, ktoré sú platnými utilítami UnoCSS. Môžete tiež použiť blocklist na vynechanie niektorých atribútov z transformácie.

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

Bude skompilované na:

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

Released under the MIT License.