Toaster Examples
The examples below assume that you have imported the component and set up the theme.
Basic
Show a toast from JavaScript. create() adds the toast, severity picks its icon and color, and swipe-dismiss lets the user swipe it away.
<div class="toaster-basic"> <button type="button" class="btn" data-severity="info">Info</button> <button type="button" class="btn" data-severity="success">Success</button> <button type="button" class="btn" data-severity="warning">Warning</button> <button type="button" class="btn" data-severity="error">Error</button></div>
<sv-toaster id="basic-toaster" swipe-dismiss></sv-toaster>
<script type="application/x-typescript" module> const toaster = document.querySelector<Toaster>("#basic-toaster")!;
const messages = { info: { title: "New update available", description: "A newer version of this application is ready to install.", }, success: { title: "Changes saved", description: "Your settings have been updated." }, warning: { title: "Unsaved changes", description: "Leaving this page will discard your edits.", }, error: { title: "Action failed", description: "Something went wrong while saving. Please try again.", }, };
let count = 0;
for (const button of document.querySelectorAll<HTMLButtonElement>(".toaster-basic button")) { button.addEventListener("click", () => { const severity = button.dataset.severity as keyof typeof messages | undefined; if (!severity) return;
count += 1; toaster.create({ severity, ...messages[severity], title: `${messages[severity].title} #${count}`, }); }); }</script>
<style> .toaster-basic { display: flex; flex-wrap: wrap; gap: 0.75em; place-content: center;
button[data-severity="info"] { color: var(--sv-info); }
button[data-severity="success"] { color: var(--sv-success); }
button[data-severity="warning"] { color: var(--sv-warning); }
button[data-severity="error"] { color: var(--sv-error); } }</style>Auto Dismiss
Remove a toast automatically after a delay. duration sets the delay in milliseconds.
<div class="toaster-auto-dismiss"> <button type="button" class="btn">Show toast for 4 seconds</button></div>
<sv-toaster id="auto-dismiss-toaster"></sv-toaster>
<script type="application/x-typescript" module> const toaster = document.querySelector<Toaster>("#auto-dismiss-toaster")!;
let count = 0;
document.querySelector(".toaster-auto-dismiss button")?.addEventListener("click", () => { count += 1;
toaster.create({ severity: "success", title: `Copied to clipboard #${count}`, description: "This toast dismisses itself after 4 seconds.", duration: 4000, }); });</script>
<style> .toaster-auto-dismiss { display: flex; justify-content: center; }</style>Action Button
Add your own action to a toast, or remove its close button. actionButton sets the label and click handler, closeButton: false drops the close button, and the function returned by create() removes the toast.
<div class="toaster-action-button"> <button id="show-custom-action" type="button" class="btn">Custom action</button> <button id="show-no-close" type="button" class="btn">No close button</button></div>
<sv-toaster id="action-button-toaster"></sv-toaster>
<script type="application/x-typescript" module> const toaster = document.querySelector<Toaster>("#action-button-toaster")!;
let count = 0;
document.querySelector("#show-custom-action")?.addEventListener("click", () => { count += 1;
const dismiss = toaster.create({ severity: "info", title: `Request #${count} has been processed`, actionButton: { label: "Learn more", action: () => { // window.location.href = "https://example.com"; dismiss(); }, }, }); });
document.querySelector("#show-no-close")?.addEventListener("click", () => { count += 1;
toaster.create({ severity: "success", title: `Report #${count} generated`, description: "There is no button here, so the toast leaves on its own.", closeButton: false, duration: 4000, }); });</script>
<style> .toaster-action-button { display: flex; flex-wrap: wrap; gap: 0.75em; justify-content: center; }</style>Custom Icon
Replace the default toast icon with your own element. iconOverride receives the severity and returns the icon, and a part attribute on the icon lets your CSS style it.
<div class="toaster-custom-icon"> <button id="show-custom-success" type="button" class="btn">Success</button> <button id="show-custom-error" type="button" class="btn">Error</button></div>
<template id="custom-icon-template"> <!-- Success --> <svg class="success" part="icon icon-success" viewBox="0 -960 960 960" xmlns="http://www.w3.org/2000/svg" > <path d="m363-310 117-71 117 71-31-133 104-90-137-11-53-126-53 126-137 11 104 90-31 133ZM480-28 346-160H160v-186L28-480l132-134v-186h186l134-132 134 132h186v186l132 134-132 134v186H614L480-28Zm0-112 100-100h140v-140l100-100-100-100v-140H580L480-820 380-720H240v140L140-480l100 100v140h140l100 100Zm0-340Z" /> </svg>
<!-- Error --> <svg class="error" part="icon icon-error" viewBox="0 -960 960 960" xmlns="http://www.w3.org/2000/svg" > <path d="m443-362 74-101 73 101 130-177-64-47-66 89-74-101-73 101-74-101-129 177 64 47 65-89 74 101Zm37 282q-83 0-156-31.5T197-197q-54-54-85.5-127T80-480q0-83 31.5-156T197-763q54-54 127-85.5T480-880q83 0 156 31.5T763-763q54 54 85.5 127T880-480q0 83-31.5 156T763-197q-54 54-127 85.5T480-80Zm0-80q134 0 227-93t93-227q0-134-93-227t-227-93q-134 0-227 93t-93 227q0 134 93 227t227 93Zm0-320Z" /> </svg></template>
<sv-toaster id="custom-icon-toaster"></sv-toaster>
<script type="application/x-typescript" module> import type { Severity } from "@staticview/ui/toaster";
const toaster = document.querySelector<Toaster>("#custom-icon-toaster")!; const template = document.querySelector<HTMLTemplateElement>("#custom-icon-template")!;
const customIcon = (severity: Severity) => { return template.content.querySelector(`.${severity}`)?.cloneNode(true) as HTMLElement; };
let count = 0;
document.querySelector("#show-custom-success")?.addEventListener("click", () => { count += 1;
toaster.create({ severity: "success", title: `New update available #${count}`, description: "A newer version of this application is ready to install.", iconOverride: customIcon, }); });
document.querySelector("#show-custom-error")?.addEventListener("click", () => { count += 1;
toaster.create({ severity: "error", title: `Action failed #${count}`, description: "Something went wrong while saving. Please try again.", iconOverride: customIcon, }); });</script>
<style> #custom-icon-toaster::part(icon) { inline-size: 2em; block-size: 2em; }
#custom-icon-toaster::part(icon-success) { fill: var(--sv-success); }
#custom-icon-toaster::part(icon-error) { fill: var(--sv-error); }
.toaster-custom-icon { display: flex; flex-wrap: wrap; gap: 0.75em; justify-content: center; }</style>Limits
Limit how many toasts the toaster keeps and shows. max-items sets how many toasts stay before the oldest one goes, and max-expanded-items sets how many show when the stack expands.
<div class="toaster-limits"> <button type="button" class="btn">Add toast</button></div>
<sv-toaster id="limits-toaster" max-expanded-items="2" max-items="4"></sv-toaster>
<script type="application/x-typescript" module> const toaster = document.querySelector<Toaster>("#limits-toaster")!;
let count = 0;
document.querySelector(".toaster-limits button")?.addEventListener("click", () => { count += 1;
toaster.create({ severity: "info", title: `Toast #${count}`, description: "The oldest toast is removed once the toaster holds four.", }); });</script>
<style> .toaster-limits { display: grid; place-content: center; }</style>Keyboard Shortcut
Reach the toaster from the keyboard with a global shortcut. shortcut="Alt+A" focuses the newest toast and expands the stack, Tab moves through the toasts, and Escape collapses them and returns the focus.
<div class="toaster-shortcut"> <button type="button" class="btn">Show three toasts</button></div>
<sv-toaster id="shortcut-toaster" shortcut="Alt+A"></sv-toaster>
<script type="application/x-typescript" module> const toaster = document.querySelector<Toaster>("#shortcut-toaster")!;
let build = 0;
document.querySelector(".toaster-shortcut button")?.addEventListener("click", () => { build += 1;
toaster.create({ severity: "info", title: `Build #${build} queued`, description: "Your build is waiting for a runner.", }); toaster.create({ severity: "success", title: `Build #${build} tests passed`, description: "All 128 tests finished without errors.", }); toaster.create({ severity: "warning", title: `Build #${build} slow network`, description: "Uploading artifacts is taking longer than usual.", }); });</script>
<style> .toaster-shortcut { display: flex; justify-content: center; }</style>