Skip to main content

Variant System

Relevant source files

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

Sources: README.md64-88 scripts/cva-extractor.ts282-291 src/lib/core-classes.json1-624

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

Sources: README.md174-217 src/components/README.md206-221

Spacing Variants

Spacing variants control padding and margin using a consistent scale. Each variant supports directional modifiers.

Spacing Variant Structure

Example Usage:
Extracted Classes (sample):
Sources: README.md174-181 src/lib/core-classes.json261-463 src/components/README.md209-212

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

Example Usage:
Extracted Classes (sample):
Sources: README.md183-190 src/lib/core-classes.json26-576

Layout Variants

Layout variants control element dimensions and positioning. Width and height variants support responsive and intrinsic sizing.

Layout Sizing Values

Example Usage:
Extracted Classes (sample):
Sources: README.md192-199 src/lib/core-classes.json196-606

Typography Variants

Typography variants control text appearance: size, weight, alignment, and leading (line height).

Typography Variant Matrix

Example Usage:
Extracted Classes:
Sources: README.md201-208 src/lib/core-classes.json150-579

Effects Variants

Effects variants apply visual enhancements: rounded corners, shadows, and borders.

Effects Variant Categories

Example Usage:
Directional Border Variants:
Extracted Classes (sample):
Sources: README.md210-217 src/lib/core-classes.json48-540

CVA Engine and Class Generation

The variant system uses class-variance-authority (CVA) to transform prop-based API into Tailwind CSS classes.

CVA Processing Flow

CVA Definition Pattern: The CVA pattern used in variant files follows this structure:
Component Usage Pattern: Components import and apply variants:
Sources: scripts/cva-extractor.ts114-143 README.md283-304

Extraction and Whitelist System

The build system extracts all CVA-defined classes to generate a whitelist for Tailwind CSS purging and tw-merge utilities.

Extraction Pipeline

Extraction Script Details: The extraction script scripts/cva-extractor.ts1-339 implements these steps:
  1. Recursively scan src/core/variants/ for .ts and .tsx files scripts/cva-extractor.ts67-93
  2. Parse TypeScript using Babel parser with plugins ['typescript', 'jsx'] scripts/cva-extractor.ts103-106
  3. Traverse AST to find CallExpression nodes where callee is cva scripts/cva-extractor.ts114-125
  4. Extract classes from:
  5. Split by whitespace to separate individual class names scripts/cva-extractor.ts200-203
  6. Deduplicate into a Set, then sort alphabetically scripts/cva-extractor.ts46-51
Generated Whitelist Structure: The output file src/lib/core-classes.json1-624 contains:
Usage in Tailwind Configuration:
Sources: scripts/cva-extractor.ts1-339 src/lib/core-classes.json1-624

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:
  1. Component-specific props (higher specificity)
  2. Directional variants (px, py) override general variants (p)
  3. Last prop wins in the component prop list
  4. className prop overrides all variants (escape hatch)
Example: Directional Override
Example: className Escape Hatch
Sources: src/components/README.md238-257

Type Safety and IntelliSense

The variant system provides full TypeScript support for type-safe prop usage and IntelliSense autocomplete.

Type Definition Structure

Type Definition Example:
IntelliSense Benefits:
  1. Autocomplete: Type <Card p= and IntelliSense shows 'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'
  2. Type checking: Using <Card p="invalid"> produces TypeScript error
  3. Documentation: Hover over props to see available values
  4. Refactoring: Rename variant values safely across the codebase
Sources: README.md11-12

Variant System File Structure

The variant system is organized by concern in src/core/variants/:
Export Pattern: All variants are exported from src/index.ts1-3:
This allows consumers to import variants:
Sources: src/index.ts1-3

Usage Patterns

Pattern 1: Direct Primitive Usage

Primitives (Block, Box, Grid, Flex, Stack) directly apply variants:
Sources: README.md82-103 src/components/README.md23-92

Pattern 2: Composite Component Prop Forwarding

Composite components (Card, Button, Badge) extend primitives and forward variant props:
Sources: README.md38-145 src/components/README.md94-149

Pattern 3: Layout Template Composition

Layouts use variants for consistent spacing and structure:
Sources: README.md147-168

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 of className="p-8"
  • Full Type Safety: TypeScript types provide IntelliSense and compile-time validation
  • Composition Patterns: Variants combine seamlessly across primitives, composites, and layouts
This architecture eliminates style duplication, reduces CSS complexity, and maintains type safety while providing unlimited design flexibility through composition. Sources: README.md1-453 .devin/wiki.json19-22 scripts/cva-extractor.ts1-339 src/lib/core-classes.json1-624