API reference

<sonner-toaster> attributes

Configure the host element via HTML attributes. All are optional.

AttributeValuesDefaultDescription
positiontop-left · top-center · top-right · bottom-left · bottom-center · bottom-rightbottom-rightWhere toasts appear relative to the viewport.
themelight · dark · systemlightColor theme. system tracks prefers-color-scheme.
rich-colorsboolean (presence)offColored success / error / warning / info variants.
close-buttonboolean (presence)offShow a close button on every toast.
invertboolean (presence)offFlip background and text against the current theme.
dirltr · rtl · autoautoText direction.
gapnumber (px)14Spacing between stacked toasts when expanded.
durationnumber (ms)4000Default lifetime; per-toast overrides win.
visible-toastsnumber3How many toasts are fully visible before fading.
offsetCSS length24pxDistance from viewport edges.
mobile-offsetCSS length16pxEdge distance under 600px viewport width.
hotkeychord string, "", or "none"altKey+KeyTKeyboard chord that expands the stack. Empty / none disables it.
expandboolean (presence)offForce-expand the stack even without hover.
container-aria-labelstringNotificationsAccessible name for the toaster region. Reactive — changes after mount take effect.

<sonner-toast> attributes

Per-toast overrides — any of these on an individual toast wins over the toaster default.

AttributeValuesDefaultDescription
typedefault · success · error · info · warning · loadingdefaultVisual variant and built-in icon.
durationnumber (ms) or Infinityinherits toasterHow long before auto-dismiss.
dismissiblefalse to opt outtrueWhether close button + swipe dismiss this toast.
positionsame as toasterinherits toasterOverride position for one toast.
close-buttonbooleaninherits toasterShow close button on this toast.
rich-colorsbooleaninherits toasterForce rich colors on this toast.
invertbooleaninherits toasterForce inverted colors on this toast.

<sonner-toast> slots

SlotPurpose
titlePrimary text.
descriptionSecondary text.
iconOverride the icon picked from type.
actionRight-aligned action button (e.g. Undo).
cancelLeft-aligned cancel button.
(default)Used by toast.custom() for fully custom content.

toast() options

The second argument to toast() (and its variants) accepts:

OptionTypeNotes
idstring | numberReuse to update a toast in place.
toasterIdstringDOM id of a specific <sonner-toaster> to route this toast to. Falls back to the default toaster if no match.
testIdstringSet as data-testid on the toast host. Survives promise transitions.
closeButtonAriaLabelstringAccessible name for the close button. Defaults to Close: <title>.
descriptionstring | Node | () => …Secondary text.
durationnumber or InfinityLifetime override (ms).
dismissiblebooleanSet false to disable swipe / close-button dismissal.
positionsame as toasterPer-toast position override.
closeButtonbooleanPer-toast override of the toaster's close-button flag.
richColors / invertbooleanForce the variant on this toast.
iconNode | stringOverride the icon picked from type.
classNamestringExtra class applied to the toast host.
action / cancel{ label, onClick } or HTMLElementRight / left button. Pass an element for full control.
onDismiss / onAutoClose(el) => voidLifecycle callbacks.

The toast() function

import { toast } from 'sonner-wc';

toast('Default message');
toast.success('Saved');
toast.error('Oops');
toast.info('FYI');
toast.warning('Heads up');
toast.loading('Working…', { duration: Infinity });
toast.message('Same as toast(), explicit form');

toast.promise()

toast.promise(savePost(), {
  loading: 'Saving…',
  success: (data) => `Saved "${data.title}"`,
  error: (err) => `Failed: ${err.message}`,
});

toast.custom()

toast.custom((id) => {
  const el = document.createElement('div');
  el.textContent = `Custom toast #${id}`;
  return el;
});

toast.dismiss()

Use the same id to update an existing toast in place. Pass no argument to dismiss everything.

const id = toast.loading('Working…');
// later…
toast.dismiss(id);

// or dismiss everything
toast.dismiss();

// update a toast in place by reusing its id
toast.loading('Working…', { id: 'job-1' });
toast.success('Done', { id: 'job-1' });

Events

Each toast dispatches the following bubbling, composed CustomEvents:

EventWhen
sonner-toast-mountedAfter the mount transition begins.
sonner-toast-updatedAfter update() applies (used by toast.promise).
sonner-toast-dismissedWhen dismiss() is called (before the exit animation finishes).
sonner-toast-autoclosedWhen the auto-dismiss timer fires.