Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 132 ยท Components and Props

Reusable Widgets: Toggle, Rating, Progress Bar, Breadcrumbs

Build accessible reusable React widgets - a toggle switch, a star rating, a progress bar, breadcrumbs, a scroll-aware navbar and a sticky footer layout - with the props, ARIA and keyboard rules each needs.

Every interface is assembled from a small set of recurring widgets: a switch that turns something on, a row of stars, a bar that shows progress, a trail of links that shows where you are. Writing them once, well, and reusing them is what turns a pile of components into a design system. "Well" has three parts: a clean API (what props), correct semantics (what a screen reader hears) and keyboard support (what happens without a mouse).

This lesson builds six widgets. For each one you will see the API decision, the accessibility rule and the common bug. They use the ideas from Component Design, controlled and uncontrolled components and accessibility.

Toggle switch

A switch changes a setting immediately (dark mode, notifications), unlike a checkbox, which usually waits for a form submit. The best semantic base is a real <button> with role="switch" and aria-checked: you get focus, Space and Enter handling and the right announcement ("Notifications, switch, on") for free.

API: a controlled component: checked, onChange(nextChecked), and a visible label. Controlled means the parent owns the value, so the same widget works with plain state, a form library or a server value.

Common bug: a <div onClick> styled as a switch. It cannot be focused, so keyboard users cannot use it.

Star rating

A rating has more interaction than it looks: hovering previews a value, clicking commits it, and the keyboard must be able to do the same. The accessible pattern is a radio group: the stars are role="radio" buttons inside a role="radiogroup", and the arrow keys change the value. A readOnly mode (for display) renders the same stars without interaction.

API: value, onChange, max = 5, readOnly, label.

Common bug: using only onMouseEnter, so touch and keyboard users never see the preview; and rating "0" being impossible to clear. Allow clicking the active star again to clear.

Progress bar

Two different things are called progress. A determinate bar knows the fraction (value of max), an indeterminate one only says "working". The semantic element is <progress value max>, or role="progressbar" with aria-valuenow, aria-valuemin and aria-valuemax on a styled div. Omit aria-valuenow for the indeterminate case. Always clamp: Math.min(max, Math.max(0, value)), because a progress value of 104% looks like a bug.

API: value, max = 100, label; no value means indeterminate.

Breadcrumbs show the path to the current page. The correct markup is a <nav aria-label="Breadcrumb"> containing an ordered list, with the last item marked aria-current="page" and not linked. The separators are decoration and should be created with CSS (or aria-hidden text) so screen readers do not read "slash slash slash".

API: an items array of { label, href }; the last item has no href.

Putting four of them to work

Switch, rating, progress bar and breadcrumbs

Press the +25 button until it passes 100: the bar and the percentage stop at 100% because of the clamp. Click the same star twice to clear a rating. The roving tabIndex makes the rating one tab stop, which is the keyboard pattern for radio groups.

Scroll-aware navbar

A navbar that changes with scroll (hiding on scroll down, showing on scroll up, or gaining a shadow once you leave the top) is a small lesson in scroll events and derived state. Two rules keep it cheap:

  1. Store a boolean, not the scroll position. scrolled = scrollTop > 8 changes rarely; scrollTop changes dozens of times per second and would re-render the navbar constantly.
  2. Use a passive listener, and clean it up. Passive means "I will not call preventDefault", which lets the browser keep scrolling smoothly.
JSX
// read-only: a hook that reports whether the page scrolled
function useScrolled(threshold = 8) {
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > threshold);
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, [threshold]);
  return scrolled;
}

setScrolled with an unchanged value does not re-render, so the listener is nearly free. The demo below scrolls inside a box instead of the page (the preview is an iframe), reading the same signal from the box's onScroll.

A "sticky footer" keeps the footer at the bottom of the viewport when the page is short, and pushes it below the content when the page is long. No JavaScript or measurements are needed: make the page a column flex container at least as tall as the screen, and let the main area grow.

CSS
.page { min-height: 100vh; display: flex; flex-direction: column; }
.page > main { flex: 1; }

In React this is just a layout component with header, main and footer slots (see children and composition).

A scroll-aware header and a sticky-footer layout

With two paragraphs the footer sits at the bottom of the layout box rather than right under the text. Add content and scroll: the header gains its shadow as soon as you leave the top, and the footer moves down with the content.

Designing widget APIs

The same questions apply to every widget you build:

  • Controlled or uncontrolled? Prefer controlled (value + onChange) for anything the parent cares about, and consider offering a defaultValue for the lazy case.
  • What is the smallest useful set of props? Add className and ...rest pass-through for flexibility, but resist boolean-flag explosions; use composition (see compound components).
  • Which native element is the base? Button, <progress>, <nav>, <ol>. Native semantics beat ARIA.
  • Where do labels come from? Every interactive widget needs an accessible name (visible text or aria-label).
  • What happens at the edges? Clamp values, handle empty lists, honor prefers-reduced-motion for the transitions.

Common mistakes

  • Clickable divs instead of buttons and links.
  • Missing keyboard support, for example hover-only previews.
  • Storing derived values in state (a percent next to value).
  • Separators read aloud in breadcrumbs because they are plain text.
  • Unclamped values that overflow the bar.
  • Re-rendering on every scroll pixel; store booleans and use passive listeners.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which markup is the best base for a toggle switch?

    A button gives keyboard focus and activation for free, and role switch with aria-checked announces the on or off state to assistive technology.
  2. 2. How should the current page be marked in breadcrumbs?

    aria-current identifies the current page for screen readers. Linking to the current page is redundant.
  3. 3. What should aria-valuenow be for an indeterminate progress bar?

    Without aria-valuenow, assistive technology understands that the progress is unknown (working), which is what indeterminate means.
  4. 4. Why store scrolled = scrollTop > 8 instead of the scroll position in state?

    Setting state to the same value does not re-render. A boolean flips only twice per scroll, instead of on every pixel.
  5. 5. How do you keep a footer at the bottom of a short page without JavaScript?

    Flex layout lets the main area grow into the free space, pushing the footer down, and it adapts when content is long.

Summary

  • Build widgets on native elements, add ARIA only where needed, and support the keyboard.
  • Make widgets controlled (value and onChange) so parents own the data.
  • Switch: button with role="switch". Rating: radio group with arrow keys. Progress: role="progressbar" with clamped values; omit aria-valuenow when unknown.
  • Breadcrumbs: nav plus an ordered list, aria-current="page" on the last item.
  • Scroll-aware UI should store a derived boolean and use passive listeners.
  • A sticky footer is a flex column with a growing main.

Next: Class Components and the Lifecycle