Skip to content

Attributify JSX transformer

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

Predstavitev

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

Bo transformirano v:

jsx
export function Component() {
  return (
    <div text-red="" text-center="" text-5xl="" animate-bounce="">
      unocss
    </div>
  )
}
Brez tega transformerja, JSX obravnava atribute brez vrednosti kot boolean atribute.
jsx
export function Component() {
  return (
    <div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
      unocss
    </div>
  )
}

Namestitev

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

Ta prednastavitev je vključena v paket unocss, lahko jo tudi uvozite od tam:

ts
import { transformerAttributifyJsx } from 'unocss'

Opozorila

WARNING

Pravila so skoraj enaka kot pri Attributify prednastavitvi, vendar je nekaj previdnostnih ukrepov.

html
<div translate-x-100% />
<!-- ne more se končati z `%` -->

<div translate-x-[100px] />
<!-- ne more vsebovati `[` ali `]` -->

Namesto tega boste morda želeli uporabiti atribute z vrednostmi:

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

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

Blocklist

Ta transformer bo transformiral le atribute, ki so veljavni UnoCSS pripomočki. Lahko tudi uporabite blocklist za obvoz nekaterih atributov pred transformacijo.

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

Bo prevedeno v:

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

Released under the MIT License.