Components
Patterns, usage rules, and best practices for UI Lab components
Component Guidelines
Component guidelines define how UI Lab components should be used, when to use them, and best practices for implementation. These guidelines ensure consistency across interfaces and help teams make better design decisions.
Component Philosophy
When to Create Components
Components should be created when:
- A pattern repeats across multiple interfaces
- The pattern has distinct, well-defined states
- The component would reduce code duplication
- The pattern is complex enough to warrant isolation
When NOT to Create Components
Don't create components for:
- One-off implementations
- Simple elements (use HTML elements instead)
- Experimental features
- Styles-only variations without logic changes
Component Structure
All components follow a consistent structure:
Template
Guidelines
- Props: Keep prop interfaces simple and intuitive
- Defaults: Provide sensible defaults for optional props
- Variants: Use variant prop for style-based variations
- Sizes: Provide common size options (sm, md, lg)
- Accessibility: Include ARIA attributes where needed
- Flexibility: Allow className for emergency customization
Button Component
Purpose
Buttons trigger actions or navigations. They're the primary way users interact with interfaces.
When to Use
- Actions: Submit forms, create items, delete
- Navigation: Links to other pages or sections
- Toggles: Toggle features on/off
- Menus: Open dropdown menus or popovers
Variants
Primary
- Most prominent, use for main action
- Background: accent-600
- Hover: accent-700
Secondary
- Less prominent, use for secondary actions
- Background: background-100
- Hover: background-200
Danger
- For destructive actions
- Background: danger-600
- Hover: danger-700
Ghost
- Minimal style, use for tertiary actions
- Background: transparent
- Hover: background-100
Sizes
- Small (sm): 32px height, 12px text
- Medium (md): 40px height, 14px text (default)
- Large (lg): 48px height, 16px text
States
- Default: Interactive and clickable
- Hover: Slightly darker or elevated
- Active: Clearly pressed/selected state
- Disabled: Reduced opacity, no interaction
- Loading: Show spinner, disable interaction
Props
Usage Examples
Accessibility
- Focus states: Clear focus ring
- Keyboard: Fully keyboard accessible
- Screen readers:
aria-labelfor icon-only buttons - Disabled: Proper
disabledattribute
Common Mistakes
- Using buttons for navigation (use links instead)
- Multiple primary buttons on same screen
- Confusing buttons and links visually
- Missing disabled visual feedback
Input Component
Purpose
Inputs collect user text data. They're essential for forms and searches.
Types
Text Input
Email Input
Password Input
Number Input
Search Input
States
- Default: Ready for input
- Focus: Clear focus indicator
- Filled: User has entered data
- Disabled: Cannot be edited
- Error: Invalid input, show error message
- Success: Valid input confirmation
Sizing
- Small (sm): 32px height
- Medium (md): 40px height (default)
- Large (lg): 48px height
Props
Usage Examples
Validation
- Required fields: Show required indicator
- Real-time validation: Validate as user types
- Error messages: Clear, actionable messages
- Success feedback: Confirm valid input
Accessibility
- Labels: Always use associated labels
- Help text: Provide context below input
- Error messages: Linked to input via
aria-describedby - Keyboard: Full keyboard support
Select Component
Purpose
Selects allow users to choose from predefined options. Use instead of free-text input when options are limited.
When to Use
- Limited number of options (< 30)
- Options are clearly defined
- Space is limited
- Consistency is important
When NOT to Use
- Large number of options (use autocomplete/combobox)
- Users need to create new values
- Options need rich formatting
- Tree structure (use custom dropdown)
States
- Default: Ready for selection
- Open: Dropdown visible, showing options
- Selected: Option chosen, highlighted
- Disabled: Cannot be changed
- Error: Invalid selection
Props
Usage Examples
Accessibility
- Keyboard navigation: Arrow keys to navigate options
- Type to search: First letter jumps to matching option
- Screen readers: Options properly announced
- Labels: Paired with form labels
Card Component
Purpose
Cards are containers for grouped content. They provide visual organization and hierarchy.
When to Use
- Organizing related content
- Creating visual grouping
- Elevating content hierarchy
- Creating repeating patterns (grids)
Variants
Default
Interactive
Elevated
Props
Usage Examples
Form Guidelines
Structure
Best Practices
- Logical grouping: Group related fields
- Clear labels: Every input needs a clear label
- Help text: Provide context when needed
- Validation: Validate on change and submit
- Error handling: Show errors clearly
- Spacing: Adequate space between fields
- Action buttons: Submit and cancel buttons
- Loading state: Show feedback during submission
Responsive Components
Breakpoints
- Mobile: < 640px
- Tablet: 640px - 1023px
- Desktop: 1024px+
Responsive Guidelines
- Buttons: Increase padding on mobile
- Inputs: Full width on mobile, constrained on desktop
- Cards: Stack on mobile, grid on desktop
- Spacing: Increase gaps on mobile for touch
- Text: Increase sizes on mobile for readability
Dark Mode Support
All components support dark mode automatically through CSS variables:
Colors automatically adjust based on prefers-color-scheme or data-theme attribute.
Performance Considerations
Optimization Tips
- Memoization: Use React.memo for expensive components
- Lazy loading: Lazy load heavy components
- Virtualization: Virtualize long lists
- Code splitting: Split large component libraries
- Bundle size: Keep components lean
Testing Components
Unit Tests
Test component rendering, props, and behavior:
Accessibility Tests
- Focus management
- ARIA attributes
- Keyboard navigation
- Screen reader compatibility
Visual Tests
- All states (default, hover, active, disabled)
- All variants
- All sizes
- Dark mode
Common Patterns
Loading State Pattern
Error State Pattern
Confirmation Pattern
Disabled State Pattern
Customization
When to Customize
Components can be customized using the className prop for emergency needs:
When NOT to Customize
Don't customize for:
- Color changes (create a new variant)
- Size changes (use size prop)
- Spacing changes (design system mismatch)
- Behavior changes (create a new component)
Contributing New Components
When creating new components:
- Define purpose: What problem does it solve?
- Design variants: What variations are needed?
- Plan props: What customization is needed?
- Accessibility: ARIA attributes, keyboard support
- Documentation: Add to this guide
- Tests: Unit and accessibility tests
- Examples: Real usage examples