Core Components
Relevant source filesPurpose and Scope
This document covers the five foundational primitives in Layer 1 of the architecture:Block, Box, Grid, Flex, and Stack. These components serve as the building blocks for all higher-level UI components and layouts in the library. They provide direct access to the CVA variant system and implement fundamental React patterns including forwardRef, TypeScript type composition, and HTML attribute forwarding.
For documentation on composite components that extend these primitives (Button, Card, etc.), see UI Components. For variant system details, see Variant System.
Sources: README.md81-103 .devin/wiki.json56-63
Architectural Role
Core components form the foundation of the three-layer architecture. They are the only components that directly apply the CVA variant system and render semantic HTML5 elements. All Layer 2 components (UI Components) and Layer 3 components (Layouts) ultimately compose or extend these five primitives.The Five Core Primitives
Overview Table
| Component | Primary Purpose | Semantic HTML Support | Key Variants | Default Width |
|---|---|---|---|---|
Block | Semantic block containers | section, article, nav, header, footer, aside, main, div | spacing, color, layout, rounded, shadow, border | w='full' |
Box | Generic flexible container | Any ElementType (div, span, p, etc.) | spacing, color, layout | No default |
Grid | CSS Grid layouts | div with display: grid | spacing, layout, cols, rows, gap | No default |
Flex | Flexbox layouts | div with display: flex | spacing, layout, direction, align, justify | No default |
Stack | Vertical/horizontal stacking | div with flexbox stacking | spacing, gap, direction | No default |
Fundamental React Patterns
forwardRef Implementation
All core components implementforwardRef to expose the underlying DOM element reference to parent components. This enables imperative DOM operations and integration with third-party libraries.
TypeScript Type Composition
Core components compose multiple TypeScript interfaces to achieve type safety across HTML attributes, variant props, and custom component props.Prop Destructuring and HTML Attribute Forwarding
Core components destructure variant props explicitly and forward remaining HTML attributes to the underlying DOM element using the spread operator (...props).
Component-Specific Features
Block: Semantic HTML Container
Block is designed for semantic block-level elements. It defaults to w='full' (full width) and provides a variant prop for selecting semantic HTML5 elements.
Key characteristics:
- Semantic element selection via
variantprop - Full width by default (
w='full') - Supports all spacing, color, layout, rounded, shadow, and border variants
- Uses
data-class="block"for DOM targeting
Box: Flexible Generic Container
Box is the most flexible primitive, accepting any ElementType via the component prop. It has no default width and minimal defaults, making it suitable for inline and custom layouts.
Key characteristics:
- Accepts any React
ElementType(div, span, p, a, button, custom components) - No default width or height
- Supports spacing, color, and layout variants
- Uses
data-class="box"for DOM targeting
Grid: CSS Grid Layout
Grid provides CSS Grid layout capabilities with props for columns, rows, and gap.
Key characteristics:
display: gridapplied by defaultcolsprop for column countrowsprop for row countgapprop for grid spacing- Supports spacing and layout variants
Flex: Flexbox Layout
Flex provides Flexbox layout capabilities with props for direction, alignment, and justification.
Key characteristics:
display: flexapplied by defaultdirectionprop for flex-directionalignprop for align-itemsjustifyprop for justify-content- Supports spacing and layout variants
Stack: Simplified Stacking
Stack is a specialized flex container for vertical or horizontal stacking with consistent gap spacing.
Key characteristics:
- Simplified API compared to
Flex directionprop: ‘vertical’ (default) or ‘horizontal’gapprop for spacing between children- Automatic flex layout
Variant Application Pattern
Core components apply variants using thecn() utility function, which merges variant-generated classes with custom className props using tw-merge for conflict resolution.
Import and Export Pattern
Core components follow a consistent import/export structure for tree-shaking and type safety.Component File Structure
Each core component has a dedicated directory with two files:Import Pattern in Component Files
src/components/ui/Block/Block.tsx3-18Export Pattern in Index Files
src/components/ui/Box/index.ts1- Named export for component
- Named export for TypeScript props interface using
typekeyword - No default exports (enforces consistent import syntax)
data-class Attributes
All core components include adata-class attribute for consistent DOM targeting in tests, CSS selectors, and debugging tools.
displayName Convention
All core components setdisplayName for improved debugging experience in React DevTools and error messages.
src/components/ui/Block/Block.tsx88
- Clear component names in React DevTools component tree
- Improved error stack traces
- Better debugging with React.StrictMode warnings
Relationship with Higher Layers
Core components serve as the foundation for Layer 2 (UI Components) and Layer 3 (Layouts). Higher-level components either extend core components with additional props or compose multiple core components together.Summary
Core components implement five fundamental patterns:- forwardRef - Expose DOM references to parent components
- Type Composition - Combine
React.HTMLAttributeswith variant prop types - Prop Destructuring - Extract variant props and forward HTML attributes
- Variant Application - Apply CVA variants via
cn()utility - Semantic HTML - Render appropriate HTML5 elements via
componentprop
- Type-safe component APIs with full TypeScript inference
- Direct access to the variant system without prop forwarding overhead
- Flexible semantic HTML structure for accessibility
- Consistent DOM targeting via
data-classattributes - Clean React DevTools debugging with
displayName