Something went wrong. Try again.
Accessible, customizable toast notifications for modern web apps. toast-queue.js.org
Something went wrong. Try again.
toast-queue index.html
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <link rel="icon" href="data:image/svg+xml,%3Csvg width%3D%22512%22 height%3D%22512%22 viewBox%3D%220 0 512 512%22 fill%3D%22none%22 xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Cstyle%3E path%7Bcolor%3Ablack%7D%40media (prefers-color-scheme%3Adark)%7Bpath%7Bcolor%3Awhite%7D%7D%3C%2Fstyle%3E%3Crect width%3D%22512%22 height%3D%22512%22 rx%3D%22120%22 fill%3D%22url(%23paint0_linear_2_2)%22 fill-opacity%3D%220.3%22%2F%3E%3Cpath d%3D%22M304.877 355.467C311.87 381.538 296.382 408.336 270.283 415.323C244.183 422.308 217.355 406.836 210.362 380.764M247.806 155.519C252.767 146.603 254.402 135.827 251.551 125.196C245.722 103.47 223.367 90.5763 201.617 96.3979C179.867 102.219 166.96 124.551 172.787 146.277C175.639 156.908 182.448 165.424 191.208 170.668M334.964 215.866C329.144 194.165 313.602 176.018 291.762 165.417C269.92 154.816 243.568 152.63 218.5 159.339C193.433 166.049 171.705 181.104 158.096 201.193C144.487 221.283 140.112 244.76 145.933 266.462C155.565 302.366 153.694 331.011 147.612 352.777C140.68 377.586 137.214 389.99 138.151 392.48C139.222 395.33 139.997 396.113 142.838 397.213C145.322 398.174 155.755 395.38 176.622 389.796L370.122 338.005C390.988 332.419 401.422 329.627 403.092 327.553C405.002 325.181 405.281 324.117 404.784 321.115C404.348 318.489 395.137 309.479 376.715 291.456C360.554 275.643 344.596 251.771 334.964 215.866Z%22 stroke%3D%22currentColor%22 stroke-width%3D%2233%22 stroke-linecap%3D%22round%22 stroke-linejoin%3D%22round%22%2F%3E%3Cdefs%3E%3ClinearGradient id%3D%22paint0_linear_2_2%22 x1%3D%22256%22 y1%3D%220%22 x2%3D%22256%22 y2%3D%22512%22 gradientUnits%3D%22userSpaceOnUse%22%3E%3Cstop stop-color%3D%22%23666666%22%2F%3E%3Cstop offset%3D%221%22%2F%3E%3C%2FlinearGradient%3E%3C%2Fdefs%3E%3C%2Fsvg%3E" sizes="any" > <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>toast-queue</title> <style> body { margin: unset; min-height: 100dvh; place-content: center; justify-items: center; }
* { box-sizing: border-box; }
main { display: grid; grid-column: auto; gap: 1rem; }
button { touch-action: manipulation; user-select: none; -webkit-user-select: none; }
[id="demo-modal-dialog"], [id="demo-popover"] { width: min(100% - 2rem, 32rem); max-block-size: 85vh; min-block-size: 50vh; border-radius: 1rem; }
/* toast-queue customization */ toast-queue { --toast-color: Canvas;
width: 300px;
&:popover-open { display: flex; flex-direction: column; gap: 0.5rem; } }
toast-queue [data-part="group"] { gap: 0.6rem; }
toast-queue [data-part="item"]:is([data-dragging]) { opacity: calc(1 - var(--tq-swipe-distance, 0)); }
toast-queue [data-part="toast"] { color: hsl(from var(--toast-color) h s 15%); border: 1px solid var(--toast-color); background: hsl(from var(--toast-color) h s 90%); box-shadow: 0 12px 12px -8px hsl(from var(--toast-color) h s l / 0.325); border-radius: 1rem; padding: 1rem; gap: 0.5rem; }
toast-queue [data-part="content"] { align-items: flex-start; flex: 1; gap: 0.3rem; }
toast-queue [data-part="actions"] { align-self: center; }
toast-queue [data-part="close-button"] { display: flex; align-items: center; justify-content: center; width: 2rem; aspect-ratio: 1; border-radius: 100%; background-color: unset; border: unset; align-self: center; font-size: 1.4rem;
&:hover { background-color: var(--toast-color); } }
toast-queue [data-part="menu"] { display: flex; justify-content: flex-end; margin: unset; }
toast-queue:not([data-active]) [data-part="menu"] { display: none; }
[data-controls] { display: grid; gap: 0.5rem; }
[data-controls] [data-row] { display: grid; grid-template-columns: 5rem 1fr 3rem; gap: 0.5rem; align-items: center; }
[data-controls] output { text-align: end; font-variant-numeric: tabular-nums; }
[data-actions] { display: flex; gap: 0.5rem; flex-wrap: wrap; } </style> </head> <body> <main> <div data-controls> <label> Preset <select id="toastPresetMenu" aria-label="Toast preset"> <option value="list">list</option> <option value="stacked">stacked</option> </select> </label>
<label> Position <select id="toastPositionMenu" aria-label="Toast position"> <option value="top-start">top start</option> <option value="top-center">top center</option> <option value="top-end">top end</option> <option value="bottom-start">bottom start</option> <option value="bottom-center">bottom center</option> <option value="bottom-end">bottom end</option> </select> </label>
<div data-row> <label for="toastOffsetInline">inline</label> <input id="toastOffsetInline" type="range" min="0" max="64" step="1" value="16"> <output id="toastOffsetInlineValue">16px</output> </div>
<div data-row> <label for="toastOffsetBlock">block</label> <input id="toastOffsetBlock" type="range" min="0" max="64" step="1" value="16"> <output id="toastOffsetBlockValue">16px</output> </div> </div>
<div data-actions> <button type="button" data-action="add-toast">Add toast</button> <button type="button" data-action="add-burst">Add 5</button> <button type="button" data-action="add-long">Long</button> <button type="button" data-action="add-unbroken">Unbroken</button> <button type="button" data-action="add-no-dismiss">No dismiss</button> <button type="button" data-action="add-action">Action</button> </div>
<div data-actions> <button type="button" commandfor="demo-modal-dialog" command="show-modal">Modal dialog top layer change</button> <button type="button" popovertarget="demo-popover">Popover top layer change</button> </div>
<dialog id="demo-modal-dialog"> <h1>Modal dialog</h1> <button type="button" data-action="add-toast">Add toast</button> <button type="button" commandfor="demo-nested-dialog" command="show-modal">Open nested dialog</button> <button type="button" commandfor="demo-modal-dialog" command="close">Close dialog</button>
<dialog id="demo-nested-dialog"> <h1>Nested dialog</h1> <button type="button" data-action="add-toast">Add toast</button> <button type="button" commandfor="demo-nested-dialog" command="close">Close dialog</button> </dialog> </dialog>
<div id="demo-popover" popover> <h1>Popover</h1> <button type="button" data-action="add-toast">Add toast</button> <button type="button" popovertarget="demo-popover" popovertargetaction="hide">Hide popover</button> </div> </main>
<script type="module"> import { ToastQueue } from './src/index.js';
import listPreset from './src/presets/list.css?url'; import stackedPreset from './src/presets/stacked.css?url';
if (typeof HTMLElement.prototype.ariaNotify !== 'function') { await import('https://cdn.jsdelivr.net/npm/@github/arianotify-polyfill'); }
if (!(typeof HTMLButtonElement !== 'undefined' && 'command' in HTMLButtonElement.prototype && 'source' in (globalThis.CommandEvent?.prototype || {}))) { await import('https://cdn.jsdelivr.net/npm/invokers-polyfill@latest/+esm'); }
const presetUrls = { list: listPreset, stacked: stackedPreset, };
const toastPreset = localStorage.getItem('tq-preset') || 'list'; const toastPosition = localStorage.getItem('tq-position') || 'top-end'; const toastOffsetInline = localStorage.getItem('tq-offset-inline') || '16'; const toastOffsetBlock = localStorage.getItem('tq-offset-block') || '16';
const toastPresetMenu = document.querySelector('#toastPresetMenu'); const toastPositionMenu = document.querySelector('#toastPositionMenu'); const toastOffsetInlineInput = document.querySelector('#toastOffsetInline'); const toastOffsetInlineValue = document.querySelector('#toastOffsetInlineValue'); const toastOffsetBlockInput = document.querySelector('#toastOffsetBlock'); const toastOffsetBlockValue = document.querySelector('#toastOffsetBlockValue');
const presetLink = document.createElement('link'); presetLink.rel = 'stylesheet'; presetLink.href = presetUrls[toastPreset]; document.head.appendChild(presetLink);
window.tq = new ToastQueue({ root: document.querySelector('main'), position: toastPosition, template: { root: `<toast-queue> <menu data-part="menu"> <button type="button" data-command="clear">Clear all</button> </menu> <ol data-part="group"></ol> </toast-queue>`, }, });
window.tq2 = new ToastQueue({ duration: 0, position: 'top-start', });
const toastQueue = tq.element;
toastQueue.addEventListener('toast-add', ({ detail }) => { console.log('[toast-queue] add', { id: detail.toast.id }); });
toastQueue.addEventListener('toast-close', ({ detail }) => { console.log('[toast-queue] close', { id: detail.toast.id, reason: detail.reason, }); });
toastQueue.addEventListener('toast-action', ({ detail }) => { console.log('[toast-queue] action', { id: detail.toast.id }); });
toastQueue.addEventListener('activate', ({ detail }) => { console.log('[toast-queue] activate', detail.reason); });
toastQueue.addEventListener('deactivate', ({ detail }) => { console.log('[toast-queue] deactivate', detail.reason); });
toastQueue.addEventListener('pause', () => { console.log('[toast-queue] pause'); });
toastQueue.addEventListener('resume', () => { console.log('[toast-queue] resume'); });
toastQueue.style.setProperty('--tq-offset-inline', `${toastOffsetInline}px`);
toastQueue.style.setProperty('--tq-offset-block', `${toastOffsetBlock}px`);
const setToastPosition = (value) => { tq.position = value; localStorage.setItem('tq-position', value); };
const setToastPreset = (value) => { presetLink.href = presetUrls[value]; localStorage.setItem('tq-preset', value); };
const setToastOffset = (axis, value) => { toastQueue.style.setProperty(`--tq-offset-${axis}`, `${value}px`);
localStorage.setItem(`tq-offset-${axis}`, value); };
window.setToastPosition = setToastPosition; window.setToastPreset = setToastPreset; window.setToastOffset = setToastOffset;
toastPresetMenu.value = toastPreset; toastPositionMenu.value = toastPosition;
toastOffsetInlineInput.value = toastOffsetInline; toastOffsetBlockInput.value = toastOffsetBlock;
toastOffsetInlineValue.value = `${toastOffsetInline}px`; toastOffsetBlockValue.value = `${toastOffsetBlock}px`;
toastPresetMenu.addEventListener('change', (event) => { setToastPreset(event.target.value); });
toastPositionMenu.addEventListener('change', (event) => { const selectedValue = event.target.value; setToastPosition(selectedValue); });
toastOffsetInlineInput.addEventListener('input', (event) => { const value = event.target.value; toastOffsetInlineValue.value = `${value}px`; setToastOffset('inline', value); });
toastOffsetBlockInput.addEventListener('input', (event) => { const value = event.target.value; toastOffsetBlockValue.value = `${value}px`; setToastOffset('block', value); });
const addToast = (content, options = {}) => { tq.add(content, { ...options, }); };
document.addEventListener('click', (event) => { const action = event.target.closest('[data-action]')?.dataset.action;
if (!action) return;
if (action === 'add-toast') { addToast( { title: 'Title', description: Date.now(), }, { icon: 'ℹ️', className: 'my-toast', action: { label: 'Action', onClick: (toast) => { console.log(toast); tq.close(toast.id); }, }, }, ); return; }
if (action === 'add-burst') { for (let index = 1; index <= 5; index++) { addToast({ title: `Toast ${index}`, description: Date.now(), }); } return; }
if (action === 'add-long') { addToast({ title: 'A deliberately long toast title that should wrap', description: 'This description is intentionally long so we can test wrapping, queue sizing, animation and how the toast behaves when there is substantially more content than usual.', }); return; }
if (action === 'add-unbroken') { addToast({ title: 'Unbroken content', description: 'https://example.com/really-long-path/with-a-very-long-unbroken-string-that-should-not-break-the-queue-layout', }); return; }
if (action === 'add-no-dismiss') { addToast( { title: 'Persistent toast', description: 'This toast cannot be dismissed by swipe or close.', }, { dismissible: false, }, ); return; }
if (action === 'add-action') { addToast( { title: 'Action toast', description: 'Test focus, keyboard and action handling.', }, { icon: '⚡', action: { label: 'Run', onClick: (toast) => { console.log('Action clicked', toast); }, }, }, ); } }); </script> </body></html>