Toast notifications that slide in and pause
A small message that slides up from the corner, says what just happened, and leaves on its own. Press a button to make one.
UI loop
Toasts that slide in
Everything in the preview is live. Try it.
The code
Toast notifications
Press either button. Toasts slide up, stack to three at most, wait while you hover or focus them, and leave after five seconds.
<div class="tst-demo">
<button class="tst-btn" type="button" data-toast="Changes saved">Save changes</button>
<button class="tst-btn tst-btn--line" type="button" data-toast="Link copied">Copy link</button>
</div>
<section class="tst-stack" aria-label="Notifications" data-toasts></section>
<p class="tst-hidden" role="status" data-toast-status></p>
.tst-demo { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.tst-btn {
min-height: 44px;
padding: 0 18px;
border: 0;
border-radius: 12px;
background: #17202C;
color: #FDF9F1;
font: inherit;
font-weight: 700;
cursor: pointer;
}
.tst-btn--line { background: #FFFFFF; color: #17202C; box-shadow: inset 0 0 0 2px #17202C; }
.tst-btn:focus-visible { outline: 3px solid #E8631A; outline-offset: 3px; }
.tst-stack {
position: fixed;
z-index: 50;
right: 16px;
bottom: 16px;
left: 16px;
display: grid;
justify-items: end;
gap: 8px;
pointer-events: none;
}
.tst {
display: flex;
align-items: center;
gap: 10px;
width: min(100%, 340px);
box-sizing: border-box;
padding: 4px 4px 4px 14px;
border-radius: 14px;
background: #17202C;
color: #FDF9F1;
font-weight: 600;
box-shadow: 0 14px 30px -12px rgba(23, 32, 44, .55);
pointer-events: auto;
animation: tst-in .3s cubic-bezier(.33, 1, .68, 1);
}
.tst svg { flex: none; width: 20px; height: 20px; fill: none; stroke: #7CE0A3; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.tst p { flex: 1; margin: 0; }
.tst-x {
flex: none;
display: grid;
place-items: center;
width: 44px;
height: 44px;
border: 0;
border-radius: 10px;
background: transparent;
color: inherit;
cursor: pointer;
}
.tst-x svg { stroke: currentColor; }
.tst-x:hover { background: rgba(253, 249, 241, .12); }
.tst-x:focus-visible { outline: 3px solid #FFB23F; outline-offset: -3px; }
.tst.is-leaving { animation: tst-out .2s cubic-bezier(.32, 0, .67, 0) forwards; }
.tst-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
@keyframes tst-in { from { opacity: 0; transform: translateY(16px) scale(.96); } }
@keyframes tst-out { to { opacity: 0; transform: translateY(8px) scale(.96); } }
@media (prefers-reduced-motion: reduce) {
.tst, .tst.is-leaving { animation-duration: 1ms; }
}
const stack = document.querySelector('[data-toasts]');
const said = document.querySelector('[data-toast-status]');
const TIME = 5000; // on screen for 5 seconds; hovering or focusing a toast stops the clock
const MAX = 3;
const TICK = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12.5l4.5 4.5L19 7.5"/></svg>';
const CROSS = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M7 7l10 10M17 7L7 17"/></svg>';
function toast(text, opener) {
const el = document.createElement('div');
el.className = 'tst';
el.innerHTML = TICK + '<p></p><button class="tst-x" type="button" aria-label="Dismiss">' + CROSS + '</button>';
el.querySelector('p').textContent = text;
stack.append(el);
while (stack.children.length > MAX) stack.firstElementChild.remove();
// the live region says it once; clearing it first means the same words are read again
said.textContent = '';
setTimeout(() => { said.textContent = text; }, 50);
let left = TIME;
let start = 0;
let timer = null;
let hover = false;
let focus = false;
const update = () => {
const stop = hover || focus;
if (stop && timer) { clearTimeout(timer); timer = null; left -= Date.now() - start; }
if (!stop && !timer) { start = Date.now(); timer = setTimeout(close, left); }
};
function close() {
clearTimeout(timer);
if (el.contains(document.activeElement) && opener) opener.focus();
el.classList.add('is-leaving');
el.addEventListener('animationend', () => el.remove(), { once: true });
}
el.addEventListener('pointerenter', () => { hover = true; update(); });
el.addEventListener('pointerleave', () => { hover = false; update(); });
el.addEventListener('focusin', () => { focus = true; update(); });
el.addEventListener('focusout', () => { focus = false; update(); });
el.querySelector('.tst-x').addEventListener('click', close);
update();
}
document.querySelectorAll('[data-toast]').forEach((btn) => {
btn.addEventListener('click', () => toast(btn.dataset.toast, btn));
});
How to use it
- The toasts sit in a plain section, and a separate hidden line with role="status" reads each message once. Putting the toasts themselves in a live region would make a screen reader read the Dismiss button every time too.
- Each toast waits while the pointer is over it or something inside it has focus, and only counts down again when both have left.
- Arriving uses an ease out curve over 300 milliseconds and leaving an ease in over 200, so new messages land softly and old ones get out of the way.
- Closing a toast with the keyboard puts focus back on the button that opened it, so nobody is dropped at the top of the page.
When to use it
- Confirming something the person just did, where the result is also visible on the page: saved, copied, sent, moved to the bin.
- Background events that are nice to know about but need no answer, like "Your export is ready" with the file also listed in Downloads.
- Not for errors that need fixing, and not for the only copy of important information. WCAG's own example of a fine toast is one whose news can also be found another way, so put errors next to the thing that failed and keep them until they are fixed.
A worked example: a toast after saving settings
A settings form that saves without reloading and confirms with a toast, while the form itself shows what was saved.
- Put the toast section and the hidden status line once, near the end of the body, on every page that may need them.
- After the save request succeeds, call toast('Settings saved', saveButton). The second argument is where focus returns if someone dismisses it with the keyboard.
- Update the form too, for example a "Last saved 10:42" line beside the button, so the news is not only in a message that disappears after five seconds.
- If the save fails, show the error inside the form instead and leave it there until the next try.
Showing an interaction like this to a client or in a tutorial? Framok records your screen and zooms in on every click for you.
Source: W3C, Understanding Success Criterion 2.2.1: Timing Adjustable, which uses a toast as its example: a message that disappears is fine when the same information can be found elsewhere.
Free to use in personal and commercial work. No credit needed, though a link back is always nice.