Design a Scalable Component Library
You're tasked with building a shared component library for a company with 10+ product teams and 200+ engineers.
Requirements
- Support theming (dark/light + per-product customization)
- Accessible by default (WCAG AA)
- Tree-shakeable
- Type-safe API
- Versioned and documented
Architecture
Token Layer
tokens/
colors.json â semantic color tokens
spacing.json â spacing scale
typography.json â font stack, sizes, weights
build.js â compile to CSS vars, SCSS, JS objectsTokens compile to multiple formats so any framework can consume them.
Component Layer
Each component follows the same pattern:
Button/
Button.tsx â main component
Button.types.ts â prop types
Button.css â styles using CSS custom properties
Button.test.tsx â unit + a11y tests
Button.stories.tsx â Storybook stories
index.ts â public APIKey API Design Decisions
Compound components over prop-heavy APIs:
// Flexible, composable
<Dialog>
<Dialog.Trigger asChild>
<Button>Open</Button>
</Dialog.Trigger>
<Dialog.Content>
<Dialog.Title>Settings</Dialog.Title>
<Dialog.Body>...</Dialog.Body>
<Dialog.Footer>
<Dialog.Close asChild>
<Button variant="ghost">Cancel</Button>
</Dialog.Close>
</Dialog.Footer>
</Dialog.Content>
</Dialog>Polymorphic components for flexibility:
<Button as="a" href="/settings">Settings</Button>
<Text as="label" htmlFor="email">Email</Text>Versioning Strategy
- Semantic versioning with changesets
- Breaking changes go through RFC process
- Codemods for automated migrations
- LTS branches for major versions
A component library is a product. The consumers are your fellow engineers. Design the API you'd want to use.