Components
Components are the building blocks of the ttoss design system. They are reusable UI elements that consume design tokens and themes to create consistent, accessible interfaces.
What is a Component?
A component in our design system is a reusable piece of UI that combines:
- Visual styling from design tokens
- Interaction behavior through props and state
- Accessibility features built-in by default
- Theme integration for consistent appearance
Component Architecture
Foundation Components
Purpose: Basic building blocks for interfaces
Examples: Button
, Input
, Text
, Box
, Flex
// Simple foundation component
import { Button } from '@ttoss/ui';
<Button variant="primary">Click me</Button>;
Pattern Components
Purpose: Complex compositions combining multiple foundation components
Examples: Card
, Modal
, Drawer
, Table
// Pattern component combining multiple elements
import { Card } from '@ttoss/ui';
<Card>
<Card.Title>Card Header</Card.Title>
<Card.Body>Main content goes here</Card.Body>
<Card.Footer>Actions or metadata</Card.Footer>
</Card>;
How Components Use Tokens
Semantic Token Integration
Components consume semantic tokens through theme variants:
// Component variants defined in theme
export const BruttalTheme: Theme = {
buttons: {
primary: {
backgroundColor: 'action.background.primary.default',
color: 'action.text.secondary.default',
':hover': {
backgroundColor: 'action.background.primary.active',
},
},
},
};
// Component automatically applies variant styles
<Button variant="primary">Primary Button</Button>;
Direct Token Access
Components can also access tokens directly through the sx
prop:
<Box
sx={{
backgroundColor: 'display.background.secondary.default',
color: 'display.text.primary.default',
padding: 'lg',
borderRadius: 'md',
}}
>
Custom styled content
</Box>
Component Types
Basic Components
Single-purpose elements with straightforward APIs:
// Text component
<Text variant="body">Simple text content</Text>
// Input component
<Input
placeholder="Enter text..."
leadingIcon="search"
trailingIcon="close"
/>
Compound Components
Components that provide multiple related sub-components:
// Card compound component
<Card>
<Card.Title>Project Overview</Card.Title>
<Card.Body>
<Text>Project details and statistics</Text>
</Card.Body>
<Card.Footer>
<Button variant="primary">View Details</Button>
</Card.Footer>
</Card>
// Layout compound component
<Layout>
<Layout.Header>
<Text variant="heading">App Title</Text>
</Layout.Header>
<Layout.Sidebar>
<Navigation />
</Layout.Sidebar>
<Layout.Main>
<MainContent />
</Layout.Main>
</Layout>
Higher-Order Components
Components that wrap and enhance other components:
// FormField wraps input components with labels and validation
<FormField name="email" label="Email Address">
<Input type="email" />
</FormField>
// Modal wraps content with overlay and positioning
<Modal isOpen={isOpen} onClose={handleClose}>
<ModalContent />
</Modal>
Component Implementation Patterns
Theme-Aware Styling
All components use the Theme UI sx
prop for styling:
export const Button = ({ variant = 'primary', ...props }) => {
return (
<button
{...props}
sx={{
// Maps to theme.buttons[variant]
variant: `buttons.${variant}`,
cursor: 'pointer',
':disabled': {
cursor: 'not-allowed',
},
...props.sx, // Allow style overrides
}}
/>
);
};
Responsive Design
Components support responsive values through arrays:
<Box
sx={{
padding: ['sm', 'md', 'lg'], // Responsive padding
fontSize: ['md', 'lg', 'xl'], // Responsive font size
flexDirection: ['column', 'row'], // Responsive layout
}}
>
Responsive content
</Box>
Accessibility Built-In
Components include accessibility features by default:
export const IconButton = ({ 'aria-label': ariaLabel, ...props }) => {
return (
<button
{...props}
type="button"
aria-label={ariaLabel}
sx={{
cursor: 'pointer',
':focus': {
outline: '2px solid',
outlineColor: 'action.border.accent.default',
},
}}
/>
);
};
Using Components
Installation and Setup
npm install @ttoss/ui @ttoss/theme
import { ThemeProvider } from '@ttoss/ui';
import { BruttalTheme } from '@ttoss/theme/Bruttal';
export const App = () => (
<ThemeProvider theme={BruttalTheme}>
<YourApplication />
</ThemeProvider>
);
Basic Usage
import { Button, Box, Text, Flex } from '@ttoss/ui';
export const Example = () => (
<Box sx={{ padding: 'lg' }}>
<Text variant="heading">Welcome</Text>
<Flex sx={{ gap: 'md', marginTop: 'md' }}>
<Button variant="primary">Get Started</Button>
<Button variant="secondary">Learn More</Button>
</Flex>
</Box>
);
Advanced Patterns
import { Card, Badge, Input, Select } from '@ttoss/ui';
export const UserProfile = () => (
<Card>
<Card.Title>
<Flex sx={{ alignItems: 'center', justifyContent: 'space-between' }}>
<Text>User Profile</Text>
<Badge variant="positive">Active</Badge>
</Flex>
</Card.Title>
<Card.Body>
<Flex sx={{ flexDirection: 'column', gap: 'md' }}>
<Input label="Full Name" placeholder="Enter your name" />
<Select
label="Role"
options={[
{ label: 'Admin', value: 'admin' },
{ label: 'User', value: 'user' },
]}
/>
</Flex>
</Card.Body>
<Card.Footer>
<Flex sx={{ gap: 'sm', justifyContent: 'flex-end' }}>
<Button variant="secondary">Cancel</Button>
<Button variant="primary">Save Changes</Button>
</Flex>
</Card.Footer>
</Card>
);
Component Categories
Layout Components
Box
: Basic container with styling capabilitiesFlex
: Flexbox container for flexible layoutsGrid
: CSS Grid container for complex layoutsContainer
: Centered container with max-width
Form Components
Input
: Text input with icons and validation statesSelect
: Dropdown selection with search capabilitiesButton
: Interactive buttons with multiple variantsCheckbox
,Radio
: Selection controlsSwitch
: Toggle switch for boolean values
Data Display
Text
: Typography with semantic variantsHeading
: Heading hierarchy (h1-h6)Badge
: Status indicators and labelsCard
: Content containers with header/body/footerTable
: Data tables with sorting and filtering
Feedback Components
Spinner
: Loading indicatorsToast
: Temporary notificationsModal
: Overlay dialogsTooltip
: Contextual help information
Best Practices
Component Usage
- Use semantic variants instead of custom styling when possible
- Leverage compound components for complex UI patterns
- Apply responsive design through token arrays
- Maintain accessibility by providing proper labels and ARIA attributes
Styling Guidelines
// ✅ Good: Use semantic tokens and variants
<Button variant="primary">Submit</Button>
<Box sx={{
backgroundColor: 'display.background.secondary.default',
padding: 'lg'
}}>
Content
</Box>
// ❌ Avoid: Direct CSS values
<Button style={{ backgroundColor: '#0066cc' }}>Submit</Button>
Accessibility Considerations
- Always provide meaningful labels for interactive elements
- Use proper heading hierarchy
- Ensure sufficient color contrast
- Test with keyboard navigation
- Include screen reader support
Next Steps
- Browse all components - Interactive component library
- Explore themes - How components integrate with themes
- Design tokens - Understand the foundation layer
Resources
- Component Storybook - Interactive examples and documentation
- Theme UI Documentation - Underlying styling system
- Accessibility Guidelines - WCAG compliance reference