Notifications & time

What show returns, the five kinds and the content of a notification, how to change or close it, when it opens the Island on its own, what a full stack does, and how long a notification stays on screen.

On this page

Show a notification

show takes a spec with a required title (every field: show) and returns an AdmissionResult right away: created, deduplicated or rejected with a reason. It never throws for bad input and never queues the request for later:

save.ts
import { createCloudlet } from '@cloudlet/toast';

const cloudlet = createCloudlet();

const admission = cloudlet.show({ title: 'Synced to the cloud', kind: 'success' });

if (admission.status === 'rejected') {
  console.warn('Notification not shown:', admission.reason, admission.issues ?? []);
}

Keep the returned handle in memory to update or dismiss that notification; it is not a serializable ID (NotificationHandle).

Call show from an event: a click, a finished request, a message from the server. Never call it during render, which runs again on every state change and twice in Strict Mode.

Kinds

kind sets the icon, the colour and the default time on screen; it defaults to info. All five run in Examples.

KindUse it for
successSomething finished as expected
infoA neutral notice
warningSomething the user should look at
errorSomething failed; it is not assertive unless you set announcementPriority
loadingWork in progress; operations set it for you

Content

Under the title a notification may have a description (text, or rich content with its own spoken announcement), an icon of yours and a progress ring. Each part runs in Examples.

  • The title is always plain text, never HTML.
  • Rich content must not be interactive: buttons and links belong in the action.
  • The icon is decorative and never replaces the title or the announcement, so mark an SVG aria-hidden.

Update, dismiss, clear

Change a live notification in place with update(handle, patch), close it with dismiss(handle), close all with clear(). Each returns a result instead of throwing: a closed notification is stale-handle, never success, and update never creates one. A part of the application that has no handle finds the record by an explicit id with getHandle(id):

upload-status.ts
import { createCloudlet } from '@cloudlet/toast';

const cloudlet = createCloudlet();

// An explicit id lets another part of the application find the record later.
cloudlet.show({ id: 'upload', title: 'Uploading photos…', kind: 'loading' });

const upload = cloudlet.getHandle('upload');
if (upload) cloudlet.update(upload, { title: 'Photos uploaded', kind: 'success' });

// The same dedupeKey on a live record counts an occurrence instead of adding a card.
cloudlet.show({ title: 'You are offline', kind: 'warning', dedupeKey: 'offline' });
cloudlet.show({ title: 'You are offline', kind: 'warning', dedupeKey: 'offline' }); // occurrences: 2

A deduplicated record keeps its content, its place in the stack and its remaining time. onClose on the spec is called once, with the close reason.

The stack and the Island

Every accepted notification goes straight into the stack, newest on top. At rest the stack is one pill: the top notification, tails for the ones behind it and a badge with the total.

Activating the pill opens the Island on the top notification. A notification that arrives while the Island is open joins the top of the stack without taking over the slide you are reading. Closing the current slide moves to the next older notification, then to a newer one, and collapses the Island after the last.

Snippets assume const cloudlet = useCloudlet(); — the buttons call them on this page’s own Toaster.

burst
for (const name of ['Cumulus', 'Stratus', 'Cirrus', 'Nimbus', 'Altocumulus']) {
  cloudlet.show({ title: `${name} drifted by` });
}

With two or more notifications, the Island's dots row ends with Clear all: the first press arms it (it turns red and reads "Clear 5"), a second within 3 seconds clears. clearAll={false} leaves it out.

Your own UI reads the same state from getSnapshot() and subscribe.

Attention

attention decides whether a notification opens the Island on its own: auto (the default) when it has an action, a description or a failed operation to retry, open always, stack never.

attention-auto
cloudlet.show({
  title: 'Wind is picking up',
  description: { kind: 'text', text: 'Gusts up to 40 km/h this afternoon.' },
  attention: 'auto',
});
  • The Island opens 600 ms after admission. While the user hovers, focuses, swipes or moves between the notifications, the candidate waits, and the delay starts again once they stop.
  • There is one candidate at a time: the newest one wins. Dismissing it, or an update after which its rule no longer holds, cancels the opening.
  • If the Island is already open, it moves to the candidate.
  • While a form in the open Island holds a draft, the candidate waits in the stack instead.
  • A notification whose operation fails and becomes retryable is a candidate again at that moment.
  • Opening for attention never moves keyboard focus.

The delay is attentionDelay for a controller, or attention.delay for one Toaster while it is mounted:

attention.tsx
import { CloudletProvider, createCloudlet, Toaster } from '@cloudlet/toast';

// A calmer default for every Toaster of this controller.
const cloudlet = createCloudlet({ attentionDelay: 1200 });

export function Notifications() {
  return (
    <CloudletProvider controller={cloudlet}>
      {/* While this Toaster is mounted, its own delay wins. */}
      <Toaster attention={{ delay: 300 }} />
    </CloudletProvider>
  );
}

Capacity and overflow

The stack holds 100 open notifications. A new one beyond that is rejected with capacity; an older one is never pushed out. Cloudlet counts what was missed instead: the badge and the pill's name include the count, and the Island shows it with a button to acknowledge it.

overflow
// The stack holds 100 notifications; the rest are counted as missed.
for (let cloud = 1; cloud <= 105; cloud += 1) {
  cloudlet.show({ title: `Cloud #${cloud} on the horizon` });
}
dismiss-all
cloudlet.clear();
cloudlet.acknowledgeOverflow(); // the missed count survives clear()

To log overflow or report it elsewhere, pass onOverflow when you create the controller:

cloudlet.ts
import { createCloudlet } from '@cloudlet/toast';

export const cloudlet = createCloudlet({
  onOverflow: ({ count }) => console.warn(`${count} notifications did not fit in the stack`),
});

Time on screen

The default time depends on the kind and on whether there is an action; set duration for one notification. Time is spent only by the notification on screen: the top pill while the stack is collapsed, the current slide while the Island is open. The others wait with their remaining time intact. The clock also stops while:

  • the pointer is over the notifications, or focus is inside them;
  • a swipe is in progress, or a slide is changing;
  • the tab is hidden or the window lost focus;
  • the Toaster is unmounted;
  • a command action is running;
  • the user opened the Island, or the current slide asks for action: it has an action, a failed operation to retry, or attention: 'open'.

A slide that opened the Island only because of its description keeps counting unless it is hovered or focused, then closes and moves on to the next one.

An update that changes kind or action recalculates the default time; one that sets duration starts a new budget; other fields leave the time running.

To keep every notification until it is closed, pass autoDismiss={false} to the Toaster; it applies live, so it can follow a user preference. createCloudlet({ autoDismiss: false }) makes a controller persistent from the start.

notifications.tsx
import { Toaster } from '@cloudlet/toast';

declare function usePreference(name: 'persistent-notifications'): boolean;

export function Notifications() {
  // While the user keeps notifications, none closes on a timer.
  const persistent = usePreference('persistent-notifications');
  return <Toaster autoDismiss={!persistent} />;
}