Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 39 ยท Projects and Practice

Project: Toast Notifications

Build an accessible toast notification system as a web component with a live region, auto-dismiss, pause on hover, events, themeable parts and a static show() API.

Toasts are the small messages that appear in a corner ("Saved", "Connection lost") and disappear on their own. They look trivial but touch almost everything you have learned: custom elements, shadow DOM, slots, events, accessibility, CSS custom properties and parts. In this project you build a <x-toaster> container and <x-toast> messages, step by step.

Requirements

  1. A single <x-toaster> container, fixed in a corner, announced to screen readers through a live region.
  2. A show(message, options) method that creates toasts with a tone (info, success, error) and a duration.
  3. Auto-dismiss after the duration, paused while the pointer is over the toast or a child has focus.
  4. A close button that is keyboard accessible.
  5. Events: toast-show and toast-dismiss with the toast's details.
  6. Theming through CSS variables and a part for the message text.
flowchart LR
  A["page calls toaster.show()"] --> B["x-toast created and appended"]
  B --> C["live region announces text"]
  C --> D{"hovered or focused?"}
  D -- "no, timer ends" --> E["dismiss + event"]
  D -- "yes" --> F["pause timer"]
  F --> D
  C -- "close clicked" --> E

Lifecycle of one toast.

Step 1: the toast element

Each toast owns its own timer and its own look. It is created by the toaster, but it is also a standalone custom element.

Step 2: the toaster and the API

The toaster holds a live region (role="status", polite) that stays in the DOM, and offers show(). Errors use role="alert" so they interrupt, which is the right call only for real problems.

Toast notification system

Try it: hover a toast and it stays; move away and the remaining time resumes (not restarting from zero). Tab into the close button and the timer pauses until focus leaves.

How the pieces fit

  • Light DOM children, shadow presentation. The toaster slots its toasts into a live region. Because the toasts are slotted light DOM, x-toaster x-toast::part(message) from the page reaches the message part of each toast.
  • Pause and resume math. The timer stores remaining time. On pause it subtracts the elapsed time; on resume it starts a new timeout with what is left.
  • Events from the toast are dispatched from the host with bubbles: true, so a page-level document listener hears them even though the toast has a shadow root (composed is harmless here and keeps the event safe if you later dispatch from inside). See event retargeting.
  • Theming uses --toast-bg, --toast-fg, --toast-accent and --toast-radius, with fallbacks, exactly as in CSS custom properties for theming.

Improvements to try

  1. Respect prefers-reduced-motion and add an entrance animation otherwise.
  2. Limit the stack to three toasts and drop the oldest.
  3. Add an optional action button ("Undo") that dispatches toast-action.
  4. Let the toaster position be set with a position attribute (top-left, bottom-right).
  5. Allow dismissing the newest toast with the Escape key.
  6. Do not let two identical messages stack: refresh the existing toast's timer instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the toaster keep its live region in the DOM from the start?

    Assistive technology watches existing live regions for changes, so the container exists first and toasts are added inside.
  2. 2. How does the toast resume after hovering without restarting the full duration?

    On pause it computes remaining -= elapsed, and resume schedules a timeout with that remaining time.
  3. 3. Why can a document-level listener hear toast-dismiss, which is dispatched on the toast host?

    The host lives in the page's tree, so a bubbling event dispatched from it reaches document. composed: true only matters for events fired from inside the shadow root.
  4. 4. Which role is appropriate for an urgent error toast?

    role="alert" is announced assertively, which suits real errors; routine messages should use a polite status region.

Summary

  • A toast system combines a long-lived container (live region) and short-lived messages.
  • Pause timers on hover and focus, resuming with the remaining time.
  • Publish toast-show and toast-dismiss events, theme with variables and parts.
  • Use polite announcements normally and role="alert" only for urgent errors.

Next: Project: Accessible Tabs