Zubra UI
Code for
GitHub

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

Compiled (any project)
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
One file, no dependencies (5.2 KB gzipped)
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
.zb source (to edit it with Zubra)
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
No build: a script tag, straight from this site
HTML
<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

index.html
<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>
Example.jsx
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>
    </>
  );
}
Example.vue
<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('-') } } }) -->
Example.svelte
<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>
example.component.ts
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

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
position"top-start" | "top-center" | "top-end" | "bottom-start" | "bottom-center" | "bottom-end""bottom-end"
durationnumber5000
maxnumber3
labelstring"Notifications"
Events
EventWhen
toastcloseA 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.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour