Toast

A short message that pops up after people do something, then disappears by itself.

import { ToastProvider, useToast } from '@halo-ds/react';

When to use it

Use a toast to confirm an action without interrupting people. Often a title is all it needs, like "Link copied" or "Changes saved". Add a line of detail or one quick action, like Undo, only when there is more to say or do. Toasts appear in a corner and go away after a few seconds. For messages people must act on, use an Alert.

Do

  • Use just a title when it says enough, like "Link copied".
  • Add a description or an action only when there is more to say or do, like Undo.
  • Show one toast per action.
  • Show toasts in the same place every time.

Don't

  • Put errors people must fix in a toast; use an Alert.
  • Show more than three toasts at once.
  • Use a toast for something people need to read later.
  • Put more than one action in a toast.

Toast, Alert or Modal?

All three show messages. Pick by how long it should stay, and whether people must answer.

  • Toast: Confirms something people just did. Disappears by itself. Never blocks the page.
  • Alert or Modal: Alert: stays in the page until the problem is fixed. Modal: a question that must be answered first. If missing the message would cause problems, don't use a toast.

Props

ToastProvider props

PropTypeWhat it does
children (required)ReactNode
position`"bottom-right" \"bottom-left" \"bottom-center" \"top-right" \"top-left" \"top-center" \undefined`Corner of the screen the toasts stack in.
limit`number \undefined`Most toasts shown at once. Older ones close first.
classNames`Partial<Record<ToastRegionPart, string>> \undefined`Extra class names for the parts inside the component, by part name, like { header: 'my-header' }.
styles`Partial<Record<ToastRegionPart, CSSProperties>> \undefined`Inline styles for the parts inside the component, by part name, like { footer: { gap: 4 } }.

It also takes the usual HTML attributes for its element.

Examples

Overview

import { Button, useToast } from '@halo-ds/react';

// Wrap your app once in <ToastProvider>, then call toast() from anywhere.
export default function Example() {
  const { toast } = useToast();
  return (
    <div style={{ display: 'flex', gap: 12 }}>
      <Button onClick={() => toast({ type: 'success', title: 'Changes saved' })}>Save</Button>
      <Button variant="secondary" onClick={() => toast({ type: 'error', title: 'Couldn\u2019t send', description: 'Check your connection.' })}>
        Show an error
      </Button>
    </div>
  );
}

Types

Neutral, information, success, warning and error.

import { Toast } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-stack">
      <Toast type="neutral" title="Message archived" onClose={() => {}} />
      <Toast type="information" title="Update available" onClose={() => {}} />
      <Toast type="success" title="Changes saved" onClose={() => {}} />
      <Toast type="warning" title="Storage almost full" onClose={() => {}} />
      <Toast type="error" title="Upload failed" onClose={() => {}} />
    </div>
  );
}

Inverse with an action

A dark toast with Undo, for actions people might want to take back.

import { Button, Toast } from '@halo-ds/react';
import { Archive } from '@phosphor-icons/react';

export default function Demo() {
  return (
    <Toast
      variant="inverse"
      icon={<Archive />}
      title="Message archived"
      description="It moved to the archive."
      actions={<Button variant="secondary" size="xs">Undo</Button>}
      onClose={() => {}}
    />
  );
}

More: https://halodesignsystem.dev/components/toast