Attributify preset for UnoCSS that enables using utility classes as HTML attributes rather than class names.
—
Processes attributify selectors with variants like pseudo-classes, responsive breakpoints, and other UnoCSS variants, converting attribute-based variants into standard UnoCSS utility format.
Creates a variant handler for processing attributify selectors with UnoCSS variants.
/**
* Creates a variant handler for processing attributify selectors
* @param options - Configuration options for variant processing
* @returns UnoCSS VariantObject
*/
function variantAttributify(options: AttributifyOptions = {}): VariantObject;
interface VariantObject {
name: 'attributify';
match(input: string, context: VariantContext): string | VariantMatch[] | undefined;
}
interface VariantContext {
generator?: UnoGenerator;
}
interface VariantMatch {
matcher: string;
}Usage Examples:
import { variantAttributify } from '@unocss/preset-attributify'
// Create variant handler with default options
const variant = variantAttributify()
// Create variant handler with custom options
const customVariant = variantAttributify({
prefix: 'ui-',
prefixedOnly: true,
trueToNonValued: true
})Regular expression for parsing variant prefixes from attribute values.
/**
* Regular expression for parsing variants from attribute values
* Matches variant prefixes like "hover:", "sm:", etc.
* Pattern: /^(?!.*\[[^:]+:.+\]$)((?:.+:)?!?)(.*)$/
*/
const variantsRE: RegExp;The variant handler processes attributify selectors through these steps:
~ syntax for attribute namesThe variant handler processes these selector patterns:
Basic Attributify Selector:
// Input: [bg~="blue-500"]
// Output: "bg-blue-500"With Variants:
// Input: [bg~="hover:blue-500"]
// Output: "hover:bg-blue-500"Self-Reference:
// Input: [hover~="~"]
// Output: "hover:hover" (attribute name becomes utility)Non-Valued Attributes:
// Input: [mt-2=""]
// Output: "mt-2"
// With trueToNonValued: true
// Input: [mt-2="true"]
// Output: "mt-2"Complex Variants:
// Input: [text~="sm:hover:red-500"]
// Output: "sm:hover:text-red-500"The variant handler resolves ambiguous cases where variants and values could be interpreted multiple ways:
Numeric Values with Variants:
<!-- Ambiguous: border="red:10" -->
<!-- Could be: border-red:10 OR red:border-10 -->
<div border="red:10"></div>Resolution Strategy:
// Returns both possibilities as VariantMatch[]
[
{ matcher: "red:border-10" }, // Variant interpretation
{ matcher: "border-red:10" } // Value interpretation
]The variant handler respects prefix configuration:
With Prefix:
// options.prefix = "ui-"
// Input: [ui-bg~="blue-500"]
// Output: "bg-blue-500"
// options.prefixedOnly = true
// Input: [bg~="blue-500"] (no prefix)
// Output: undefined (ignored)Special handling for the ~ self-reference syntax:
<!-- Attribute name becomes the utility -->
<div hover="~"></div> <!-- hover:hover -->
<div focus="~"></div> <!-- focus:focus -->
<div bg="red-500 hover:~"></div> <!-- bg-red-500 hover:bg -->Handles bracket notation for arbitrary values:
// Input: [w~="[200px]"]
// Output: "w-[200px]"
// With variants
// Input: [w~="sm:[200px]"]
// Output: "sm:w-[200px]"Responsive Breakpoints:
<div text="sm:lg md:xl lg:2xl"></div>Generates:
sm:text-lgmd:text-xllg:text-2xlPseudo-Classes:
<div bg="blue-500 hover:blue-600 focus:blue-700"></div>Generates:
bg-blue-500hover:bg-blue-600focus:bg-blue-700Combined Variants:
<div text="sm:hover:red-500 lg:focus:blue-500"></div>Generates:
sm:hover:text-red-500lg:focus:text-blue-500Dark Mode:
<div bg="white dark:black text="black dark:white"></div>Generates:
bg-whitedark:bg-blacktext-blackdark:text-whiteThe variant handler handles these edge cases:
undefined)The variant handler integrates with UnoCSS's variant system:
// Part of preset configuration
{
variants: [variantAttributify(options)]
}The handler works alongside other UnoCSS variants and respects the global variant configuration.
Install with Tessl CLI
npx tessl i tessl/npm-unocss--preset-attributify