Skip to content

Toast (Status Message) ​

A toast is a transient status confirmation ("Saved", "Copied to clipboard") that appears following user interaction and dismisses automatically after a brief timeout.

Because no native HTML element exists for transient status messages, the most pervasive failure mode is timing: the notification unmounts from the DOM before assistive screen readers can announce it.

Live Demo ​

Click the trigger buttons repeatedly below to observe toast stacking behavior, where older notifications visually recede with a maximum stack limit of 3 items before automatic dismissal:

Keyboard Interaction ​

Keyboard operations and expected behavior
Key InteractionExpected Behavioral Contract
TabFocus the action trigger button; focus is never diverted to toast messages
Enter / SpaceTriggers the action and dispatches the toast notification while preserving focus on the button

Pointer Interactions ​

Pointer operations and expected behavior
Pointer ActionExpected Behavioral Contract
Click Trigger ButtonDispatches a toast notification into the designated display region

Rules ​

Place the toast live region in the DOM, empty, from page load MUSTUI-R42
Put an empty element with role="status" and aria-live="polite" in the page from the start and only swap its text when notifying. A region created together with the message may not be announced.
Check: TST-01TST-01 Empty live region✕ NGThe text may not be announcedThe live region is inserted together with its text when the notification fires✓ OKEmpty first, text added laterAn empty live region is in the page from load and the text is added later
Use polite for completion notices, and assertive or role="alert" only for urgent errors MUSTUI-R43
assertive interrupts whatever is being read. Keeping it for situations that need immediate action avoids stopping users for no reason.
Check: TST-02TST-02 Use polite for success✕ NGSuccess message interrupts speechAn ordinary success message uses aria-live="assertive"✓ OKAnnounced at the next pauseAn ordinary success message uses aria-live="polite"
Do not move focus to a toast when it appears MUST NOTUI-R44
A toast only informs. With focus left where it was, users carry on with their work.
Check: TST-03TST-03 Focus stays put✕ NGFocus jumps to the toastWhen the toast appears, focus moves from the button to the toast✓ OKFocus stays on the buttonWhen the toast appears, focus stays on the button
Do not report important results such as deletions or updates by toast alone MUST NOTUI-R45
Toasts disappear over time. Make the result visible on the page itself too, such as the item leaving the list or the new value being shown.
Check: TST-04TST-04 Not toast-only results✕ NGResult not reflected on screenAfter deleting File B it stays in the list, so only the toast shows the result✓ OKAlso gone from the listFile B is gone from the list, so the screen shows the result too
Set a limit on simultaneous toasts and how long they stay SHOULDUI-R46
Keep the limit to about 3–4 and drop the oldest beyond it. Repeated actions do not bury the screen in notices.
Check: TST-05TST-05 Cap and display time✕ NGToasts pile up without limitNo maximum count or display time is defined, so toasts pile up✓ OKState both in the specThe spec sets a maximum of 3 and 5 seconds, and at most 3 are shown
Pause the auto-dismiss timer while the pointer is over a toast SHOULDUI-R47
The notice does not vanish before it is read.
Check: TST-06TST-06 Pause on hover✕ NGDisappears under the pointerThe toast disappears when the time is up even though the pointer is over it✓ OKStays while hoveredWhile the pointer is over the toast, it stays past the display time

Implementation ​

A complete, production-grade module satisfying all requirements above:

View Source Code (HTML Markup and JavaScript Module)
html
<button type="button" id="save-btn">Save settings</button>
<button type="button" id="copy-btn">Copy link</button>

<!-- Live region pre-rendered empty in initial DOM markup -->
<div id="toast-region" role="status" aria-live="polite" aria-atomic="true"></div>
javascript
// modules/toast.js
export function showToast(region, message, { duration = 3000, max = 3 } = {}) {
  const toast = document.createElement('div')
  toast.className = 'toast-item'
  toast.textContent = message
  region.appendChild(toast)

  // Discard oldest toasts when exceeding maximum stack limit
  while (region.children.length > max) {
    region.firstElementChild.remove()
  }

  let timer = setTimeout(() => {
    if (region.contains(toast)) {
      toast.remove()
    }
  }, duration)

  // Pause dismissal countdown on hover
  toast.addEventListener('mouseenter', () => {
    clearTimeout(timer)
  })
  toast.addEventListener('mouseleave', () => {
    timer = setTimeout(() => {
      if (region.contains(toast)) {
        toast.remove()
      }
    }, 1500)
  })
}

// Execution example
const region = document.getElementById('toast-region')
document.getElementById('save-btn').addEventListener('click', () => {
  showToast(region, 'Settings saved successfully')
})
document.getElementById('copy-btn').addEventListener('click', () => {
  showToast(region, 'Link copied to clipboard')
})