aiellie

Search

Find a component, template or page

GitHub
Design

Toast

Stacked and anchored notifications with actions, semantic status icons and swipe-to-dismiss behavior.

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/toast"

shadcn needs the @aiellie registry in components.json first. npx aiellie add toast adds it on its first run, then installs the same way.

Toast says something happened without stopping anyone: a save, a copy, an export that finished or failed. It is Base UI's toast with this registry's glass on it. Toasts stack in the corner of the screen, or anchor to the button that caused them for a quick confirmation, and each one wears an icon for its type.

Getting started

  1. Add the provider and a toaster

    Once, near the root of the app. A manager made outside React lets anything add a toast, not only components.

    app/providers.tsx
    "use client"
    
    import {
      ToastProvider,
      Toaster,
      createToastManager,
    } from "@/components/ui/toast"
    
    export const toasts = createToastManager()
    
    export function Providers({ children }: { children: React.ReactNode }) {
      return (
        <ToastProvider toastManager={toasts}>
          {children}
          <Toaster />
        </ToastProvider>
      )
    }
  2. Add a toast

    type picks the icon: success, info, warning, error or loading. actionProps adds a button, like Undo.

    toasts.add({
      title: "Chat deleted",
      description: "It's gone from your history.",
      type: "success",
      actionProps: { children: "Undo", onClick: restore },
    })

Anatomy

<div data-slot="toast-viewport"> {/* Toaster: the bottom corner, stacking */}
  <div data-slot="toast" data-type="success"> {/* glass, a shadow */}
    <div data-slot="toast-content">
      <span data-slot="toast-icon" />
      <div data-slot="toast-title" />
      <div data-slot="toast-description" />
      <button data-slot="toast-action" /> {/* only with actionProps */}
    </div>
    <button data-slot="toast-close" />
  </div>
</div>

{/* AnchoredToaster: each toast beside its anchor */}
<div data-slot="toast-positioner">
  <div data-slot="toast"><span data-slot="toast-arrow" />…</div>
</div>

Stacked toasts fan out behind the newest, open up on hover, and can be swiped away. They slide in from below and out the same way; a toast pushed out by the limit fades instead. An error toast's title is in destructive. A loading toast spins until its promise settles, then becomes the success or error toast in place.

Anchored toasts are one line, a description and an icon, with an arrow toward the anchor. Both toasters take variant="solid" to drop the blur.

Examples

Follow a promise

promise shows the loading toast and swaps it for the outcome.

Anchored to its button

With an AnchoredToaster, pass positionerProps with the element to point at.

API reference

Toaster and AnchoredToaster

Both take Base UI's Toast.Viewport props and render the toasts in a portal.

PropTypeDefaultDescription
variant"glass" | "solid""glass"With or without the blur behind each toast.
showArrowbooleantrueAnchoredToaster only: points the toast at its anchor.
classNamestringMerged onto the viewport.

toasts.add

The manager is Base UI's. These are the options this registry's toasts read.

OptionTypeDescription
title / descriptionReactNodeWhat it says.
type"success" | "info" | "warning" | "error" | "loading"Its icon, and for error the title's color.
timeoutnumberHow long it stays, in ms. 0 keeps it until it is closed.
priority"low" | "high"high is announced at once to screen readers.
actionPropsButtonPropsAdds a button, like Undo.
positionerProps{ anchor, side, align }Where an anchored toast points.

Parts

ExportNotes
ToastProviderHolds the toasts. Pass toastManager to add them from outside React.
createToastManager / useToastManagerMake a manager, or reach the provider's from a component.
Toast, ToastContent, ToastTitle, ToastDescription, ToastAction, ToastClose, ToastIconThe pieces the toasters are built from, for a toaster of your own.
ToastPortal, ToastViewport, ToastPositioner, ToastArrowWhere toasts render, and how an anchored one is placed.