Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 39 ยท Projects and Practice

Practice Exercises and Challenges

Practice custom elements, shadow DOM, slots, events, forms, accessibility and theming with graded exercises, runnable starters, hints and a final capstone challenge.

Reading about components is not the same as building them. This lesson gives you graded exercises that follow the course: properties and events, forms and accessibility, styling, and patterns. Each has a goal, a checklist and a hint. Two have runnable starters; solve them directly in the editor, then compare with the hints.

Work in this order: do the exercise, run it, break it on purpose (bad values, missing attributes, keyboard only), and fix what you find. That habit teaches more than any amount of reading.

Level 1: Properties and events

Exercise 1: <x-stepper>. A number control with minus and plus buttons.

  • Property value (clamped between min and max attributes, defaults 0 and 10).
  • Reflect value to the attribute.
  • Fire value-change with detail: { value }, only when it actually changes.
  • Disable the minus button at min and the plus at max.

Starter (complete the TODOs):

Exercise 1 starter: x-stepper

Hint: clamp with Math.min(this.max, Math.max(this.min, Number(v))) inside the setter. Dispatching from attributeChangedCallback guarantees the event fires whether the change came from a button, the property or an attribute edit. Remember that the first call (initial markup) also arrives there; skip it when oldV === null if you do not want an initial event.

Exercise 2: event delegation. Put ten <x-stepper> elements in a list and use one listener on the list to total their values. Which bubbles setting do you need?

Level 2: Shadow DOM, slots and styling

Exercise 3: <x-callout>. A note box with a named icon slot and a default slot.

  • Default icon (an emoji) shown through slot fallback content when no icon is supplied.
  • Attribute tone (info, warn, error) changing the accent through :host([tone=...]).
  • CSS variables --callout-bg and --callout-accent, and a part="content".
Exercise 3 starter: x-callout

Hint: set the variable on the host: :host([tone="warn"]) { --callout-accent: #d97706; --callout-bg: #fffbeb; }. For the page-side bold, write x-callout[tone="error"]::part(content) { font-weight: 700; }. Revisit shadow parts and host selectors if either feels fuzzy.

Exercise 4: shared styles. Convert Exercise 3 to a constructable stylesheet shared by all instances, and verify with 100 instances that you created only one CSSStyleSheet object.

Level 3: Forms and accessibility

Exercise 5: <x-tag-input>. A form-associated control collecting tags typed with Enter.

  • Submits the tags as a comma-separated string under its name.
  • required support through setValidity, with a clear message.
  • Backspace on an empty field removes the last tag; each tag has an accessible remove button.
  • Announces "added" and "removed" messages in a polite live region.

Exercise 6: keyboard audit. Take the stepper from Exercise 1 and make it a proper role="spinbutton": Up and Down arrows change the value, Page Up and Page Down change by 10, Home and End jump to min and max, and aria-valuenow, aria-valuemin, aria-valuemax are kept current through ElementInternals.

Exercise 7: reset and disabled. Add formResetCallback to Exercise 5 (restore the default tags) and formDisabledCallback (disable the inner input and remove buttons). Test with <fieldset disabled>.

Level 4: Patterns and tooling

Exercise 8: lazy definition. Write a page with <x-heavy-chart> below the fold. Use IntersectionObserver to define it only when it nears the viewport, reserve its space with :not(:defined), and handle a property assigned before upgrade.

Exercise 9: SSR a component. Write the HTML a server would emit for Exercise 3 using <template shadowrootmode="open">, then hydrate it without calling attachShadow a second time. See declarative shadow DOM.

Exercise 10: test it. Using the harness idea from testing web components, write five tests for the stepper: default value, clamping, event detail, attribute reflection, and disabled buttons at limits.

Capstone: a mini design system

Build a page showing four components from this track working together: a themed <x-button> (variants through attributes, parts, focus ring), <x-callout>, <x-tabs> from the tabs project, and the toasts from the toast project. Requirements:

  1. One set of design tokens (--color-primary, --space-*, --radius) drives all four, with a dark-mode toggle that changes only variables.
  2. Everything works with keyboard only, and each component has its public API (attributes, properties, events, slots, parts, variables) written in a short comment block at the top of its class.
  3. A reusable stylesheet is shared across instances.
  4. A short test list (as plain assertions) covers the main behaviors.

Self-review checklist

Keyboard Tab, arrows, Esc
Semantics Role, name, state
Theming Variables and parts
Tests Behavior, not internals

Check all four before calling a component done

1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Exercise 1, where does dispatching value-change guarantee coverage of every kind of change?

    Buttons, the property and direct attribute edits all end in an attribute change, so one dispatch point covers them.
  2. 2. Which tool lets 100 component instances share one parsed stylesheet?

    A single CSSStyleSheet adopted by many shadow roots is parsed once.
  3. 3. What is the best way to make a form-associated tag input invalid when empty and required?

    setValidity plugs the control into the browser's constraint validation so :invalid and checkValidity work.
  4. 4. What should a self-review of any finished component always include?

    A component is done when it works for keyboards and assistive technology, can be themed, and has tests for its behavior.

Summary

  • Practice in layers: properties and events, shadow DOM and styling, forms and accessibility, then patterns.
  • Break your components on purpose: bad values, keyboard-only use, disabled forms, late definition.
  • A finished component has a documented API, keyboard and screen reader support, themeability and tests.
  • The capstone combines the whole course into a small design system.

Next: Web Components tutorial home