Liking cljdoc? Tell your friends :D

x-toaster

A fixed-position coordinator that manages a stack of x-toast notifications. Provides viewport placement, capacity management, and an imperative API for spawning toasts programmatically.

Tag

<x-toaster></x-toaster>

Usage

Declarative

Place x-toast elements as children of x-toaster. The toaster handles positioning, stacking, and coordinated dismissal.

<x-toaster position="top-end">
  <x-toast type="success" heading="Saved" message="Your file was saved."></x-toast>
</x-toaster>

Imperative (toast() method)

const toaster = document.querySelector('x-toaster');
toaster.toast({
  type: 'success',
  heading: 'File saved',
  message: 'Your changes have been saved.',
  timeoutMs: 4000,
  showProgress: true,
});

Observed Attributes

AttributeTypeDefaultDescription
positionenum"top-end"Viewport placement. See values below.
max-toastsnumber5Maximum visible toasts. Oldest is evicted when exceeded.
labelstring"Notifications"aria-label for the region landmark.

position values

ValuePlacement
top-startTop-left (logical inline-start)
top-centerTop, horizontally centred
top-endTop-right (logical inline-end) — default
bottom-startBottom-left
bottom-centerBottom, horizontally centred
bottom-endBottom-right

Properties

JS PropertyAttributeTypeNotes
positionpositionstringReflects attribute.
maxToastsmax-toastsnumberReflects attribute.
labellabelstringReflects attribute.

Public Method

toast(options) → HTMLElement

Creates an x-toast element with the given options, appends it to the toaster, and returns the element reference.

toaster.toast(options: {
  type?:        'info' | 'success' | 'warning' | 'error';
  heading?:     string;
  message?:     string;
  icon?:        string;
  dismissible?: boolean;   // default: true (x-toast default)
  timeoutMs?:   number;
  showProgress?: boolean;
}) → HTMLElement

The returned element is a live <x-toast> node. You can call .dismiss() on it directly to programmatically remove it.

Events

x-toaster-dismiss

Fired when a child toast is about to be dismissed. Provides a chance to observe or veto dismissals.

PropertyValue
bubblestrue
composedtrue
cancelabletrue

Detail:

FieldTypeNotes
typestringToast type (info, success, warning, error)
reasonstringDismissal reason ("button", "keyboard", "timeout", "api", etc.)
headingstringToast heading text
messagestringToast message text

Cancellation: Call event.preventDefault() to abort the dismissal. The toast stays visible.

toaster.addEventListener('x-toaster-dismiss', e => {
  if (e.detail.reason === 'timeout') {
    e.preventDefault(); // keep this toast alive despite timeout
  }
});

Shadow Parts

PartElementNotes
(none)Toaster shadow DOM contains only a <slot>. Use ::slotted(x-toast) to style children.

CSS Custom Properties

PropertyDefaultDescription
--x-toaster-inset16pxDistance from viewport edges
--x-toaster-gap8pxVertical gap between toasts
--x-toaster-max-width480pxMax width of the overlay container
--x-toaster-z-index9000Stack order above page content

Stacking order

  • Top positions (top-*): newest toast appears nearest the corner; older toasts are pushed toward the center.
  • Bottom positions (bottom-*): newest toast appears nearest the corner; older toasts are pushed toward the center.

This is achieved via flex-direction: column-reverse (top) and flex-direction: column (bottom), with toasts always appended as the last DOM child.

Accessibility

  • The x-toaster host has role="region" and aria-label (configurable via label).
  • Individual x-toast children carry their own role="status" or role="alert" announcements.
  • pointer-events: none on the host ensures non-toast areas of the overlay don't block interaction with underlying content.

Coordination with x-toast

x-toast is fully standalone. When placed inside x-toaster, the following protocol applies:

  1. x-toast fires x-toast-dismiss (bubbles, composed) when dismissed by button, keyboard, or timeout.
  2. x-toaster intercepts the event (reason ≠ "toaster-remove"), calls preventDefault(), and fires x-toaster-dismiss.
  3. If x-toaster-dismiss is not prevented, x-toaster calls toast.dismiss("toaster-remove").
  4. x-toast receives the second dismiss call, fires another x-toast-dismiss (reason: "toaster-remove"), which x-toaster does not intercept → x-toast animates exit and removes itself.

Examples

Bottom-center toaster with custom gap

<x-toaster position="bottom-center" style="--x-toaster-gap: 12px;">
</x-toaster>

<script>
  import '@vanelsas/baredom/x-toaster';
  import '@vanelsas/baredom/x-toast';

  const toaster = document.querySelector('x-toaster');
  toaster.toast({ type: 'info', message: 'Page loaded.' });
</script>

Preventing specific dismissals

toaster.addEventListener('x-toaster-dismiss', e => {
  if (e.detail.type === 'error') {
    e.preventDefault(); // errors must be manually dismissed
  }
});

Programmatically dismissing a specific toast

const ref = toaster.toast({ type: 'warning', message: 'Check your input.' });
// Later:
ref.dismiss('resolved');

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close