Design a Component's Interface

Kwame’s navigation passes its tests, and the library’s site now has six components. Each one defines its colors, spacing, and states its own way, so reusing one means copying its CSS and patching it by hand.

You’ll turn the FAQ accordion into a component a page can reuse and restyle without touching its rules. You’ll decide what a page may change, style its states from the element itself, and write down what it promises its users. Start with the first page that wants to reuse it.

Give the Accordion a Styling Interface

Mei Lin Chen keeps asking the front desk two things about the step ladder, so Kwame adds them to its facts. In step-ladder.html, add these two disclosures after the “Next free” paragraph, inside the <section class="tool-facts">:

Language: HTML
<details class="faq-item">
  <summary>Will it fit in my car?</summary>
  <p>Folded, it's about 6 and a half feet long, so it
    fits most hatchbacks with the back seats down.</p>
</details>
<details class="faq-item">
  <summary>Can I use it outdoors?</summary>
  <p>Yes, on firm, level ground. Keep it off wet grass
    and mud.</p>
</details>

Save, then open both questions:

Two open questions inside the ladder's pale green facts box. Their answers sit on the same pale green, so nothing sets them apart from the box.
The Answer Panel Disappears Into the Facts Box

Each answer’s panel is the same pale green as the facts box. A page rule such as .tool-facts .faq-item::details-content would fix it, but it depends on how the accordion draws its panel. If Kwame changes that, every page that patched it breaks.

A component needs an interface: a short list of what a page may change, with the component deciding how to use each value. It works like a function’s parameters, except that a page sets the values as custom properties, and they arrive through the cascade, not a call.

Recap the token tiers: primitive tokens say what a value is, semantic tokens such as --color-panel say what it’s for, and component tokens say where one component uses it. The accordion’s border, corners, and focus ring need semantic size tokens too, alongside --space-s and --space-m, so add these to the :root rule in styles.css:

Language: CSS
--border-width: 2px;
--radius-s: 0.25rem;
--focus-width: 3px;
--focus-offset: 2px;

Then point the base :focus-visible rule at the last two, so every ring shares them:

Language: CSS
:focus-visible {
  outline: var(--focus-width) solid var(--color-accent);
  outline-offset: var(--focus-offset);
}

Create faq.css next to styles.css, and start it with the accordion’s tokens, each defaulting to a semantic token:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.