Skip to content

Toast

A short message that appears in a corner to say how something the person just did turned out, and leaves by itself.

import { toast, Toaster } from '@sypnia/ui/react'

On this page
  1. Tones
  2. With «Undo»
  3. After work that takes a moment
  4. When to use it
  5. For everyone

Tones

Success is the default. The options are tone, detail, action and duration in milliseconds. Press each button.

Code
import { Button, toast, Toaster } from '@sypnia/ui/react';

// Place <Toaster /> once, at the root of the app. Then call toast() from anywhere.
export default function Tones() {
  return (
    <div className="sy-row sy-row--pairs">
      <Button onClick={() => toast('Changes saved')}>Saved</Button>
      <Button onClick={() => toast('Key copied', { detail: 'Paste it in the settings of your app.' })}>With a detail</Button>
      <Button onClick={() => toast('The export will arrive by email', { tone: 'info' })}>Information</Button>
      <Button onClick={() => toast('The row was not deleted', { tone: 'fail', detail: 'Another table points at it.' })}>Failure</Button>
      <Toaster />
    </div>
  );
}

With «Undo»

Remove a filter, then undo it from the toast.

status: paidregion: eutotal > 100
Code
import { useState } from 'react';
import { Button, Chip, toast, Toaster } from '@sypnia/ui/react';

const all = ['status: paid', 'region: eu', 'total > 100'];
// One action at most. With it, the toast stays a little longer.
export default function Undo() {
  const [filters, setFilters] = useState(all);
  const remove = (f: string) => {
    const before = filters;
    setFilters(filters.filter((x) => x !== f));
    toast(`Filter «${f}» removed`, { action: { label: 'Undo', onClick: () => setFilters(before) } });
  };
  return (
    <div className="sy-chips">
      {filters.map((f) => <Chip key={f} onRemove={() => remove(f)} removeLabel={`Remove the filter ${f}`}>{f}</Chip>)}
      {filters.length === 0 && <Button size="sm" onClick={() => setFilters(all)}>Restore the filters</Button>}
      <Toaster />
    </div>
  );
}

After work that takes a moment

The button shows the work; the toast gives the result.

Code
import { useState } from 'react';
import { Button, toast, Toaster } from '@sypnia/ui/react';

// The button shows the work; the toast gives the result. Long messages stay longer.
export default function AfterAnAction() {
  const [busy, setBusy] = useState(false);
  const backup = () => {
    setBusy(true);
    setTimeout(() => { setBusy(false); toast('Backup finished', { detail: '5 tables and 5,552 rows saved. It is kept for 7 days.' }); }, 1800);
  };
  return (
    <>
      <Button variant="primary" busy={busy} onClick={backup}>{busy ? 'Backing up…' : 'Back up now'}</Button>
      <Toaster />
    </>
  );
}

When to use it

Use it when

  • The answer to an action: «Changes saved», «Key copied», «Row deleted».
  • The action can be undone: add one action («Undo»).
  • Call toast() from anywhere, with <Toaster /> placed once at the root of the app.

Do not use it when

  • The person must read it or decide something: it leaves by itself, so they may never see it. Use an Alert or a Dialog.
  • A failure needs fixing: show an Alert where it happened, with the way out.
  • Something happened that the person did not start: Banner, or a notification.
  • Several things finish at once: say it in one toast («3 tables exported»).

For everyone

  • Toasts are in a polite live region: screen readers say them after what they are reading, without taking the focus.
  • They stay longer the more text they have, and 7 seconds when they carry an action. Never put there the only way to do something.
  • Each has a «Dismiss» button reachable with Tab. At most four are on screen.
  • Every tone has its own icon. On phones they appear at the bottom, centred.
  1. Tones
  2. With «Undo»
  3. After work that takes a moment
  4. When to use it
  5. For everyone

    Search pages, sections and terms