Skip to content

Attributify JSX transformer

Hỗ trợ attributify không giá trị trong JSX/TSX: @unocss/transformer-attributify-jsx.

Trình bày

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

Sẽ được biến đổi thành:

jsx
export function Component() {
  return (
    <div text-red="" text-center="" text-5xl="" animate-bounce="">
      unocss
    </div>
  )
}
Không có transformer này, JSX coi các thuộc tính không giá trị là các thuộc tính boolean.
jsx
export function Component() {
  return (
    <div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
      unocss
    </div>
  )
}

Cài đặt

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

Preset này được bao gồm trong gói unocss, bạn cũng có thể import nó từ đó:

ts
import { transformerAttributifyJsx } from 'unocss'

Lưu ý

WARNING

Quy tắc gần như giống với quy tắc của Attributify preset, nhưng có một số lưu ý.

html
<div translate-x-100% />
<!-- cannot end with `%` -->

<div translate-x-[100px] />
<!-- cannot contain `[` or `]` -->

Thay vào đó, bạn có thể muốn dùng các thuộc tính có giá trị:

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

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

Danh sách chặn

Transformer này sẽ chỉ biến đổi các thuộc tính là các tiện ích UnoCSS hợp lệ. Bạn cũng có thể blocklist bỏ qua một số thuộc tính không được biến đổi.

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

Sẽ được biên dịch thành:

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

Released under the MIT License.