Attributify JSX transformer
Stöd för värdelös attributify i JSX/TSX: @unocss/transformer-attributify-jsx.
Presentation
jsx
export function Component() {
return (
<div text-red text-center text-5xl animate-bounce>
unocss
</div>
)
}Kommer att transformeras till:
jsx
export function Component() {
return (
<div text-red="" text-center="" text-5xl="" animate-bounce="">
unocss
</div>
)
}Utan denna transformer, JSX behandlar värdelösa attribut som booleska attribut.
jsx
export function Component() {
return (
<div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
unocss
</div>
)
}Installation
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
Denna preset ingår i unocss-paketet, du kan också importera den därifrån:
ts
import { transformerAttributifyJsx } from 'unocss'Förbehåll
WARNING
Reglerna är nästan desamma som för Attributify preset, men det finns flera försiktighetsåtgärder.
html
<div translate-x-100% />
<!-- kan inte sluta med `%` -->
<div translate-x-[100px] />
<!-- kan inte innehålla `[` eller `]` -->Istället kan du använda värderade attribut:
html
<div translate="x-100%" />
<div translate="x-[100px]" />Blocklist
Denna transformer kommer endast att transformera attribut som är giltiga UnoCSS-verktyg. Du kan också använda blocklist för att kringgå vissa attribut från att transformeras.
js
transformerAttributifyJsx({
blocklist: [/text-[a-zA-Z]*/, 'text-5xl']
})jsx
<div text-red text-center text-5xl animate-bounce>
unocss
</div>Kommer att kompileras till:
html
<div text-red text-center text-5xl animate-bounce="">unocss</div>