Attributify JSX transformer
รองรับ valueless attributify ใน JSX/TSX: @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>
)
}หากไม่มี transformer นี้ JSX จะถือว่า valueless attributes เป็น boolean attributes
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-jsxbash
yarn add -D @unocss/transformer-attributify-jsxbash
npm install -D @unocss/transformer-attributify-jsxbash
bun add -D @unocss/transformer-attributify-jsxts
import { defineConfig, presetAttributify } from 'unocss'
import transformerAttributifyJsx from '@unocss/transformer-attributify-jsx'
export default defineConfig({
// ...
presets: [
// ...
presetAttributify(),
],
transformers: [
transformerAttributifyJsx(), // <--
],
})TIP
preset นี้รวมอยู่ในแพ็กเกจ unocss คุณสามารถ import จากที่นั่นได้เช่นกัน:
ts
import { transformerAttributifyJsx } from 'unocss'ข้อควรระวัง
WARNING
กฎเกณฑ์เกือบจะเหมือนกับ Attributify preset แต่มีข้อควรระวังหลายประการ
html
<div translate-x-100% />
<!-- ไม่สามารถลงท้ายด้วย `%` -->
<div translate-x-[100px] />
<!-- ไม่สามารถมี `[` หรือ `]` -->แทนที่จะเป็นเช่นนั้น คุณอาจต้องการใช้ valued attributes แทน:
html
<div translate="x-100%" />
<div translate="x-[100px]" />Blocklist
transformer นี้จะแปลงเฉพาะ attributes ที่เป็น UnoCSS utilities ที่ถูกต้อง คุณยังสามารถใช้ blocklist เพื่อข้าม attributes บางตัวจากการถูกแปลง
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>