Skip to Content
zjkm-ui
Components

Promise Toast

A toast that follows a task from loading to done.

Props
<PromiseToast promise={task} loading="Publishing…" success="Published" error="Couldn't publish" />

Installation

npx shadcn@latest add https://ui.zjkm.tech/r/promise-toast.json
import { PromiseToast } from "@/components/promise-toast"

Using JavaScript? Set tsx: false in components.json and the CLI converts it.

New to shadcn? Run npx shadcn@latest init first. The CLI needs components.json and the @/ import alias.

Usage

Pass a promise and it follows the task: a spinner while it runs, then a tick or a cross with your message, then it leaves on its own. Pass a new promise to show it again. It sits fixed in the bottom-right corner of the window and announces itself politely to screen readers.

import { PromiseToast } from "@/components/promise-toast";

export function PublishButton() {
  const [task, setTask] = useState<Promise<unknown> | null>(null);
  return (
    <>
      <button onClick={() => setTask(publish())}>Publish</button>
      <PromiseToast promise={task} loading="Publishing…" success="Published" error="Couldn't publish" />
    </>
  );
}

Props

PropTypeDefaultDescription
promise`Promisenull`
loadingstringShown while it runs.
successstringShown when it resolves.
errorstringShown when it rejects.
onClose() => voidCalled when it leaves.
lingernumber2500Milliseconds it stays after settling.

With reduced motion, the spinner turns slowly and the toast appears without sliding.

On this page