Variant System
Relevant source files- .devin/wiki.json
- README.md
- scripts/cva-extractor.ts
- src/components/README.md
- src/index.ts
- src/layouts/index.ts
- src/lib/core-classes.json
Purpose and Scope
The Variant System is the foundational styling layer of@ui8kit/core, providing a CVA-based (class-variance-authority) approach to component styling. This system groups Tailwind CSS utility classes into 12 composable, reusable variant categories that cover approximately 80% of design scenarios, eliminating the need for manual className management and reducing style duplication across components.
This document covers the variant architecture, the 12 variant categories, CVA class generation, extraction mechanics, and composition patterns. For information about how components consume these variants, see Core Components and UI Components. For build-time extraction of variant classes, see Build System.
Sources: README.md170-217 .devin/wiki.json19-22
Architecture Overview
The Variant System operates as a three-stage pipeline: variant definitions, CVA resolution, and class application.Variant System Pipeline
The 12 Variant Categories
The system provides 12 variant categories organized by design concern. Each category maps developer-friendly prop names to Tailwind CSS utility classes.Variant Category Overview
| Category | Variant Props | Value Range | Purpose |
|---|---|---|---|
| Spacing | p, m, px, py, pt, pb, pl, pr, mx, my, mt, mb, ml, mr | none, xs, sm, md, lg, xl, 2xl, auto | Control padding and margin |
| Colors | bg, c, borderColor | Design system tokens: primary, secondary, accent, destructive, muted, card, background, foreground, etc. | Semantic color application |
| Layout - Sizing | w, h, minH, maxW | auto, full, screen, fit, min, max | Width and height control |
| Layout - Position | position | static, relative, absolute, fixed, sticky | Element positioning |
| Typography - Size | size | xs, sm, base, lg, xl, 2xl, 3xl, 4xl, 5xl | Font size scaling |
| Typography - Weight | weight, fw | normal, medium, semibold, bold | Font weight control |
| Typography - Align | align, ta | left, center, right, justify | Text alignment |
| Typography - Leading | leading | tight, normal, relaxed | Line height control |
| Effects - Rounded | rounded | none, sm, md, lg, xl, 2xl, 3xl, full | Border radius |
| Effects - Shadow | shadow | none, sm, md, lg, xl, 2xl, inner | Box shadow application |
| Effects - Border | border | 0, default, 2, 4 + directional variants | Border thickness |
| Flexbox/Grid | gap, justify, items, cols, rows | Contextual values | Layout alignment and spacing |
Spacing Variants
Spacing variants control padding and margin using a consistent scale. Each variant supports directional modifiers.Spacing Variant Structure
Color Variants
Color variants apply the design system’s semantic color tokens to backgrounds, text, and borders. Colors automatically support dark mode through Tailwind CSS variables.Color Variant Mapping
Layout Variants
Layout variants control element dimensions and positioning. Width and height variants support responsive and intrinsic sizing.Layout Sizing Values
| Prop | Values | Generated Classes | Use Case |
|---|---|---|---|
w | auto, full, screen, fit, min, max, px, numeric | w-auto, w-full, w-screen, w-fit, w-3 to w-12 | Width control |
h | auto, full, screen, fit, min, max, px, numeric | h-auto, h-full, h-screen, h-fit, h-3 to h-12 | Height control |
minH | full, screen, 200px, 300px, 400px, 500px | min-h-full, min-h-screen, min-h-[200px] | Minimum height |
maxW | none, sm, md, lg, xl, 2xl, 4xl, 6xl, full, screen-* | max-w-sm, max-w-md, max-w-screen-lg | Maximum width constraints |
position | static, relative, absolute, fixed, sticky | static, relative, absolute, fixed, sticky | Positioning mode |
Typography Variants
Typography variants control text appearance: size, weight, alignment, and leading (line height).Typography Variant Matrix
Effects Variants
Effects variants apply visual enhancements: rounded corners, shadows, and borders.Effects Variant Categories
| Category | Prop | Values | Generated Classes |
|---|---|---|---|
| Rounded | rounded | none, sm, md, lg, xl, 2xl, 3xl, full | rounded-none, rounded-sm, rounded-md, rounded-lg, rounded-xl, rounded-2xl, rounded-3xl, rounded-full |
| Shadow | shadow | none, sm, md, lg, xl, 2xl, inner | shadow-none, shadow-sm, shadow-md, shadow-lg, shadow-xl, shadow-2xl, shadow-inner |
| Border | border | 0, default, 2, 4 + directional (t, r, b, l) | border-0, border, border-2, border-4, border-t, border-r, border-b, border-l |
CVA Engine and Class Generation
The variant system usesclass-variance-authority (CVA) to transform prop-based API into Tailwind CSS classes.
CVA Processing Flow
Extraction and Whitelist System
The build system extracts all CVA-defined classes to generate a whitelist for Tailwind CSS purging andtw-merge utilities.
Extraction Pipeline
-
Recursively scan
src/core/variants/for.tsand.tsxfiles scripts/cva-extractor.ts67-93 -
Parse TypeScript using Babel parser with plugins
['typescript', 'jsx']scripts/cva-extractor.ts103-106 -
Traverse AST to find
CallExpressionnodes where callee iscvascripts/cva-extractor.ts114-125 -
Extract classes from:
- String literals (base classes) scripts/cva-extractor.ts129-137
- Object expressions (variant definitions) scripts/cva-extractor.ts138-142
- Nested objects and arrays scripts/cva-extractor.ts164-195
- Split by whitespace to separate individual class names scripts/cva-extractor.ts200-203
- Deduplicate into a Set, then sort alphabetically scripts/cva-extractor.ts46-51
Variant Composition
Variants are designed to be composable—multiple variants can be combined on a single component to achieve complex designs.Composition Patterns
Composition Priority
When variants conflict, the order of precedence is:- Component-specific props (higher specificity)
- Directional variants (
px,py) override general variants (p) - Last prop wins in the component prop list
classNameprop overrides all variants (escape hatch)
Type Safety and IntelliSense
The variant system provides full TypeScript support for type-safe prop usage and IntelliSense autocomplete.Type Definition Structure
- Autocomplete: Type
<Card p=and IntelliSense shows'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' - Type checking: Using
<Card p="invalid">produces TypeScript error - Documentation: Hover over props to see available values
- Refactoring: Rename variant values safely across the codebase
Variant System File Structure
The variant system is organized by concern insrc/core/variants/:
Usage Patterns
Pattern 1: Direct Primitive Usage
Primitives (Block, Box, Grid, Flex, Stack) directly apply variants:
Pattern 2: Composite Component Prop Forwarding
Composite components (Card, Button, Badge) extend primitives and forward variant props:
Pattern 3: Layout Template Composition
Layouts use variants for consistent spacing and structure:Summary
The Variant System provides the foundational styling layer for@ui8kit/core through:
- 12 Composable Categories: Spacing, colors, layout, typography, and effects covering ~80% of design scenarios
- CVA-Based Engine: Type-safe variant resolution using
class-variance-authority - 618 Extracted Classes: Build-time extraction generates whitelist for Tailwind purging
- Prop-Based API: Clean developer experience with
p="lg"instead ofclassName="p-8" - Full Type Safety: TypeScript types provide IntelliSense and compile-time validation
- Composition Patterns: Variants combine seamlessly across primitives, composites, and layouts