|Kit

Notifier

Spawn toast notifications imperatively from anywhere — success/error/warning/info/loading, actions, promises and in-place updates — with createNotifier. A custom toaster modelled on the overlays manager, animated in CSS.

The Notifier is the imperative toast manager. You create one, mount its region once near the app root, then call notify.* from anywhere — an event handler, a data mutation, a background task — with no useState or JSX at the call site. Each live toast is rendered as a Notification card.

It's a custom toaster modelled on the kit's createOverlays: a framework-agnostic store + a once-mounted renderer + a bound controller. Ark UI's Presence drives each toast's enter/exit lifecycle; everything else — the queue, the timers, placement, the deck↔column stacking, hover-pause, swipe-to-dismiss — is the kit's own, with the display and animation pushed into CSS (so heights animate, and it all respects prefers-reduced-motion).

Setup

createNotifier(options) returns a bound notify object and a <Notifications /> renderer. Call it once in a "use client" module and re-export the pieces:

app/notifier.ts
'use client';
import { createNotifier } from '@42-central/ui-react/notification';

export const { notify, Notifications } = createNotifier({ placement: 'bottom-end' });

Mount <Notifications /> once near the root. It renders nothing until a toast opens and is SSR-inert, so it's safe at the top of the tree:

app/layout.tsx
import { Notifications } from './notifier';

export default function Layout({ children }) {
  return (
    <body>
      {children}
      <Notifications />
    </body>
  );
}

Then call notify from anywhere:

import { notify } from '@/app/notifier';

notify('Copied to clipboard');            // neutral toast
notify.success('Saved');

Status presets

Five presets — success, error, warning, info, loading — each maps to a hue (green / red / orange / neutral / neutral) and a default leading icon (loading shows a spinner and never auto-dismisses). Pass a bare string for the title, or an options object for more.

Code

notify.success('Changes saved');
notify.error('Something went wrong');
notify.warning('Double-check your input');
notify.info('A new version is available');
notify.loading('Syncing…');

With a description

Pass an options object with title and description for a secondary line. It reveals with a Collapse height animation.

Code

notify.success({
  title: 'Profile updated',
  description: 'Your changes are live across all 42 campuses.',
});

Action

action is any ReactNode — its own section under the toast, revealed independently of the description. Compose it however you like; the kit ships Notification.ActionTrigger for the standard button styling. Keep the id notify.* returns to dismiss the toast from inside the handler — good for an Undo.

Code

const id = notify.info({
  title: 'Message archived',
  action: (
    <Notification.ActionTrigger
      onClick={() => {
        notify.success('Message restored');
        notify.dismiss(id);
      }}
    >
      Undo
    </Notification.ActionTrigger>
  ),
});

Promise

notify.promise drives a single toast through an async lifecycle: it starts as loading, then transitions to success or error when the promise settles. success / error may be functions of the resolved value / thrown error.

Code

notify.promise(upload(), {
  loading: 'Uploading…',
  success: (name) => ({ title: 'Upload complete', description: name }),
  error: 'Upload failed — please try again',
});

Updating a toast

Every spawn returns an id. Keep it and call notify.update(id, …) to patch a live toast — only the fields you pass change; anything you already set is kept, and the status type is preserved. To change the status too (say a manual loading → success), re-call the status helper with the same id. Because the description and action are independent Collapse sections, adding or removing either animates its height rather than jumping.

Code

const id = notify.loading('Uploading…');

// patch in place — keeps everything else
notify.update(id, { description: 'Halfway there…' });

// …or transition its status (resets to the success preset)
notify.success({ id, title: 'Uploaded' });

Custom color

Every toast shares one outline surface; color is the only styling knob. A status helper picks the hue for you, so it doesn't take a color (passing one is a type error). Call notify(...) directly for a toast with no status semantics and give it any palette color; pass icon: null to drop the leading glyph.

Code

// fully custom — no status, no icon
notify({
  title: 'Pro tip',
  description: 'Press ⌘K to search anywhere.',
  color: 'purple',
  icon: null,
});

Placement & stacking

placement (a createNotifier option) anchors the region to any edge or corner. With expand (default), toasts collapse into a tidy deck — older cards peek behind the newest, tops aligned — that expands into a column on hover; hovering also pauses the auto-dismiss timers. max caps how many render at once.

Because placement belongs to the notifier (not an individual toast), each button below is its own createNotifier instance.

Code

const { notify, Notifications } = createNotifier({ placement: 'top-end', expand: true, max: 5 });

From inside a modal

Most mutations live in a form inside a Modal or Drawer, so that is where most failures get reported. A toast created while a modal is open paints above it, and screen readers announce it. Clicking or swiping it doesn't count as an outside click, so dismissing the toast leaves the modal and the form open. Toasts that were already on screen when the modal opened stay under it, as page content does. See Stacking.

Code

<Modal open={open} onOpenChange={setOpen} title="Edit slot">
  <Button onClick={() => notify.error({
    title: "Couldn't save the slot",
    description: 'Someone else changed it. Reload and try again.',
  })}>
    Save
  </Button>
</Modal>

Behaviors

  • Auto-dismiss after duration (default 5s); loading toasts never expire.
  • Pause while the pointer is over the stack, while keyboard focus is inside it, and while the tab is hidden — so nothing expires unseen, and a keyboard user reaching an action gets the same time as a mouse. Focus moving between toasts keeps the hold; it lets go once focus leaves the stack.
  • Swipe / drag a toast aside to dismiss it, or click it. Presses that start on the close button or an action are left to that control.
  • Mobile (below the xs breakpoint) the region pins to the bottom, full-width, above the safe area — regardless of placement.

Accessibility

  • Every toast is announced. Each one is its own live region: role="status" for success, warning, info, loading and neutral toasts, which waits for the screen reader to finish, and role="alert" for an error, which interrupts. The whole card is read together (title, description and action), and read again when it changes in place, so a notify.promise that settles or a notify.update is announced too; a loading toast that fails becomes an alert.
  • Nothing is said twice. A toast that was already on screen when <Notifications /> mounted (say it remounts on navigation) was announced when it arrived, so it rejoins as a polite status, even an error, rather than interrupting to repeat itself.
  • The region is a landmark, named "Notifications" (translations.regionLabel), so a screen-reader user can jump to the stack and read the toasts at their own pace.
  • Timers wait for the keyboard: see Pause under Behaviors.

Playground

The controls below build a single toast and spawn it on click. Notifier-level options (placement / max / expand) are set once at creation, so tune those in your createNotifier call.

NotificationPlayground

Loading preview

Props

Code

<NotificationPlayground
  status="success"
  message="Changes saved"
  duration={5000}
/>

API

No testId on notify.* calls

Unlike the rest of the kit, notify(...) / notify.success(...) / etc. don't take a testId — there's no props object rendered as a component here to thread one through, just a plain input object consumed by an imperative call. Each call already returns an id (see notify.update / notify.dismiss above) for referencing a specific toast from code; to find one in the DOM, query by role/text per the Testing guide, or reach for Notification.Root's own testId when you compose a bespoke toast body by hand.

createNotifier(options) — configures the notifier and its region:

Prop

Type

<Notifications /> — the region renderer createNotifier returns:

Prop

Type

<Notifications translations={{ regionLabel: 'Alertes', closeTriggerLabel: 'Fermer' }} />

notify(input) and notify.success/error/warning/info/loading(input) — where input is a title node or these options:

Prop

Type

The bound notify object also carries: notify.promise(promise, { loading, success?, error? }), notify.update(id, input), notify.dismiss(id?) (one toast, or all if omitted), notify.remove(id) (drop immediately, no exit), notify.has(id), and notify.getToasts().

Changelog

v0.27.0
  • A toast raised while a Modal, Drawer or Lightbox is open paints above it, is announced, and no longer closes it when clicked

    #453
v0.20.0
  • Swap any built-in icon through an icons map — icons={{ asc, desc }} on DataTable, icons={{ show, hide }} on PasswordInput; a key you leave out keeps the kit's icon, and a part's children still win

    #361
  • Toasts are announced — errors as alert, others as polite status — and auto-dismiss pauses while focus is in the stack

    #376
v0.19.0
  • A translations prop for the close, dismiss, collapse/expand and region labels; <Notifications /> forwards its keys to every toast

    #331
v0.3.0
  • closable → withCloseButton on Alert, Notification and the notifier

    #25
v0.1.0
  • New toast Notifications with a notifier: notify, notify.update, and a collapsible body

    #8

On this page