Attributify preset for UnoCSS that enables using utility classes as HTML attributes rather than class names.
npx @tessl/cli install tessl/npm-unocss--preset-attributify@66.5.0UnoCSS Preset Attributify enables attributify mode for UnoCSS, allowing developers to use utility classes as HTML attributes rather than class names. This provides an alternative, more semantic syntax for applying UnoCSS utilities with improved readability and maintainability in component-based frameworks.
npm install @unocss/preset-attributifyimport { presetAttributify } from "@unocss/preset-attributify";Default import:
import presetAttributify from "@unocss/preset-attributify";For CommonJS:
const presetAttributify = require("@unocss/preset-attributify");For TypeScript types:
import type { AttributifyOptions, AttributifyAttributes } from "@unocss/preset-attributify";import { defineConfig } from 'unocss'
import { presetAttributify } from '@unocss/preset-attributify'
export default defineConfig({
presets: [
presetAttributify({
// Options
prefix: 'un-',
prefixedOnly: false,
strict: false,
nonValuedAttribute: true
})
]
})HTML usage example:
<button
bg="blue-400 hover:blue-500 dark:blue-500 dark:hover:blue-600"
text="sm white"
font="mono light"
p="y-2 x-4"
border="2 rounded blue-200"
>
Button
</button>UnoCSS Preset Attributify is built around several key components:
presetAttributify function that configures the entire attributify systemCore preset function that sets up attributify mode with comprehensive configuration options.
function presetAttributify(options: AttributifyOptions = {}): Preset;
interface AttributifyOptions {
strict?: boolean;
prefix?: string;
prefixedOnly?: boolean;
nonValuedAttribute?: boolean;
ignoreAttributes?: string[];
trueToNonValued?: boolean;
}Parses HTML attributes to extract UnoCSS utility classes from attributify syntax.
function extractorAttributify(options?: AttributifyOptions): Extractor;
const defaultIgnoreAttributes: string[];Processes attributify selectors with variants like pseudo-classes and responsive breakpoints.
function variantAttributify(options?: AttributifyOptions): VariantObject;
const variantsRE: RegExp;Provides intelligent autocomplete for attributify syntax in IDEs and editors.
function autocompleteExtractorAttributify(options?: AttributifyOptions): AutoCompleteExtractor;Type-safe attribute names and values for TypeScript and JSX usage.
type AttributifyNames<Prefix extends string = ''> =
| `${Prefix}${BasicAttributes}`
| `${Prefix}${PseudoPrefix}:${BasicAttributes}`;
interface AttributifyAttributes extends Partial<Record<AttributifyNames, string | boolean>> {}
type BasicAttributes = SpecialSingleWord | TwoStringsComposition | SeparateEnabled;interface AttributifyOptions extends PresetOptions {
/** Only generate CSS for attributify or class */
strict?: boolean;
/** Attribute prefix */
prefix?: string;
/** Only match for prefixed attributes */
prefixedOnly?: boolean;
/** Support matching non-valued attributes */
nonValuedAttribute?: boolean;
/** A list of attributes to be ignored from extracting */
ignoreAttributes?: string[];
/** Non-valued attributes will also match if the actual value represented in DOM is true */
trueToNonValued?: boolean;
}