Skip to content

Feedback

Toast

Transient viewport notifications with imperative API.

import { Toast } from '@var-ui/react';

Toast

For live notification queues without React, mount a ToastRegion and call the imperative toast API from any client script. In React apps, use ToastProvider with useToast() or toast from @var-ui/react. The imperative toast() needs a mounted ToastProvider: without one (or during SSR) it does nothing, returns '', and warns once in development. Changing maxVisibleToasts keeps toasts already on screen.

Examples

Default

Demo.tsxTSX
import { Button, ToastProvider, toast, useToast } from '@var-ui/react';

function NotifyButton() {
  const { add } = useToast();
  return <Button onClick={() => add({ tone: 'success', title: 'Saved' })}>Save</Button>;
}

<ToastProvider>
  <NotifyButton />
</ToastProvider>

// Imperative API (requires ToastProvider mounted)
toast.show({ tone: 'info', title: 'Hello' });
toast.update(id, { title: 'Updated' });
toast.dismiss(id);

Placement and offset

placement is a corner (top-start, top-end, bottom-start, bottom-end) or an edge’s center (top-center, bottom-center), and follows the reading direction. offset moves the region off those edges, for example above a persistent bottom bar. A number is px. block is the top or bottom edge, and inline the start or end edge:

TSX
<ToastProvider placement="bottom-end" offset={{ block: 'var(--player-height, 0px)' }}>

The offsets are the recipe variables --var-ui-toast-offsetblock and --var-ui-toast-offsetinline. Their 0px default is a fallback where they’re read, not a declaration on the region, so plain CSS can set them anywhere above it, in a media query too: :root { --var-ui-toast-offsetblock: var(--player-height) }. className and style on ToastProvider reach the region.

Your own queue

To reach toasts from outside React (a store, a fetch wrapper), create the queue yourself. Either pass it to ToastProvider queue={…}, which keeps toast() and useToast() working, or render your own ToastRegion. createToaster(queue) gives you the same imperative API bound to that queue:

TS
import { createToaster, ToastQueue } from '@var-ui/react';

export const appToasts = new ToastQueue({ maxVisibleToasts: 3 });
export const notify = createToaster(appToasts);

notify({ title: 'Added to favorites', tone: 'success' });

Props

Top layer and modal dialogs

The region is a native popover="manual" element: it is shown while the queue has toasts and removed when the queue empties, with no z-index and no portal container option. The top layer stacks by insertion order, so when a modal <dialog> opens ([open] changes) the region re-promotes itself (hidePopover(); showPopover()) and stays above the backdrop, replaying its fade in. Anything outside a modal dialog is inert while it is open, so toasts shown over a modal are visible but not clickable (dismiss buttons included); their auto-dismiss timers keep running. Prefer short, non-interactive toasts while a modal is open.

Accessibility

Toasts render in a live region with appropriate status semantics. Dismiss controls expose an accessible name and keyboard support via React Aria Components.