Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Configure and use UnoCSS atomic CSS engine with presets like Wind (Tailwind-compatible), Icons, and Attributify.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/transformer-attributify-jsx.md
1---2name: transformer-attributify-jsx3description: Support valueless attributify in JSX/TSX4---56# Transformer Attributify JSX78Fixes valueless attributify mode in JSX where `<div foo>` becomes `<div foo={true}>`.910## The Problem1112In JSX, valueless attributes are transformed:1314```jsx15// You write16<div m-2 rounded text-teal-400 />1718// JSX compiles to19<div m-2={true} rounded={true} text-teal-400={true} />20```2122The `={true}` breaks UnoCSS attributify detection.2324## Installation2526```ts27import {28defineConfig,29presetAttributify,30transformerAttributifyJsx31} from 'unocss'3233export default defineConfig({34presets: [35presetAttributify(),36],37transformers: [38transformerAttributifyJsx(),39],40})41```4243## How It Works4445The transformer converts JSX boolean attributes back to strings:4647```jsx48// Input (after JSX compilation)49<div m-2={true} rounded={true} />5051// Output (transformed)52<div m-2="" rounded="" />53```5455Now UnoCSS can properly extract the attributify classes.5657## Options5859```ts60transformerAttributifyJsx({61// Only transform specific attributes62// Default: transforms all that match attributify patterns63blocklist: ['text', 'font'],64})65```6667## When to Use6869Required when using:70- React71- Preact72- Solid73- Any JSX-based framework7475With valueless attributify syntax:7677```jsx78// This needs the transformer79<div flex items-center gap-4 />8081// This works without transformer (has values)82<div flex="~" items="center" gap="4" />83```8485## Framework Setup8687### React8889```ts90// vite.config.ts91import React from '@vitejs/plugin-react'92import UnoCSS from 'unocss/vite'9394export default {95plugins: [96UnoCSS(), // Must be before React97React(),98],99}100```101102```ts103// uno.config.ts104import {105defineConfig,106presetAttributify,107presetWind3,108transformerAttributifyJsx109} from 'unocss'110111export default defineConfig({112presets: [113presetWind3(),114presetAttributify(),115],116transformers: [117transformerAttributifyJsx(),118],119})120```121122### Preact123124Same as React, use `@preact/preset-vite` or `@prefresh/vite`.125126### Solid127128```ts129import UnoCSS from 'unocss/vite'130import solidPlugin from 'vite-plugin-solid'131132export default {133plugins: [134UnoCSS(),135solidPlugin(),136],137}138```139140## TypeScript Support141142Add type declarations:143144```ts145// shims.d.ts146import type { AttributifyAttributes } from '@unocss/preset-attributify'147148declare module 'react' {149interface HTMLAttributes<T> extends AttributifyAttributes {}150}151```152153<!--154Source references:155- https://unocss.dev/transformers/attributify-jsx156-->157