Skip to content

Feedback

Toast

A global notification system. Mount Toaster once, then call toast.success(), toast.error() and friends from anywhere.

Mount Toaster once in the root layout:

<!-- +layout.svelte -->
<script>
  import { Toaster } from "@codeatlasdev/ui";
  let { children } = $props();
</script>

{@render children()}
<Toaster />

Then call toast from anywhere:

<script>
  import { toast } from "@codeatlasdev/ui";
</script>

<button onclick={() => toast.success("Changes saved.")}>Save</button>

toast is a global store, so the calls need no component context. The Toaster is a polite live region (role="region", aria-live="polite") and each toast is a role="status" (success, info, default) or role="alert" (error, warning) with a dismiss button. Toasts stack in the bottom right corner, up to 380px wide, in a fixed container layered with --ca-z-snackbar (they are not in the browser top layer, unlike dialogs and popovers). toast.show accepts variant, duration, id, description and action in its options; the shortcut methods take the duration or the same options object (without variant) as a second argument.

Calling toast with the same variant and message again does not stack a copy: the existing toast restarts its timer and shows a ×N count, so toast.error("x") ten times in a row leaves one toast. At most three toasts show at once and the oldest is dropped past that. Pass id to replace one specific toast in place (new text, variant and timer, count back to 1), for example an “Uploading…” toast that becomes “Uploaded”. The demo below mounts its own Toaster because every demo is an independent island.

#103

Variants and duration

Five variants, plus persistent (duration 0) and custom durations. The default auto-dismiss is 4 seconds.

Variants

Duration

Reference #103 · Pick in the gallery

Code
svelte
<script lang="ts">
  import { Button, Toaster, toast } from '@codeatlasdev/ui';
</script>

<Toaster />

<div class="group">
  <p class="label">Variants</p>
  <div class="row">
    <Button variant="outlined" onclick={() => toast.show('This is a default notification.')}>Default</Button>
    <Button variant="outlined" onclick={() => toast.success('Changes saved successfully.')}>Success</Button>
    <Button variant="outlined" onclick={() => toast.error('Something went wrong. Please try again.')}>Error</Button>
    <Button variant="outlined" onclick={() => toast.warning('Your session expires in 5 minutes.')}>Warning</Button>
    <Button variant="outlined" onclick={() => toast.info('A new version is available.')}>Info</Button>
  </div>
</div>

<div class="group">
  <p class="label">Duration</p>
  <div class="row">
    <Button variant="outlined" onclick={() => toast.show('This will not auto-dismiss.', { variant: 'info', duration: 0 })}>
      Persistent (duration 0)
    </Button>
    <Button variant="outlined" onclick={() => toast.success('Gone in 1 second.', 1000)}>1 s</Button>
    <Button variant="outlined" onclick={() => toast.info('Stays for 8 seconds.', 8000)}>8 s</Button>
  </div>
</div>

<style>
  .group { flex-basis: 100%; display: grid; gap: 8px; }
  .label {
    margin: 0;
    font-size: 12px;
    font-weight: 500;
    color: var(--ca-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }
  .row { display: flex; flex-wrap: wrap; gap: 8px; }
</style>

Add a secondary line with description and one inline button with action: { label, onClick }. Clicking the action runs onClick and dismisses the toast, and an actionable toast stays 8 seconds unless you pass duration. The description is part of the same live-region announcement as the message. The auto-dismiss timer pauses while the toast is hovered or holds focus (WCAG 2.2.1) and restarts with the time that was left; Escape dismisses the toast that has focus. Toasts with an action never merge as repeats, since each carries its own handler.

#492

Description and action

A description under the title, a single inline action, and both. The action is a real button reachable with Tab.

Reference #492 · Pick in the gallery

Code
svelte
<script lang="ts">
  import { Button, Toaster, toast } from '@codeatlasdev/ui';
</script>

<Toaster />

<Button variant="outlined" onclick={() => toast.success('Task completed', { description: 'Next step scheduled for tomorrow 09:00' })}>
  With description
</Button>
<Button
  variant="outlined"
  onclick={() =>
    toast.warning('Sequence paused for Acme', {
      action: { label: 'Mark as qualified', onClick: () => toast.success('Marked as qualified.') },
    })}
>
  With action
</Button>
<Button
  variant="outlined"
  onclick={() =>
    toast.info('You have 7 tasks today', {
      description: 'Three are due before noon.',
      action: { label: 'Open Today', onClick: () => toast.success('Opening the queue…') },
    })}
>
  Both
</Button>
#490

Dedupe and replace by id

Ten identical errors collapse into one toast with a count; the upload toast is replaced in place by its id.

Reference #490 · Pick in the gallery

Code
svelte
<script lang="ts">
  import { Button, Toaster, toast } from '@codeatlasdev/ui';

  function flood() {
    for (let i = 0; i < 10; i++) toast.error('Too many requests, retry later.');
  }

  function upload() {
    toast.info('Uploading report.pdf…', { id: 'upload', duration: 0 });
    setTimeout(() => toast.success('report.pdf uploaded.', { id: 'upload' }), 1500);
  }
</script>

<Toaster />

<Button variant="outlined" onclick={flood}>Fail 10 times</Button>
<Button variant="outlined" onclick={upload}>Upload with id</Button>

API

PropTypeDefaultDescription
toast.show(message, opts?)(message: string, opts?: ToastOptions) => string–Shows a toast and returns its id. Variant defaults to 'default'. An identical message and variant bumps the existing toast's count instead of adding one.
toast.success(message, durationOrOpts?)(message: string, duration?: number | Omit<ToastOptions, 'variant'>) => string–Success toast.
toast.error(message, durationOrOpts?)(message: string, duration?: number | Omit<ToastOptions, 'variant'>) => string–Error toast.
toast.warning(message, durationOrOpts?)(message: string, duration?: number | Omit<ToastOptions, 'variant'>) => string–Warning toast.
toast.info(message, durationOrOpts?)(message: string, duration?: number | Omit<ToastOptions, 'variant'>) => string–Info toast.
descriptionstring–In `ToastOptions`: secondary, smaller line under the message (`--ca-text-secondary`).
action{ label: string; onClick: () => void }–In `ToastOptions`: one inline button. Runs `onClick` and dismisses the toast. Sets the default duration to 8000 ms.
toast.pause(id) / toast.resume(id)(id: string) => void–Hold and restart the auto-dismiss timer with the time left. The Toaster calls them on hover and focus; call them yourself only for a custom renderer.
idstring–In `ToastOptions`: replaces the toast with this id in place (text, variant, timer, count 1), or creates it with this id. Returned by every call.
ToastItem.countnumber–How many identical calls the toast stands for; the Toaster shows `×N` when above 1.
toast.remove(id)(id: string) => void–Dismisses a toast by id.
toast.itemsToastItem[]–Reactive list of the visible toasts.
durationnumber4000Auto-dismiss delay in ms. 0 keeps the toast until dismissed.
<Toaster inline />boolean–Stacks inside the nearest positioned ancestor (position: absolute) instead of the viewport, so it takes the tokens of a data-preset island. Default false.
ToastVariant'default' | 'success' | 'error' | 'warning' | 'info'–Accepted variants.