Compound Component Pattern
Components are structured as a collection of sub-components that work together. Each component is namespaced and exported both individually and as a grouped namespace.Example: Card Component
Data Attributes for Styling
Every component renders three data attributes for precise styling and debugging:data-scope- The component family (e.g.,"card","carousel","form")data-part- The specific part within the component (e.g.,"root","header","title")data-slot- A combined identifier (e.g.,"card-header","carousel-button")
From the Card Source
card.tsx
- CSS targeting without relying on class names
- Style encapsulation - scope styles to specific components
- Developer experience - easy debugging in DevTools
Export Pattern
Components use a dual export pattern:card-parts.ts
UI Components vs Utility Components
Zayne Labs UI includes two types of components:UI Components (ui/*)
Full-featured interactive components with state management:
- Card - Presentational card layouts
- Carousel - Image/content carousel with navigation
- Form - Form field management with validation
- DropZone - File upload areas
- Context providers for state sharing
- Event handlers and interactivity
- Complex composition patterns
Utility Components (common/*)
Lightweight rendering utilities without styling:
- For - Array rendering with type safety
- Show - Conditional rendering helper
- Slot - Component composition utility
- ErrorBoundary - Error handling boundary
Carousel Example
The Carousel demonstrates advanced compound component patterns:carousel.tsx
- Render props for dynamic content (
ItemListandIndicatorList) - Shared context - state managed in
Root, consumed by children - Flexible composition - add/remove/reorder parts as needed
Key Principles
Composability
Components are building blocks. Mix and match parts to create custom layouts.
Flexibility
Use only what you need. Every part is optional and customizable.
Type Safety
Full TypeScript support with generic props and type inference.
Accessibility
Semantic HTML by default with polymorphic
as prop support.Next Steps
Styling
Learn how to style components with Tailwind or vanilla CSS
TypeScript
Explore type signatures and polymorphic props