toast-region
Short notifications that appear in a corner and go away by themselves. Put one region on the page; anything (a framework, a plain script) shows a toast by dispatching an event, so nothing needs a reference to it: document.dispatchEvent(new CustomEvent('toast', { detail: { message: 'Saved', variant: 'success' } })). detail takes message, heading, variant (info, success, warning, danger), duration in ms (0 keeps it until closed) and action: { label, href }. Toasts are announced politely (an error assertively), wait while hovered or focused, and sit above dialogs (the top layer).
Try it: events appear here (toastclose).
Source: toast-region.zb, and its registry item.
Install
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/toast-region.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/toast-region.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/toast-region.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/toast-region.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/toast-region.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/toast-region.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/toast-region.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/toast-region.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/toast-region.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/toast-region.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/toast-region.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/toast-region.json
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/toast-region.js"></script>The compiled install writes src/components/zubra/toast-region.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.
Usage
<button type="button" id="show-toast-region">Save</button>
<toast-region position="bottom-end"></toast-region>
<script type="module">
import './components/zubra/toast-region.js';
const element = document.querySelector('toast-region');
element.addEventListener('toastclose', (event) => console.log('toastclose', event.detail));
document.querySelector('#show-toast-region').addEventListener('click', () => {
document.dispatchEvent(new CustomEvent('toast', { detail: {
heading: 'Saved',
message: 'Your changes are live.',
variant: 'success',
} }));
});
</script>import './components/zubra/toast-region.js';
export function ToastRegionExample() {
return (
<>
<button type="button" onClick={() => document.dispatchEvent(new CustomEvent('toast', { detail: {
heading: 'Saved',
message: 'Your changes are live.',
variant: 'success',
} }))}>Save</button>
<toast-region
position="bottom-end"
ontoastclose={(event) => console.log('toastclose', event.detail)}
></toast-region>
</>
);
}<script setup>
import './components/zubra/toast-region.js';
const onToastclose = (event) => console.log('toastclose', event.detail);
const show = () => document.dispatchEvent(new CustomEvent('toast', { detail: {
heading: 'Saved',
message: 'Your changes are live.',
variant: 'success',
} }));
</script>
<template>
<button type="button" @click="show">Save</button>
<toast-region position="bottom-end" @toastclose="onToastclose"></toast-region>
</template>
<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) --><script>
import './components/zubra/toast-region.js';
const show = () => document.dispatchEvent(new CustomEvent('toast', { detail: {
heading: 'Saved',
message: 'Your changes are live.',
variant: 'success',
} }));
</script>
<button type="button" onclick={show}>Save</button>
<toast-region
position="bottom-end"
ontoastclose={(event) => console.log('toastclose', event.detail)}
></toast-region>import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/toast-region.js';
@Component({
selector: 'app-toast-region-example',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<button type="button" (click)="show()">Save</button>
<toast-region position="bottom-end" (toastclose)="onToastclose($event)"></toast-region>
`,
})
export class ToastRegionExample {
onToastclose(event: Event) {
console.log('toastclose', (event as CustomEvent).detail);
}
show() {
document.dispatchEvent(new CustomEvent('toast', { detail: {
heading: 'Saved',
message: 'Your changes are live.',
variant: 'success',
} }));
}
}API
| Property | Type | Default |
|---|---|---|
position | "top-start" | "top-center" | "top-end" | "bottom-start" | "bottom-center" | "bottom-end" | "bottom-end" |
duration | number | 5000 |
max | number | 3 |
label | string | "Notifications" |
| Event | When |
|---|---|
toastclose | A toast closed; detail is its message. |
Size
2.2 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 19.5 KB.