Skip to content

Sonner ​

Transient notifications, the way shadcn ships them: a region mounted once near the root of the app, and a toast function callable from anywhere — an event handler, a form's error branch, a service — with no handle threaded down to it. The API is sonner's: the same toast methods, the same options, the same Toaster props and the same defaults — four seconds on screen, three toasts visible, a 14 px gap, 356 px wide, Alt+T to reach the region. The look is shadcn's: the popover surface and the lucide icons.

Toasts pile up behind the newest one, scaled down, and fan out while the pointer is over them — which also holds every timer, so a toast being read does not vanish. The stack is computed into style properties rather than driven by an injected stylesheet, so it works under every adapter. They are written through the CSSOM, never as a style attribute, so a strict Content-Security-Policy — no 'unsafe-inline', as blackhole sends by default — leaves them alone. The server writes no properties: until the page hydrates, the list is already fixed through a class, and it is empty anyway, since toasts are raised on the client.

sh
ream nebula:add sonner

Organism · organisms/Sonner.ts · no dependencies

Usage ​

ts
import { Sonner, sonnerToast } from '@c9up/nebula'

// Once, in the app's layout.
Sonner({ richColors: true, closeButton: true })

// Anywhere.
sonnerToast('Event created', { description: 'Sunday, 9:00' })

The file exports Toaster and toast, as sonner does. The package barrel renames them Sonner and sonnerToast, because the deprecated Toaster still owns those names; a copy added with ream nebula:add sonner keeps the original names:

ts
import { Toaster, toast } from './components/organisms/Sonner.js'

The examples below use the file's names.

Examples ​

Types ​

ts
toast('Invitation sent')                                  // plain: no icon
toast.success('Changes saved', { description: 'They are live for everyone.' })
toast.info('A new version is available')
toast.warning('Storage almost full')
toast.error('Could not save')
toast.loading('Uploading report.pdf…')                     // stays until replaced

Each type but the plain one carries shadcn's icon. A loading toast has no timer: it waits for an outcome. duration: Infinity keeps any other toast until it is dismissed.

Rich colours ​

ts
Toaster({ richColors: true })
toast.error('Payment declined')   // a red surface instead of the popover one

richColors can also be set on one toast. theme ('light', 'dark', or 'system', the default, which follows the app's dark variant) and invert change the surface.

An action, and a cancel ​

ts
toast('Conversation archived', {
  action: { label: 'Undo', onClick: () => unarchive(conversationId) },
})

toast('Delete this draft?', {
  cancel: { label: 'Keep it', onClick: () => {} },
  action: { label: 'Delete', onClick: () => remove(draftId) },
})

The action runs its onClick, then closes the toast — unless the handler called event.preventDefault(), which keeps it. The cancel button runs its onClick and closes the toast without calling onDismiss. Both take { label, onClick }; for content of your own, use toast.custom.

One toast per problem ​

A toast raised under an id already on screen updates it instead of stacking another, so "you are offline", raised on every failed request, stays one toast:

ts
toast.warning('You are offline', { id: 'offline', duration: Infinity })
toast.warning('Still offline — retrying', { id: 'offline', duration: Infinity })

// Back online.
toast.dismiss('offline')

The same mechanism turns a loading toast into its outcome:

ts
const id = toast.loading('Saving…')
// later
toast.success('Saved', { id })

toast.dismiss() with no id dismisses every toast, and toast.getToasts() lists those on screen, newest first.

Following a promise ​

ts
const { id, unwrap } = toast.promise(save(form), {
  loading: 'Saving…',
  success: (saved) => `Saved ${saved.name}`,
  error: 'Could not save',
})

const saved = await unwrap()   // the value, or the rejection handed back

One toast goes from loading to success or error. A stage is a message, a function of the outcome (which may be async), or { message, ...options }. An HTTP response whose ok is false is reported through error, with HTTP error! status: <code>. When no stage says what happened, the loading toast is dismissed. finally runs once the promise settles. work may also be a function returning the promise.

toast.promise returns { id, unwrap } rather than the id with unwrap attached: a rejection is reported in the toast, and unwrap() hands it back to the caller's own error path.

Your own content ​

ts
toast.custom((id) => html`<div class="rounded-md border p-4">
  Uploaded. ${Button({ size: 'sm', onClick: () => toast.dismiss(id), children: 'OK' })}
</div>`)

render receives the toast's id. The result is shown in place of the title, unstyled.

Position, several regions ​

ts
Toaster({ position: 'top-center', expand: true, visibleToasts: 5 })

toast('Copied', { position: 'bottom-center' })   // a corner of its own

There are six positions — top-left, top-center, top-right, bottom-left, bottom-center, bottom-right (the default). A toast with its own position gets a list of its own in that corner. expand keeps the stack fanned out. A second region takes an id, and shows only the toasts raised with that toasterId:

ts
Toaster({ id: 'editor', position: 'top-right' })
toast('Draft saved', { toasterId: 'editor' })

API ​

Toaster ​

The region. Mount it once near the root; it shows the toasts addressed to its id, or those with no toasterId when it has none.

PropTypeDefaultDescription
positionPosition'bottom-right'The corner.
richColorsbooleanfalseColoured surfaces per type.
closeButtonbooleanfalseA close cross on every toast.
expandbooleanfalseKeep the stack fanned out instead of piled up.
durationnumber4000Milliseconds on screen.
visibleToastsnumber3How many show at once; the rest are kept, hidden.
gapnumber14Space between fanned-out toasts, in px.
theme'light' | 'dark' | 'system''system'system follows the app's dark variant.
invertbooleanfalseSwap the light and dark surfaces.
hotkeystring[]['altKey', 'KeyT']KeyboardEvent keys and codes that reach the region.
offsetOffset'24px'From the viewport's edges: a value, or { top, right, bottom, left }. Each is set as that one property; a value the property refuses is ignored.
mobileOffsetOffset'16px'The same, at 600 px wide and under.
dir'rtl' | 'ltr' | 'auto'—Writing direction of the region.
iconsToastIconsshadcn'sReplace the icon of success, info, warning, error, loading, or the close cross; null shows none.
toastOptionsToastOptions—Defaults for every toast: class, descriptionClass, classNames, closeButton, duration, unstyled, closeButtonAriaLabel (default 'Close toast').
containerAriaLabelstring'Notifications'The region's name, before the hotkey.
customAriaLabelstring—Replaces the region's whole accessible name.
idstring—Show only the toasts raised with this toasterId.
classReactive<string>—Merged into the region's classes.

toast ​

MethodReturnsDescription
toast(message, options?)ToastIdA plain toast. toast.message is the same.
toast.success / info / warning / error (message, options?)ToastIdA toast of that type, with its icon.
toast.loading(message, options?)ToastIdA spinner and no timer.
toast.custom(render: (id) => Child, options?)ToastIdYour own content, unstyled.
toast.promise(work, data?){ id, unwrap }One toast for the life of an async call.
toast.dismiss(id?)ToastId | undefinedDismiss one toast, or every toast with no id.
toast.getToasts()ToastT[]The toasts on screen, newest first.

Toast options ​

What every toast call takes after its message (ExternalToast).

OptionTypeDescription
idnumber | stringA stable id. A toast raised under an id already on screen updates it.
descriptionSlotA second, muted line.
durationnumberMilliseconds on screen. Infinity keeps it until dismissed.
action / cancel{ label, onClick }The two buttons.
onDismiss(toast) => voidCalled when the close cross or toast.dismiss removes it — not on cancel.
onAutoClose(toast) => voidCalled when its time runs out.
dismissiblebooleanfalse keeps the close cross and the cancel button from removing it; its timer still runs, so pair it with duration: Infinity to pin it.
closeButton, richColors, invertbooleanPer-toast overrides of the Toaster's.
iconChildReplaces the type's icon; null shows none.
positionPositionA corner of its own.
toasterIdstringShown only by the Toaster with this id.
class, descriptionClass, classNamesstring, string, ToastClassnamesExtra classes, merged after nebula's own.
unstyledbooleanDrops nebula's look, keeping only the layout.
testIdstringWritten as data-testid.

Accessibility ​

The region is a <section> named after the hotkey ("Notifications alt+T"), and it holds two hidden live regions, mounted empty before any toast exists: a polite one (role="status") for every toast, and an assertive one (role="alert") for errors — an error does not wait behind whatever is being read. An update is announced too, so a promise reaching its outcome is heard. The region carries data-nebula-live, which exempts it from the aria-hidden a modal Dialog puts on its siblings.

KeyAction
Alt+TFans the stack out and moves focus into the region. hotkey changes it.
EscapeWith focus in the region, piles the stack back up.
TabReaches the close cross and the buttons, native <button>s.

Hovering the stack, pressing on it, and a hidden tab hold every timer; each resumes with the time it had left. There is no swipe to dismiss and no getHistory. The close cross — never shown on a loading or a custom toast — is named "Close toast", which toastOptions.closeButtonAriaLabel replaces.