# Components

> How to design system components that last: atomic design, anatomy, variants and properties, sizes, states, composition vs configuration, API design and a component checklist.

Web page: https://odeocreative.me/tutorials/design-systems/components
Author: Hecham Gazhi (https://odeocreative.me)

Track: [Design System Tutorial](/md/tutorials/design-systems.md)

## What makes a good system component

A component in a design system is a reusable piece of UI — a button, an input, a card — with a defined anatomy, a fixed set of options, every state designed, accessibility built in, and matching implementations in design and code.

Good components are **flexible enough** to cover real product needs, **constrained enough** to keep the UI consistent, and **well documented** so people use them correctly.

## Atomic design

Brad Frost's **atomic design** is a popular way to think about how components build on each other:

| Level | Description | Examples |
| --- | --- | --- |
| Atoms | Smallest building blocks | Button, input, label, icon, avatar |
| Molecules | Simple groups of atoms working together | Search field (input + button), form field (label + input + help text) |
| Organisms | Complex sections made of molecules and atoms | Header, product card grid, data table |
| Templates | Page-level layouts with placeholder content | Product listing layout |
| Pages | Templates with real content | The actual Shoes category page |

> Tip: You don't have to use these names. The useful idea is that small, well-built parts compose into larger ones — so fixing an atom improves everything built from it.

## Anatomy

Document the named parts of each component. Shared names make reviews and code easier.

```
Text field anatomy
┌─────────────────────────────────────────┐
  ① Label  "Email"                 ② Optional indicator
┌─────────────────────────────────────────┐
│ ③ Leading icon  ④ Input text       ⑤ Trailing action │  ← ⑥ Container
└─────────────────────────────────────────┘
  ⑦ Helper text / error message      ⑧ Character count
```

## Variants, properties and sizes

### Variants

Visual versions for different emphasis or purpose: primary, secondary, tertiary, destructive. Each variant should have a clear "when to use" rule.

### Properties

Options that can be combined: size (sm, md, lg), icon (none, leading, trailing, icon-only), full width (true/false), loading (true/false).

### Sizes

Fixed sizes mapped to the sizing and spacing tokens, so components of the same size align in a row.

> Common mistake: Variant explosion. Every new variant multiplies design, code, testing and documentation work — a button with 5 variants × 3 sizes × 4 icon options × 6 states already means 360 combinations. Add variants only when there's real, repeated need.

## States

| State | Applies to | Notes |
| --- | --- | --- |
| Default / rest | All |  |
| Hover | Pointer devices | Never the only way to reveal information |
| Focus-visible | All interactive | Clear ring for keyboard users; never remove outlines without replacement |
| Active / pressed | Buttons, list items |  |
| Selected / checked | Toggles, tabs, chips, rows | Use more than colour |
| Disabled | Controls | Consider whether disabling is the right choice |
| Loading | Buttons, components that fetch data | Prevent duplicate actions |
| Error / invalid | Form controls | Icon + text + colour |
| Read-only | Form controls | Visible and copyable but not editable |

## Configuration vs composition

There are two ways to make components flexible:

- **Configuration** — one component with many props: `<Card title image badge footer actions variant />`. Simple to use for common cases, but props pile up as needs grow.
- **Composition** — smaller parts combined freely: `<Card><Card.Media/><Card.Body/><Card.Actions/></Card>`. More flexible for unusual layouts, but more to learn.

> Tip: Many systems use both: configuration for simple, highly consistent components (Button, Badge) and composition for containers with varied content (Card, Modal, Table).

## Designing the component API

The API is the set of names and options people use — in Figma properties and in code props. Keep them aligned and predictable.

- Use the **same names** in Figma properties and code props.
- Choose sensible **defaults** so the simplest usage is the most common one.
- Prefer **enums** (`variant="danger"`) over many booleans (`isDanger isPrimary`), which can conflict.
- Don't expose raw style overrides (custom colours, margins) unless necessary — they undermine consistency.

```tsx
type ButtonProps = {
  variant?: "primary" | "secondary" | "tertiary" | "danger"; // default "primary"
  size?: "sm" | "md" | "lg";                                   // default "md"
  iconStart?: React.ReactNode;
  iconEnd?: React.ReactNode;
  loading?: boolean;
  disabled?: boolean;
  fullWidth?: boolean;
  type?: "button" | "submit" | "reset";
  children: React.ReactNode; // the label
};

<Button variant="primary" size="md" iconEnd={<ArrowRight />}>Continue</Button>
```

## Component checklist

Before a component is marked as ready, check that it has:

1. A clear purpose and "when to use / when not to use" guidance.
2. Named anatomy.
3. All variants, sizes and states designed and built.
4. Token-based styling only — no hard-coded values.
5. Responsive behaviour and content limits (long labels, wrapping, truncation).
6. RTL support.
7. Keyboard interaction, focus management and screen-reader labels.
8. Contrast checked in all themes.
9. Matching Figma component and code component with the same API.
10. Documentation with examples, do's and don'ts.
11. Tests: visual regression, accessibility and unit tests in code.

## Key takeaways

- System components have a defined anatomy, limited options, every state, built-in accessibility and matching design/code versions.
- Atomic design shows how small parts compose into bigger ones.
- Add variants only for real, repeated needs — variant explosion is costly.
- Use configuration for simple components and composition for flexible containers.
- Align the API across Figma and code, with good defaults and enum options.

## Exercise: Specify a component

1. Pick a component: text field, select or card.
2. Draw its anatomy and name every part.
3. List variants, sizes and states, and remove any that aren't truly needed.
4. Write its API as a TypeScript type and as Figma properties with the same names.
5. Go through the component checklist and note what's missing.

## Quiz

1. In atomic design, a search field made of an input and a button is a…
   - Atom
   - **Molecule** ✓
   - Organism
   - Template
   - Why: Molecules are simple groups of atoms working together.

2. Which state do keyboard users rely on most?
   - Hover
   - **Focus-visible** ✓
   - Disabled
   - Loading
   - Why: The focus indicator shows which element the keyboard is on.

3. Why keep the number of variants low?
   - Figma limits variants
   - **Each variant multiplies design, code, test and documentation work** ✓
   - Users dislike variety
   - Variants slow down the browser
   - Why: Every variant is a long-term maintenance cost.

4. Why prefer `variant="danger"` over boolean props like `isDanger` and `isPrimary`?
   - It's shorter
   - **Enums prevent conflicting combinations** ✓
   - Booleans don't work in React
   - It's required by Figma
   - Why: With booleans, isDanger and isPrimary could both be true.

5. A component made of freely combined parts like Card.Media and Card.Body uses…
   - Configuration
   - **Composition** ✓
   - Inheritance
   - Tokens
   - Why: Composition combines smaller parts for flexible layouts.
