Mini Component Library
What you'll build
A set of 4 reusable components with variants (sizes, colors), a design token file, and a Storybook-like demo page.
Goals
- Design a consistent API for multiple components
- Use design tokens for colors, spacing, and typography
- Support variants (primary/secondary, sizes) via props
- Compose components into a demo page
Prerequisites
- React function components and props
- CSS or a styling solution (CSS Modules, Tailwind, etc.)
- Component composition (children prop)
Steps
- 1
Define design tokens
Create a tokens file with colors (primary, secondary, danger), spacing (xs, sm, md, lg), and font sizes. These are the single source of truth for styling. Acceptance: all components reference tokens, not hardcoded values.
Hint
CSS custom properties (variables) work well: --color-primary: #3b82f6; etc.
- 2
Build the Button component
Create a Button with variant (primary/secondary/danger) and size (sm/md/lg) props. Use the tokens for all styling. Acceptance: all 9 combinations (3 variants × 3 sizes) look consistent.
Hint
Forward refs and spread ...props so the button accepts onClick, disabled, type, etc.
- 3
Build Input and Card
Create an Input with label, error, and helper text props, and a Card with header, body, and footer slots (using children). Acceptance: both compose well with Button.
Hint
Card can use named slots: <Card.Header>, <Card.Body>, <Card.Footer> for a clean API.
- 4
Build a Modal component
Create a Modal with open/onClose props, a backdrop, and Escape-to-close. Use a portal (createPortal) so the modal renders outside the DOM tree. Acceptance: modal traps focus and closes on Escape and backdrop click.
Hint
createPortal renders into document.body so the modal isn't clipped by parent overflow or z-index.
- 5
Build a demo page
Create a page that showcases every component and variant — like a mini Storybook. Include a form (Input + Button), a card with content, and a modal triggered by a button. Acceptance: the demo demonstrates all components working together.
Hint
This demo page doubles as documentation — anyone can see the components and their props in action.
Try it yourself
What you learned
You built a component library with a design system — the foundation of scalable UIs. Consistent tokens and component APIs are what make teams productive and UIs coherent.
Related Resources
Related Lessons
- React Components
Reusable components with consistent APIs.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.