Toast

A short message that slides in at an edge of the screen and leaves on its own: toast() queues one and a Toaster shows it, at the bottom centre by default.

import { Button } from "@produkto/ui/Button";
import { toast, Toaster } from "@produkto/ui/Toast";

const Example = () => (
  <>
    <Button onClick={() => toast("Title")}>Button</Button>
    <Toaster />
  </>
);

export default Example;

Usage

import { Button } from "@produkto/ui/Button";
import { toast, Toaster } from "@produkto/ui/Toast";
<Toaster />
<Button onClick={() => toast("Title")}>Button</Button>

Examples

Border radius

borderRadius sets a toast's corners on the kit's one scale: none, xs 4px, sm 6px, base 8px, lg 12px (the default) or xl 16px; each button shows its own toast.

import { Button } from "@produkto/ui/Button";
import {
  toast,
  Toaster,
  type ToastBorderRadius,
} from "@produkto/ui/Toast";

const radii: ToastBorderRadius[] = ["none", "xs", "sm", "base", "lg", "xl"];

const Example = () => (
  <>
    <div className="flex flex-wrap items-center gap-2">
      {radii.map((borderRadius) => (
        <Button
          key={borderRadius}
          onClick={() => toast("Title", { borderRadius })}
        >
          {borderRadius}
        </Button>
      ))}
    </div>
    <Toaster />
  </>
);

export default Example;

Types

toast.success, toast.info, toast.warning and toast.destructive mark a toast with a coloured icon, toast.promise spins until its promise settles, and toast() alone has no icon.

import { Button } from "@produkto/ui/Button";
import { toast, Toaster } from "@produkto/ui/Toast";

const types: { label: string; show: () => string }[] = [
  { label: "Default", show: () => toast("Title") },
  { label: "Success", show: () => toast.success("Title") },
  { label: "Info", show: () => toast.info("Title") },
  { label: "Warning", show: () => toast.warning("Title") },
  { label: "Destructive", show: () => toast.destructive("Title") },
  {
    label: "Promise",
    show: () =>
      toast.promise<{ title: string }>(
        () =>
          new Promise((resolve) =>
            setTimeout(() => resolve({ title: "Title" }), 2000),
          ),
        {
          loading: "Loading",
          success: (data) => data.title,
          destructive: "Destructive",
        },
      ),
  },
];

const Example = () => (
  <>
    <div className="flex flex-wrap items-center gap-2">
      {types.map((type) => (
        <Button key={type.label} onClick={type.show}>
          {type.label}
        </Button>
      ))}
    </div>
    <Toaster />
  </>
);

export default Example;

Description

description sets a quieter line under the title, where by default a toast is its title alone.

import { Button } from "@produkto/ui/Button";
import { toast, Toaster } from "@produkto/ui/Toast";

const Example = () => (
  <>
    <Button onClick={() => toast("Title", { description: "Description" })}>
      Button
    </Button>
    <Toaster />
  </>
);

export default Example;

Action

action sets a small outline button at the toast's end that closes it once clicked, where by default a toast has none.

import { Button } from "@produkto/ui/Button";
import { toast, Toaster } from "@produkto/ui/Toast";

const Example = () => (
  <>
    <Button onClick={() => toast("Title", { action: { label: "Button" } })}>
      Button
    </Button>
    <Toaster />
  </>
);

export default Example;

Side

side puts a toast at the top or the bottom edge of the screen, the bottom by default, the Toaster's for every toast unless one picks its own.

import { Button } from "@produkto/ui/Button";
import { toast, Toaster, type ToastSide } from "@produkto/ui/Toast";

const sides: { side: ToastSide; label: string }[] = [
  { side: "top", label: "Top" },
  { side: "bottom", label: "Bottom" },
];

const Example = () => (
  <>
    <div className="flex flex-wrap items-center gap-2">
      {sides.map((item) => (
        <Button
          key={item.side}
          onClick={() => toast("Title", { side: item.side })}
        >
          {item.label}
        </Button>
      ))}
    </div>
    <Toaster />
  </>
);

export default Example;

Align

align sets a toast at the start, the middle or the end of its edge, the middle by default, the Toaster's for every toast unless one picks its own.

import { Button } from "@produkto/ui/Button";
import { toast, Toaster, type ToastAlign } from "@produkto/ui/Toast";

const aligns: { align: ToastAlign; label: string }[] = [
  { align: "start", label: "Start" },
  { align: "center", label: "Center" },
  { align: "end", label: "End" },
];

const Example = () => (
  <>
    <div className="flex flex-wrap items-center gap-2">
      {aligns.map((item) => (
        <Button
          key={item.align}
          onClick={() => toast("Title", { align: item.align })}
        >
          {item.label}
        </Button>
      ))}
    </div>
    <Toaster />
  </>
);

export default Example;

Duration

duration is how many milliseconds a toast stays, 5000 by default, and Infinity keeps it until it is closed; hovering it pauses the count.

import { Button } from "@produkto/ui/Button";
import { toast, Toaster } from "@produkto/ui/Toast";

const durations: { duration: number; label: string }[] = [
  { duration: 2000, label: "2 seconds" },
  { duration: 5000, label: "5 seconds" },
  { duration: 10000, label: "10 seconds" },
  { duration: Infinity, label: "Until closed" },
];

const Example = () => (
  <>
    <div className="flex flex-wrap items-center gap-2">
      {durations.map((item) => (
        <Button
          key={item.label}
          onClick={() => toast("Title", { duration: item.duration })}
        >
          {item.label}
        </Button>
      ))}
    </div>
    <Toaster />
  </>
);

export default Example;

Id

id names a toast, so another with the same id replaces it in place, where by default each new toast stacks under the last.

import { Button } from "@produkto/ui/Button";
import { toast, Toaster } from "@produkto/ui/Toast";

const Example = () => (
  <>
    <div className="flex flex-wrap items-center gap-2">
      <Button onClick={() => toast("Title")}>Default</Button>
      <Button onClick={() => toast("Title", { id: "title" })}>With id</Button>
    </div>
    <Toaster />
  </>
);

export default Example;

API Reference

Toast

ToastProps · components/Toast/Toast.types.ts
PropTypeDefault
borderRadiusToastBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl""lg"

ToastProvider

ToastProviderProps · takes the props of Radix Toast.Provider

ToastViewport

ToastViewportProps · also takes the props of Radix Toast.Viewport
PropTypeDefault
sideToastSide"top" | "bottom""bottom"
alignToastAlign"start" | "center" | "end""center"
hotkey-["F8"]

ToastTitle

ToastTitleProps · takes the props of Radix Toast.Title

ToastDescription

ToastDescriptionProps · takes the props of Radix Toast.Description

ToastAction

ToastActionProps · also takes the props of Radix Toast.Action
PropTypeDefault
appearanceButtonAppearance"solid" | "soft" | "outline" | "transparent""outline"
toneButtonTone"neutral" | "accent" | "destructive" | "success" | "warning" | "info""neutral"
sizeButtonSize"xs" | "sm" | "base" | "lg""sm"

ToastClose

ToastCloseProps · takes the props of Radix Toast.Close

Toaster

ToasterProps
PropTypeDefault
sideToastSide"top" | "bottom""bottom"
alignToastAlign"start" | "center" | "end""center"
durationnumber-
classNamestring-

It also takes the props of Radix Toast.Root.