Skip to main content

UI Components

Relevant source files

Purpose and Scope

This page documents the Layer 2 composite UI components located in src/components/ui/. These 15 components provide a developer-friendly API that extends the core primitives with prop forwarding, variant integration, and semantic composition patterns. For documentation of the underlying primitives (Box, Block, Grid, Flex, Stack), see Core Components. For the variant system that powers styling, see Variant System. For complete API reference including all props, see UI Components API. Sources: README.md105-145 src/components/README.md1-20 .devin/wiki.json75-84

Architecture Position

UI Components occupy Layer 2 in the three-layer architecture, sitting between core primitives and layout templates:
Sources: README.md62-79 README.md105-145 src/components/README.md8-19

Core Principles

1. Prop Forwarding Architecture

UI components extend base primitives by forwarding variant props while adding component-specific functionality. This pattern enables developers to use spacing, layout, and styling props directly without managing className:
Sources: src/components/ui/Badge/Badge.tsx1-96 src/components/README.md8-19

2. TypeScript Type Composition

Components achieve type safety by composing interfaces from multiple variant type sources:
Sources: src/components/ui/Badge/Badge.tsx20-32 src/components/README.md206-221

3. data-class Convention

All UI components apply semantic data-class attributes for consistent DOM targeting and testing. This convention enables CSS selectors and test queries without relying on dynamically generated class names: Sources: src/components/ui/Badge/Badge.tsx59-92 src/components/ui/Accordion/Accordion.tsx67-182 src/components/README.md223-235

4. Compound Component Pattern

Complex components use nested sub-components with shared context for flexible composition:
Sources: src/components/ui/Accordion/Accordion.tsx9-182 README.md17 README.md125-144

Complete Component Catalog

The library provides 15 composite UI components organized by functional category: Sources: README.md370-386 src/components/README.md21-203

Prop Forwarding Implementation

Destructuring and Forwarding Pattern

UI components use a consistent pattern for prop forwarding illustrated by the Badge implementation:
Implementation example from Badge: src/components/ui/Badge/Badge.tsx34-95 Key characteristics:
  1. Selective destructuring - Extract only the variant props the component uses
  2. Spread operator - Collect remaining HTML attributes in ...props
  3. CVA resolution - Apply each variant function with corresponding prop
  4. Class merging - Use cn() utility to merge all class strings
  5. Forwarding - Pass merged className and spread props to base component
Sources: src/components/ui/Badge/Badge.tsx34-95

Variant Integration Patterns

Multi-Variant Composition

Components typically integrate 3-7 variant categories simultaneously:

Import and Application Pattern

Each UI component follows this import structure:
Sources: src/components/ui/Badge/Badge.tsx1-18 src/components/ui/Accordion/Accordion.tsx5-7

Type Safety Architecture

Interface Composition Strategy

UI components compose their TypeScript interfaces from multiple sources using Pick utility types for selective prop inclusion:

Example: Badge Type Composition

src/components/ui/Badge/Badge.tsx20-32 This pattern achieves:
  • Selective inclusion - Only expose spacing props needed (m, mx, my), not all padding props
  • Type inheritance - Extend base HTML props for event handlers
  • Variant integration - Include all props from variant interfaces
  • Custom extension - Add component-specific props (leftSection, rightSection, dot)

Example: Accordion Type Composition

src/components/ui/Accordion/Accordion.tsx26-32 src/components/ui/Accordion/Accordion.tsx91-93 Demonstrates:
  • Controlled/uncontrolled - Support both value patterns
  • Selective width - Pick only w from layout variants
  • Context typing - Type-safe context values for compound components
Sources: src/components/ui/Badge/Badge.tsx20-32 src/components/ui/Accordion/Accordion.tsx26-93

Compound Component Implementation

Context-Based State Sharing

Complex components like Accordion use React Context for state management across sub-components:

Accordion Implementation Pattern

The Accordion component demonstrates a two-level context hierarchy:
  1. AccordionContext - Manages global accordion state (open items, click handlers)
  2. AccordionItemContext - Provides item-specific value to triggers/content
  3. State management - Supports both controlled and uncontrolled modes
  4. Sub-component coordination - Trigger reads context to toggle, content reads to render
Sources: src/components/ui/Accordion/Accordion.tsx9-182

Card Compound Pattern

Card uses a simpler pattern without context, relying on prop composition:
Each sub-component:
  • Receives all variant props independently
  • Applies semantic data-class attributes
  • Forwards refs for DOM access
  • Extends base primitives
Sources: README.md125-144 src/components/README.md96-117

Usage Patterns

Basic Component Usage

Simple components with variant props:

Compound Component Composition

Complex components with nested structure:

Layout Component Usage

Responsive layouts with variant props:
Sources: README.md121-145 src/components/README.md23-203

Integration with Core Primitives

UI components extend core primitives while maintaining the same variant prop API: The inheritance relationship enables:
  1. Variant reuse - All spacing, layout, color variants from primitives
  2. Ref forwarding - Access underlying DOM elements via forwardRef
  3. HTML attributes - All standard HTML props pass through
  4. Type safety - Full TypeScript support across composition chain
Sources: src/components/README.md8-19 README.md81-103

Best Practices

1. Prefer Variants Over className

Use variant props instead of manual className for consistency:

2. Use data-class for Custom Styling

Target components via data-class attributes in custom CSS:

3. Leverage Compound Components

Use sub-components for semantic structure:

4. Apply Type Safety

Use TypeScript interfaces for prop validation:
Sources: src/components/README.md237-258 .devin/wiki.json200-209

Summary

UI Components in Layer 2 provide:
  • 15 composite components covering common UI patterns
  • Prop forwarding from core primitives to user-facing API
  • Type safety via composed TypeScript interfaces
  • Variant integration across 12 reusable variant categories
  • data-class convention for stable DOM targeting
  • Compound patterns for flexible composition
  • Semantic HTML with accessibility built-in
This architecture achieves the library’s goal of building complex interfaces with minimal code while maintaining type safety, flexibility, and consistency. For component-by-component API documentation, see UI Components API. For usage examples and workflows, see Basic Workflow. Sources: README.md105-145 src/components/README.md1-258 .devin/wiki.json75-84