Skip to the main content

Toast Notifications

FreeUses XON Feedback

A stack of short messages in a polite live region: info, success and error toasts that dismiss themselves, pause on hover and clear with Escape.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<section class="tst" aria-labelledby="tst-title" data-on-keydown="tstClear" data-key="Escape">
  <h2 id="tst-title" class="tst__title">Toast notifications</h2>
  <p class="tst__lead">Hover or focus a toast to pause it. Escape clears them all.</p>

  <div class="tst__controls">
    <button type="button" class="tst__btn tst__btn--info" id="tst-add-info" data-on-click="tstAdd" data-kind="info">Show info</button>
    <button type="button" class="tst__btn tst__btn--success" id="tst-add-success" data-on-click="tstAdd" data-kind="success">Show success</button>
    <button type="button" class="tst__btn tst__btn--error" id="tst-add-error" data-on-click="tstAdd" data-kind="error">Show error</button>
    <button type="button" class="tst__btn tst__btn--quiet" id="tst-clear" data-on-click="tstClear">Clear all</button>
  </div>

  <div class="tst__screen">
    <div class="tst__skel" aria-hidden="true"><span></span><span></span><span></span></div>
    <div class="tst__region" role="region" aria-label="Notifications">
      <ul class="tst__stack" id="tst-stack" aria-live="polite" data-each="tst.items" data-each-key="id">
        <li class="tst__toast tst__toast--{kind}" data-id="{id}" data-on-mouseenter="tstPause" data-on-mouseleave="tstResume" data-on-focusin="tstPause" data-on-focusout="tstResume">
          <svg class="tst__icon" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false">
            <circle cx="12" cy="12" r="11" fill="currentColor"/>
            <path class="tst__g tst__g--info" d="M12 11v6M12 7.2v.1" fill="none" stroke="#fff" stroke-width="2.4" stroke-linecap="round"/>
            <path class="tst__g tst__g--success" d="M7 12.5l3.2 3.2L17 9" fill="none" stroke="#fff" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/>
            <path class="tst__g tst__g--error" d="M12 7v6M12 16.8v.1" fill="none" stroke="#fff" stroke-width="2.4" stroke-linecap="round"/>
          </svg>
          <div class="tst__msg"><p class="tst__msg-title">{title}</p><p class="tst__msg-text">{text}</p></div>
          <button type="button" class="tst__close" aria-label="Dismiss: {title}" data-on-click="tstDismiss" data-id="{id}">
            <svg viewBox="0 0 20 20" width="16" height="16" aria-hidden="true" focusable="false"><path d="M5 5l10 10M15 5L5 15" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
          </button>
        </li>
      </ul>
    </div>
  </div>
  <p class="tst__option">
    <input class="tst__check" type="checkbox" id="tst-sticky" data-model="tst.sticky">
    <label for="tst-sticky">Keep new toasts until I dismiss them</label>
  </p>
  <p class="tst__empty" id="tst-empty" data-if="tst.items.length == 0">No toasts showing.</p>
</section>

CSS

.tst {
  --tst-surface: #ffffff;
  --tst-fg: #1b2032;
  --tst-muted: #4a5266;
  --tst-line: #d3d8e6;
  --tst-screen: #eef0f9;
  --tst-skel: #d9ddee;
  --tst-accent: #4338ca;
  --tst-on-accent: #ffffff;
  --tst-info: #4338ca;
  --tst-ok: #0f6b3f;
  --tst-err: #b42318;
  --tst-ring: #4338ca;
  box-sizing: border-box;
  width: min(100%, 36rem);
  padding: 1.25rem;
  color: var(--tst-fg);
  background: var(--tst-surface);
  border: 1px solid var(--tst-line);
  border-radius: 16px;
  box-shadow: 0 10px 30px -18px rgba(27, 32, 50, 0.4);
}
.tst *, .tst *::before, .tst *::after { box-sizing: border-box; }
.tst__title { margin: 0; font-size: 1.25rem; letter-spacing: -0.01em; }
.tst__lead { margin: 0.3rem 0 1rem; color: var(--tst-muted); font-size: 0.9375rem; line-height: 1.5; }
.tst__controls { margin-bottom: 1rem; display: flex; flex-wrap: wrap; gap: 0.5rem; }
.tst__btn {
  min-height: 44px;
  padding: 0.45rem 1rem;
  font: inherit;
  font-weight: 600;
  color: var(--tst-on-accent);
  background: var(--tst-info);
  border: 2px solid var(--tst-info);
  border-radius: 10px;
  cursor: pointer;
}
.tst__btn--success { background: var(--tst-ok); border-color: var(--tst-ok); }
.tst__btn--error { background: var(--tst-err); border-color: var(--tst-err); }
.tst__btn--quiet { color: var(--tst-fg); background: transparent; border-color: var(--tst-line); }
.tst__btn:hover { filter: brightness(1.1); }
.tst__btn:focus-visible, .tst__close:focus-visible, .tst__check:focus-visible { outline: 3px solid var(--tst-ring); outline-offset: 2px; }
.tst__option { display: flex; align-items: center; gap: 0.6rem; margin: 0.9rem 0 0; font-size: 0.9375rem; }
.tst__check { width: 22px; height: 22px; margin: 0; accent-color: var(--tst-accent); }

/* a small stand-in for the page the toasts sit on; in a real page the stack is position: fixed */
.tst__screen { position: relative; height: 14rem; overflow: hidden; background: var(--tst-screen); border: 1px solid var(--tst-line); border-radius: 12px; }
.tst__skel { display: grid; gap: 0.6rem; padding: 1rem; }
.tst__skel span { display: block; height: 0.7rem; background: var(--tst-skel); border-radius: 999px; }
.tst__skel span:nth-child(1) { width: 55%; } .tst__skel span:nth-child(2) { width: 80%; } .tst__skel span:nth-child(3) { width: 40%; }
.tst__region { position: absolute; right: 0.75rem; bottom: 0.75rem; left: 0.75rem; display: flex; justify-content: flex-end; }
.tst__stack { display: flex; flex-direction: column; gap: 0.5rem; width: min(100%, 21rem); margin: 0; padding: 0; list-style: none; }
.tst__toast {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: start;
  gap: 0.7rem;
  padding: 0.7rem 0.6rem 0.7rem 0.8rem;
  color: var(--tst-fg);
  background: var(--tst-surface);
  border: 1px solid var(--tst-line);
  border-left: 5px solid var(--tst-info);
  border-radius: 10px;
  box-shadow: 0 8px 18px -10px rgba(27, 32, 50, 0.55);
  animation: tst-in 0.25s ease both;
}
.tst__toast--success { border-left-color: var(--tst-ok); }
.tst__toast--error { border-left-color: var(--tst-err); }
.tst__icon { margin-top: 1px; color: var(--tst-info); }
.tst__toast--success .tst__icon { color: var(--tst-ok); }
.tst__toast--error .tst__icon { color: var(--tst-err); }
.tst__g { display: none; }
.tst__toast--info .tst__g--info, .tst__toast--success .tst__g--success, .tst__toast--error .tst__g--error { display: inline; }
.tst__msg { min-width: 0; }
.tst__msg p { margin: 0; }
.tst__msg-title { font-weight: 700; font-size: 0.9375rem; }
.tst__msg-text { color: var(--tst-muted); font-size: 0.8125rem; line-height: 1.4; }
.tst__close { display: grid; place-items: center; width: 32px; height: 32px; padding: 0; color: var(--tst-muted); background: transparent; border: 0; border-radius: 8px; cursor: pointer; }
.tst__close:hover { color: var(--tst-fg); background: var(--tst-screen); }
.tst__empty { margin: 0.6rem 0 0; color: var(--tst-muted); font-size: 0.8125rem; }
@media (max-width: 30em) { .tst { padding: 1rem; } .tst__screen { height: 17.5rem; } }
@keyframes tst-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (prefers-color-scheme: dark) {
  .tst {
    --tst-surface: #1a1e2e;
    --tst-fg: #edf0fa;
    --tst-muted: #b9bfd3;
    --tst-line: #3a4157;
    --tst-screen: #12141f;
    --tst-skel: #232840;
    --tst-accent: #aeb1ff;
    --tst-on-accent: #14162b;
    --tst-info: #aeb1ff;
    --tst-ok: #6ee7a8;
    --tst-err: #ffa59b;
    --tst-ring: #c9cbff;
    box-shadow: none;
  }
  .tst__toast { background: #232842; box-shadow: none; }
  .tst__g { stroke: #14162b; }
}
@media (prefers-reduced-motion: reduce) {
  .tst__toast { animation: none; }
}

JavaScript

// A toast is one object in a list; XON draws the list and keeps the rows in step with it.
// Timers live beside the list, keyed by the toast id, so hovering or focusing a toast can pause its timer.
var tst = xon.data('tst', { items: [], sticky: false, next: 1 });
var TST_DELAY = 5000;
var TST_MAX = 3;
var tstTimers = {};

var TST_COPY = {
  info: { title: 'Update available', text: 'Version 2 is ready.' },
  success: { title: 'Saved', text: 'Changes stored in this example.' },
  error: { title: 'Could not save', text: 'Sample error. Nothing was lost.' }
};

function tstStop(id) {
  var t = tstTimers[id];
  if (t && t.handle) { clearTimeout(t.handle); t.handle = 0; }
  return t;
}

function tstRemove(id) {
  tstStop(id);
  delete tstTimers[id];
  tst.items = tst.items.filter(function (item) { return item.id !== id; });
}

function tstStart(id, ms) {
  var t = tstTimers[id] || (tstTimers[id] = {});
  t.left = ms;
  t.at = Date.now();
  t.handle = setTimeout(function () { tstRemove(id); }, ms);
}

function tstAdd(event, button) {
  var kind = TST_COPY[button.dataset.kind] ? button.dataset.kind : 'info';
  var id = tst.next++;
  var items = tst.items.concat([{ id: id, kind: kind, title: TST_COPY[kind].title, text: TST_COPY[kind].text }]);
  while (items.length > TST_MAX) tstRemove(items.shift().id);
  tst.items = items;
  if (!tst.sticky) tstStart(id, TST_DELAY);
}

function tstDismiss(event, button) {
  tstRemove(Number(button.dataset.id));
}

function tstClear() {
  Object.keys(tstTimers).forEach(function (id) { tstStop(id); });
  tstTimers = {};
  tst.items = [];
}

function tstPause(event, row) {
  var t = tstStop(Number(row.dataset.id));
  if (t) t.left = Math.max(1000, t.left - (Date.now() - t.at));
}

function tstResume(event, row) {
  var id = Number(row.dataset.id), t = tstTimers[id];
  if (t && !t.handle && !row.contains(document.activeElement) && !row.matches(':hover')) tstStart(id, t.left);
}

xon.use(tstAdd, tstDismiss, tstClear);
xon.use.auto(tstPause, tstResume);

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • Toasts are added to a polite live region, so they are announced without taking focus.
  • Every toast has a close button named after its message.
  • A checkbox keeps toasts on screen until dismissed, so the timing can be switched off.
  • A toast pauses while it is hovered or holds focus.