コンテンツへスキップ

Attributify JSX トランスフォーマー

JSX/TSX での 値なしの Attributify をサポート: @unocss/transformer-attributify-jsx

プレゼンテーション

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

以下のように変換されます

jsx
export function Component() {
  return (
    <div text-red="" text-center="" text-5xl="" animate-bounce="">
      unocss
    </div>
  )
}
このトランスフォーマーがない場合、JSX は値のない属性をブール属性として扱います。
jsx
export function Component() {
  return (
    <div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
      unocss
    </div>
  )
}

インストール

bash
pnpm add -D @unocss/transformer-attributify-jsx
bash
yarn add -D @unocss/transformer-attributify-jsx
bash
npm install -D @unocss/transformer-attributify-jsx
uno.config.ts
ts
import { defineConfig, presetAttributify } from 'unocss'
import transformerAttributifyJsx from '@unocss/transformer-attributify-jsx'

export default defineConfig({
  // ...
  presets: [
    // ...
    presetAttributify(),
  ],
  transformers: [
    transformerAttributifyJsx(), // <--
  ],
})

ヒント

このプリセットは unocss パッケージに含まれており、そこからインポートすることもできます。

ts
import { transformerAttributifyJsx } from 'unocss'

注意点

警告

ルールは Attributify プリセット のものとほぼ同じですが、いくつかの注意点があります。

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

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

代わりに、値付き属性を使用したい場合があります。

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

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

ブロックリスト

このトランスフォーマーは、有効な UnoCSS ユーティリティである属性のみを変換します。変換されないようにいくつかの属性を blocklist でバイパスすることもできます。

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

以下のようにコンパイルされます

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

MITライセンスの下でリリースされています。