Component Libraries — UI/UX

A component is one piece of UI made once and used everywhere. A component library is the collection of those components plus their documentation. Hand-made…

Open three different pages of a tidy app. The buttons are the same height, the same roundness, the same colour. That isn't because the designer copied carefully. There's only one button, and it's used in three places.

One piece of UI (a button, a field, a room card) designed and coded once, then used everywhere, is a component. Its looks and states are decided from the start. A component library is the collection of those components plus their documentation (the instructions for using them). A component library is like the standard room kit at Mrs. Sri's homestay: every room gets the same reading lamp, the same key tag and the same "Do not disturb" sign. The sign has two sides and the lamp comes in two sizes, like a component with a few choices of look. If Mrs. Sri changes the key tag design, she changes the kit list once, and all the rooms follow. If every room bought its own things, a year later no two rooms would match.

One source, many uses

The Attic and Garden room cards were once copied from the Terrace room card, then changed a little. Today Mrs. Sri raises the height of the button on the Terrace card from 40px to 48px. How many of the three "Choose" buttons are 48px now? Guess first.

Here are three room cards made by copying. We measured the height of their buttons in Chromium, WebKit and Firefox with getBoundingClientRect() (a JavaScript command that reads an element's size). The result was the same in all three:

Card Where it came from Button height Anything else
Terrace Room the one Mrs. Sri just changed 48px
Attic Room a copy, border: 0 38px its green is #16803d, a typo
Garden Room a copy, padding: 8px 42px fully booked, but its button stays green because the copy has no :disabled style (the look of a disabled button)

So the answer to the guess: only one. A copy doesn't change when the original changes, so each copy slowly becomes more different from the original. This is called drift (slowly moving away).

A component turns this around: not many copies, but one source used in many places. In plain JavaScript, a component can be a function that takes data and returns markup (HTML code) as text. In this code, the function roomCard makes one room card, then innerHTML puts all three cards into #rooms:

<div class="rooms" id="rooms"></div>

<script>
  function roomCard({ name, price, guests, status = "available" }) {
    const priceText = price.toLocaleString("en-US", { style: "currency", currency: "USD" }); // 35 becomes "$35.00"
    let action = `<button type="button" class="btn btn--primary btn--lg">Choose ${name}</button>`;
    if (status === "full") action = `<button type="button" class="btn btn--secondary btn--lg" disabled>Full on 12 Dec</button>`;
    return `<article class="room-card room-card--${status}">
      <h2>${name}</h2>
      <p>${priceText} / night · ${guests} guests</p>
      ${action}
    </article>`;
  }

  document.querySelector("#rooms").innerHTML = [
    roomCard({ name: "Terrace Room", price: 35, guests: 2 }),
    roomCard({ name: "Attic Room", price: 27.5, guests: 2 }),
    roomCard({ name: "Garden Room", price: 42.5, guests: 4, status: "full" }),
  ].join("");
</script>

Anatomy: variants, states, sizes, props

Every component in a library needs four things: variants (its versions of the look), states (how it is while in use), sizes, and props (the inputs that the people who use the component may set). Here are the three components of Mrs. Sri's page:

Button .btn Field .field Room card .room-card
Variants primary, secondary, ghost text, tel, email available, selected, full
Sizes sm 32px, md 40px, lg 48px one: 44px compact, with photo
States default, hover, active, focus-visible, disabled, loading default, focus, invalid, disabled loading (skeleton), default, photo failed to load
Props label, variant, size, disabled, busy label, type, help, error, disabled name, price, guests, status

A component's API is the way you use it from outside. In plain HTML and CSS, the button component's API is its classes and attributes:

.btn { padding: 7px 16px; font: inherit; font-weight: 600; line-height: 24px; border: 1px solid transparent; border-radius: var(--radius-md); }
.btn--sm { padding: 3px 12px; font-size: 14px; }  /* 32px */
.btn--lg { padding: 11px 16px; }                  /* 48px */
.btn--primary   { color: var(--on-action); background: var(--action); }
.btn--secondary { color: var(--action); background: var(--surface-card); border-color: var(--action); }
.btn--ghost     { color: var(--action); background: transparent; }
.btn:focus-visible { outline: 3px solid var(--text); outline-offset: 2px; }
.btn:disabled { color: var(--text-muted); background: var(--border); border-color: var(--border); cursor: not-allowed; }
.btn[aria-busy="true"] { cursor: progress; }

Documenting a component

A component without documentation gets used the wrong way, or copied again. One documentation page holds:

  1. Purpose: when to use it, when not to. "Room cards for the list of rooms; not for a booking summary."
  2. Variants, sizes and states, each with an example you can see.
  3. A props table, like this one for roomCard (string = text, number = a number):
Prop Type Default Note
name string required may be long: the title wraps to a new line
price number required per night: 35 is written "$35.00"
guests number required the most guests
status "available", "selected", "full" "available" full: a disabled button and the next free date
  1. Accessibility notes: the button label names the room ("Choose Terrace Room"), so a screen reader user can tell the three buttons apart.
  2. Example code to copy.

Many teams write this documentation in Storybook, a tool that calls itself "a frontend workshop for building UI components and pages in isolation" (separately from the app). In Storybook, each story shows one state of a component, for example a primary button in size lg that is loading, or a tel field whose content is wrong (invalid). The Lab on this page works the same way.

Use a library or build your own

There are three kinds to choose from. A headless library is a library with no look: it holds only behaviour.

Kind Examples What you get What you do
Full library Material UI, Ant Design a finished look and behaviour adapting it to the brand, often fighting its default styles
Headless library Radix Primitives, React Aria, Headless UI behaviour: keyboard, focus, ARIA attributes all of the CSS
Code copied into the project shadcn/ui component code dropped into your project folder by npx shadcn@latest add button maintaining that code yourself

shadcn/ui says of itself: "This is not a component library. It is how you build your component library." The code becomes yours.

How to choose for Mrs. Sri's site:

⚠️ How a library slowly falls apart

TL;DR: A component is one piece of UI made once and used everywhere. A component library is the collection of those components plus their documentation. Hand-made copies slowly become different from the original (drift: buttons at 48, 38, 42px). With a component, one change reaches every use. Every component has four things, for example on a button: variants (versions of the look: primary, secondary, ghost), states (default, hover, active, focus-visible, disabled, loading), sizes (sm 32, md 40, lg 48px) and props (its inputs; on the room card: name, price, guests, status). Document the purpose, variants and states, a props table, accessibility notes and example code, for instance in Storybook (a tool for documenting components, one story = one state). Use built-in HTML elements first (<button>, <dialog>). For complex behaviour such as a date picker (a calendar for picking dates), use a headless library, which gives behaviour with no look (Radix Primitives, React Aria, Headless UI). A full library (Material UI, Ant Design) suits internal tools, and shadcn/ui (component code copied into your project) suits you if you want the component code to be yours.

⚡ Copies drift apart

Guess first: the Attic and Garden cards were copied from the Terrace card. Today the height of the Terrace card's button goes from 40px to 48px. How many of the three "Choose" buttons are 48px now?

Only one. The others are 38px and 42px, because a copy doesn't change when the original changes. This is called drift: copies slowly become different from the original.

Meaning: A component (one piece of UI made once, then used everywhere) turns this problem around: one source, many uses. A change at the source reaches every use.

⚡ One function, three cards

Write:

function roomCard({ name, price, guests, status = "available" }) {
  const priceText = price.toLocaleString("en-US", { style: "currency", currency: "USD" }); // 35 becomes "$35.00"
  return `<article class="room-card room-card--${status}">
    <h2>${name}</h2>
    <p>${priceText} / night · ${guests} guests</p>
  </article>`;
}

Meaning: roomCard is a room card component written as a JavaScript function. The function takes one room's data and returns the HTML code of its card, with ${name} replaced by the room's name. The object { name, price, guests, status } is the props, the component's inputs. When the function is called three times with three rooms' data, one change here reaches all three cards.

⚡ Four things in every component

Meaning: Every component in a library needs four things, for example on a button: variants (versions of the look: primary, secondary, ghost), states (default, hover, active, focus-visible, disabled, loading), sizes (sm 32, md 40, lg 48px) and props (inputs). Document them all, for instance in Storybook (a tool for documenting components), one story per state.

What you will learn