UI Components
Relevant source files- .devin/wiki.json
- README.md
- src/components/README.md
- src/components/ui/Accordion/Accordion.tsx
- src/components/ui/Badge/Badge.tsx
Purpose and Scope
This page documents the Layer 2 composite UI components located insrc/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: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 managingclassName:
2. TypeScript Type Composition
Components achieve type safety by composing interfaces from multiple variant type sources:3. data-class Convention
All UI components apply semanticdata-class attributes for consistent DOM targeting and testing. This convention enables CSS selectors and test queries without relying on dynamically generated class names:
| Component | Root data-class | Sub-component data-classes |
|---|---|---|
| Badge | data-class="badge" | badge-dot, badge-left-section, badge-right-section |
| Card | data-class="card" | card-header, card-title, card-description, card-content, card-footer |
| Accordion | data-class="accordion" | accordion-item, accordion-trigger, accordion-content |
| Button | data-class="button" | button-left-section, button-right-section |
4. Compound Component Pattern
Complex components use nested sub-components with shared context for flexible composition:Complete Component Catalog
The library provides 15 composite UI components organized by functional category:| Component | Location | Base Primitive | Primary Purpose | Key Variants |
|---|---|---|---|---|
| Button | src/components/ui/Button | BaseButton | Interactive actions | buttonStyleVariants, buttonSizeVariants |
| Card | src/components/ui/Card | BaseCard | Content containers | spacingVariants, roundedVariants, shadowVariants |
| Badge | src/components/ui/Badge | BaseBadge | Status indicators | badgeSizeVariants, badgeStyleVariants |
| Text | src/components/ui/Text | Box | Semantic text elements | typographyVariants, colorVariants |
| Title | src/components/ui/Title | Box | Heading hierarchy (h1-h6) | typographyVariants, colorVariants |
| Container | src/components/ui/Container | Block | Responsive containers | layoutVariants, spacingVariants |
| Icon | src/components/ui/Icon | Box | SVG icon wrapper | colorVariants, spacingVariants |
| Image | src/components/ui/Image | Box | Optimized images | roundedVariants, shadowVariants, layoutVariants |
| Group | src/components/ui/Group | Flex | Horizontal grouping | flexVariants, spacingVariants |
| Stack | src/components/ui/Stack | Flex | Vertical/horizontal stacking | flexVariants, spacingVariants |
| Grid | src/components/ui/Grid | Grid | CSS Grid layouts | gridVariants, spacingVariants |
| Sheet | src/components/ui/Sheet | Block | Drawer/sheet overlays | layoutVariants, shadowVariants |
| Accordion | src/components/ui/Accordion | Context-based | Expandable sections | flexVariants, layoutVariants |
| Flex | src/components/ui/Flex | Flex | Flexbox layouts | flexVariants, spacingVariants |
| Block | src/components/ui/Block | Block | Semantic sections | All variants via forwarding |
Prop Forwarding Implementation
Destructuring and Forwarding Pattern
UI components use a consistent pattern for prop forwarding illustrated by the Badge implementation:- Selective destructuring - Extract only the variant props the component uses
- Spread operator - Collect remaining HTML attributes in
...props - CVA resolution - Apply each variant function with corresponding prop
- Class merging - Use
cn()utility to merge all class strings - Forwarding - Pass merged className and spread props to base component
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:Type Safety Architecture
Interface Composition Strategy
UI components compose their TypeScript interfaces from multiple sources usingPick 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
wfrom layout variants - Context typing - Type-safe context values for compound components
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:- AccordionContext - Manages global accordion state (open items, click handlers)
- AccordionItemContext - Provides item-specific value to triggers/content
- State management - Supports both controlled and uncontrolled modes
- Sub-component coordination - Trigger reads context to toggle, content reads to render
Card Compound Pattern
Card uses a simpler pattern without context, relying on prop composition:- Receives all variant props independently
- Applies semantic
data-classattributes - Forwards refs for DOM access
- Extends base primitives
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:Integration with Core Primitives
UI components extend core primitives while maintaining the same variant prop API:| UI Component | Extends Primitive | Added Features |
|---|---|---|
| Button | BaseButton | Size variants, style variants, loading state, icon sections |
| Badge | BaseBadge | Size variants, style variants, dot indicator, icon sections |
| Card | BaseCard | Compound structure (Header, Content, Footer, Title, Description) |
| Text | Box | Semantic text elements (p, span, em, strong), truncation |
| Title | Box | Heading hierarchy (h1-h6), semantic order prop |
| Container | Block | Responsive size presets, centered prop |
| Icon | Box | Lucide icon integration, size scaling |
| Group | Flex | Horizontal layout defaults, simplified justify/align |
| Stack | Flex | Vertical/horizontal stacking, simplified gap prop |
- Variant reuse - All spacing, layout, color variants from primitives
- Ref forwarding - Access underlying DOM elements via forwardRef
- HTML attributes - All standard HTML props pass through
- Type safety - Full TypeScript support across composition chain
Best Practices
1. Prefer Variants Over className
Use variant props instead of manualclassName for consistency:
2. Use data-class for Custom Styling
Target components viadata-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: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