Skip to main content

Core Components

Relevant source files

Purpose 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.
Sources: README.md62-103 src/components/ui/Block/Block.tsx1-88

The Five Core Primitives

Overview Table

Sources: README.md81-103

Fundamental React Patterns

forwardRef Implementation

All core components implement forwardRef to expose the underlying DOM element reference to parent components. This enables imperative DOM operations and integration with third-party libraries.
Example structure from Block component: src/components/ui/Block/Block.tsx33-36
Sources: src/components/ui/Block/Block.tsx33-86

TypeScript Type Composition

Core components compose multiple TypeScript interfaces to achieve type safety across HTML attributes, variant props, and custom component props.
Type composition pattern from Block: src/components/ui/Block/Block.tsx20-31
Sources: src/components/ui/Block/Block.tsx20-31

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).
Prop destructuring pattern from Block: src/components/ui/Block/Block.tsx34-61
Sources: src/components/ui/Block/Block.tsx34-86

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 variant prop
  • Full width by default (w='full')
  • Supports all spacing, color, layout, rounded, shadow, and border variants
  • Uses data-class="block" for DOM targeting
Element type resolution: src/components/ui/Block/Block.tsx62-68
Usage examples:
Sources: src/components/ui/Block/Block.tsx1-88 README.md85-87

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
Usage examples:
Sources: README.md85 src/components/ui/Box/index.ts1

Grid: CSS Grid Layout

Grid provides CSS Grid layout capabilities with props for columns, rows, and gap. Key characteristics:
  • display: grid applied by default
  • cols prop for column count
  • rows prop for row count
  • gap prop for grid spacing
  • Supports spacing and layout variants
Usage examples:
Sources: README.md88

Flex: Flexbox Layout

Flex provides Flexbox layout capabilities with props for direction, alignment, and justification. Key characteristics:
  • display: flex applied by default
  • direction prop for flex-direction
  • align prop for align-items
  • justify prop for justify-content
  • Supports spacing and layout variants
Usage examples:
Sources: README.md88

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
  • direction prop: ‘vertical’ (default) or ‘horizontal’
  • gap prop for spacing between children
  • Automatic flex layout
Usage examples:
Sources: README.md89

Variant Application Pattern

Core components apply variants using the cn() utility function, which merges variant-generated classes with custom className props using tw-merge for conflict resolution.
Application pattern from Block: src/components/ui/Block/Block.tsx70-79
Sources: src/components/ui/Block/Block.tsx70-79

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-18

Export Pattern in Index Files

src/components/ui/Box/index.ts1
Export structure:
  • Named export for component
  • Named export for TypeScript props interface using type keyword
  • No default exports (enforces consistent import syntax)
Sources: src/components/ui/Block/Block.tsx1-18 src/components/ui/Box/index.ts1

data-class Attributes

All core components include a data-class attribute for consistent DOM targeting in tests, CSS selectors, and debugging tools.
Usage from Block component: src/components/ui/Block/Block.tsx69
Consumer testing example:
Sources: src/components/ui/Block/Block.tsx69

displayName Convention

All core components set displayName for improved debugging experience in React DevTools and error messages. src/components/ui/Block/Block.tsx88
Benefits:
  • Clear component names in React DevTools component tree
  • Improved error stack traces
  • Better debugging with React.StrictMode warnings
Sources: src/components/ui/Block/Block.tsx88

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.
Extension pattern (Card extends Block):
Composition pattern (DashLayout uses Grid):
Sources: README.md62-168

Summary

Core components implement five fundamental patterns:
  1. forwardRef - Expose DOM references to parent components
  2. Type Composition - Combine React.HTMLAttributes with variant prop types
  3. Prop Destructuring - Extract variant props and forward HTML attributes
  4. Variant Application - Apply CVA variants via cn() utility
  5. Semantic HTML - Render appropriate HTML5 elements via component prop
These patterns enable:
  • 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-class attributes
  • Clean React DevTools debugging with displayName
All higher-level components in the library either extend or compose these five primitives, ensuring architectural consistency throughout the system. Sources: README.md62-103 src/components/ui/Block/Block.tsx1-88 .devin/wiki.json56-63