import { Meta } from 'storybook/blocks'; # Component Patterns Consistent patterns for building and using components throughout the application. ## Component Architecture ### Atomic Design Principles Our component library follows atomic design methodology: | Level | Description | Examples | |-------|-------------|----------| | **Tokens** | Design primitives | Colors, spacing, typography | | **Primitives** | Base UI elements | Button, Input, Card | | **Components** | Composed elements | ChatMessage, CitationCard | | **Patterns** | Reusable combinations | Form layouts, Card grids | | **Templates** | Page structures | Dashboard, Article page | ### File Structure ``` primitives/ ├── Button/ │ ├── Button.tsx # Component implementation │ ├── Button.test.tsx # Unit tests │ ├── Button.stories.tsx # Storybook stories │ └── index.ts # Public exports ``` ## Component API Patterns ### Props Interface Pattern All components use TypeScript interfaces for props: ```tsx export interface ButtonProps extends React.ButtonHTMLAttributes, VariantProps { /** Render as a different element using Slot */ asChild?: boolean; /** Show loading spinner */ loading?: boolean; /** Icon before text */ leftIcon?: React.ReactNode; /** Icon after text */ rightIcon?: React.ReactNode; } ``` ### forwardRef Pattern All primitive components forward refs: ```tsx export const Button = forwardRef( ({ className, variant, size, ...props }, ref) => { return ( ``` ## Modal Patterns ### Basic Modal Usage ```tsx const [isOpen, setIsOpen] = useState(false); setIsOpen(false)}> Confirm Action setIsOpen(false)} /> Are you sure you want to proceed? ``` ### Confirmation Dialog Pattern ```tsx function ConfirmDialog({ open, onClose, onConfirm, title, message }) { return (

{title}

{message}

); } ``` ## Table Patterns ### Basic Data Table ```tsx ( )} ]} data={users} keyExtractor={(row) => row.id} sortColumn={sortColumn} sortDirection={sortDirection} onSort={handleSort} onRowClick={(row) => openUserDetail(row)} /> ``` ### Table with Selection ```tsx const [selected, setSelected] = useState(new Set()); row.id} selectedRows={selected} onSelectRow={(key, checked) => { setSelected(prev => { const next = new Set(prev); checked ? next.add(key) : next.delete(key); return next; }); }} onSelectAll={(checked) => { setSelected(checked ? new Set(data.map(r => r.id)) : new Set()); }} /> ``` ## Navigation Patterns ### Tab Navigation ```tsx Overview Analytics Settings ``` ### Breadcrumb Navigation ```tsx router.push(item.href)} /> ``` ## Loading States ### Button Loading ```tsx ``` ### Skeleton Pattern ```tsx // Define skeleton for component function CardSkeleton() { return (
); } // Use during loading {isLoading ? : } ``` ## Error Handling ### Form Errors ```tsx ``` ### Empty States ```tsx } title="No results found" description="Try adjusting your search criteria" action={} /> ```