Toast

A 3D toaster whose slice of bread is a real Toast: the lever runs your work, and the slice pops with its result. Built with ui-sdk (opens in a new tab).

    Installation

    pnpm dlx shadcn@latest add @alexandrejoly/toast

    Usage

    import { createToaster } from "@ui-sdk/react/toast";
    import {  Toast,  ToastDial,  ToastLever,  ToastRegion,  ToastStage,  ToastStop,} from "@/components/ui/literal-ui/toast/toast";import { ToastAudio } from "@/components/ui/literal-ui/toast/toast-sound";
    const toaster = createToaster();
    <Toast onToast={() => "Toast is ready"} toaster={toaster}>  <ToastStage>    <ToastRegion />    <ToastDial />    <ToastStop />    <ToastLever />  </ToastStage>  <ToastAudio /></Toast>

    Examples

    Async work

    onToast can await real work. The lever stays down until it settles: a resolved title pops as a success, and a rejection pops its message as an error, every second save here. Stop aborts signal.

      Controlled

      Set toasting to start a cycle from your own code. Here the form pushes the lever down, and the slice pops with your message three seconds later. The lever, the dial and Stop still work by hand, and onToastingChange reports each start and each end.

        API reference

        Toast

        Holds the cycle, the slice and the browning level. The slice is a toast in your toaster.

        Takes every prop of div.

        PropTypeDefault
        toaster

        From createToaster(). Give it to this toaster alone: every toast it holds shows in the slot.

        ToasterNone
        onToast

        Runs once the catch holds the lever. Return a title for a success, or toast options such as { type: "error", title }. A throw shows its message as an error. Return nothing to keep what you set with toaster.update(id, …).

        (cycle: ToastCycle) => ToastOutcome | void | Promise<ToastOutcome | void>None
        canToast

        Asked as the lever reaches the catch. Return false and the lever springs back up.

        () => booleanNone
        defaultLevel

        The dial's browning level: how long a cycle lasts and how much it browns the slice.

        Default: 3

        1 | 2 | 3 | 4 | 53
        level

        The browning level, controlled.

        1 | 2 | 3 | 4 | 5None
        onLevelChange

        Runs when the dial turns to a new level.

        (level: ToastLevel) => voidNone
        defaultToasting

        Starts a cycle on mount, as if the lever had been pushed down.

        Default: false

        booleanfalse
        toasting

        Whether a cycle runs, controlled. Set it to true to push the lever down and start a cycle, and to false to stop it, as Stop does.

        booleanNone
        onToastingChange

        Runs when a cycle starts, by hand or from toasting, and when it ends, by a pop or by Stop.

        (toasting: boolean) => voidNone
        idleTitle

        The title of a fresh slice, for screen readers.

        Default: "Plain bread, ready to toast"

        string"Plain bread, ready to toast"
        toastingTitle

        The slice's title while a cycle runs.

        Default: "Toasting…"

        string"Toasting…"

        ToastCycle

        What onToast receives.

        FieldType
        id

        The slice's toast id in your toaster.

        string
        level

        The dial's level for this cycle.

        1 | 2 | 3 | 4 | 5
        browning

        How toasted the slice will be once the cycle ends, from 0 to 1. Browning adds up from one cycle to the next.

        number
        burnt

        Whether that browning reads as burnt.

        boolean
        signal

        Aborted when Stop ends the cycle. The slice then goes back to what it said.

        AbortSignal

        ToastStage

        The toaster in 3D, with the controls laid over its parts. It shows a still picture until the scene loads, and keeps it without WebGL 2, with the slice's title written on it.

        Takes every prop of div.

        ToastRegion

        The front slot, where the slice stands. Pull the slice up to take it out, and a fresh one drops in.

        Takes every prop of UI SDK ToastRegion (opens in a new tab).

        ToastDial

        The browning dial. Drag up or down, or use the arrow keys.

        Takes every prop of UI SDK Slider (opens in a new tab).

        PropTypeDefault
        aria-label

        Names the dial.

        Default: "Browning"

        string"Browning"

        ToastLever

        The lever. Drag it down to the catch, or press Enter, Space or the Down arrow.

        Takes every prop of UI SDK Slider (opens in a new tab).

        PropTypeDefault
        aria-label

        Names the lever.

        Default: "Lever"

        string"Lever"

        ToastStop

        Ends the cycle early. The slice keeps its browning and its previous title.

        Takes every prop of UI SDK Button (opens in a new tab).

        ToastAudio

        Plays the toaster's recorded sounds. It renders nothing, and loads nothing until the page is pressed. Import it from toast-sound.

        PropTypeDefault
        muted

        Silences this toaster alone.

        Default: false

        booleanfalse

        ToastAudioProvider

        Mutes every ToastAudio inside it, for a sound preference that covers the whole app or one part of it. It renders nothing of its own.

        PropType
        muted

        Silences the toasters below. Turned off during a press, their sounds unlock at once.

        boolean

        useToastEvents

        Follows what the toaster does as it happens, as ToastAudio does: the lever, the dial, Stop, each pop and each slice.

        ReturnsType
        subscribe

        Calls listener with each event. Returns the unsubscribe.

        (listener: (event: ToastEvent) => void) => () => void