Toast
A short notice that goes away on its own.
Put one Toaster near the app’s root, then call toast() from anywhere when something finishes.
A toast can offer an action, like Undo. Toasts stack, fan out when you hover or focus them, and
can be swiped away. Screen readers announce them without moving focus.
Dependencies
What it's built from and what's built from it in Mocha, read from the imports in its source.
| Uses | ButtonprimitiveTextprimitive |
|---|---|
| Used by | Nothing yet. |
| Packages | @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20lucide-react@1.73.0 |
Installation
Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.
With the CLI
Manually
Install its packages:
Add the components it uses first, listed under Dependencies.
Then copy the source:
API reference
Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.
Toast
A toast's card: a stack that peeks behind the newest toast and fans out on hover or focus.
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: ToastState) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: ToastState) => string)
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<ToastState>
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: ToastState) => CSSProperties)
ToastAction
A button for the toast's action, like Undo. Empty, it shows the action's label.
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: ToastButtonState) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: ToastButtonState) => string)
- disabled
Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless
focusableWhenDisabledis set.Type boolean · Default false
- dismiss
Whether pressing it dismisses the toast.
Type boolean · Default the toast's `action.dismiss`, else true
- onAction
Runs when it's pressed, instead of the toast's own
action.onAction. A returned Promise marks it pending.details.sourcesays how it was pressed, for changes made after anawait.Type (event: Event, details: ActionDetails) => unknown
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<ToastButtonState>
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: ToastButtonState) => CSSProperties)
ToastClose
Dismisses the toast.
- action
Runs before the toast closes. A returned Promise keeps it open and the button pending until it settles.
details.sourcesays how it was pressed, for changes made after anawait.Type (event: Event, details: ActionDetails) => unknown
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: ToastButtonState) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: ToastButtonState) => string)
- disabled
Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless
focusableWhenDisabledis set.Type boolean · Default false
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<ToastButtonState>
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: ToastButtonState) => CSSProperties)
ToastDescription
More detail, under the title. Empty, it shows the toast's description.
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: {}) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: {}) => string)
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<{}>
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: {}) => CSSProperties)
Toaster
Where toasts appear: the bottom right unless side and align say otherwise. Put one near the app's root. Pass toaster for a queue of its own, from createToaster().
- align
Where along that edge.
"start"and"end"follow the writing direction, and swiping toward them dismisses too.Type "center" | "start" | "end" · Default end
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: ToastRegionState) => string)
- container
Where the region renders. Without top layer support, the body.
Type PopupContainer
- debug
Logs every state change with its reason to the console, in development builds.
Type boolean
- expanded
Whether the stack is always expanded. Otherwise it expands while the pointer or focus is in it.
Type boolean · Default false
- maxVisible
How many toasts show at once. Older ones wait, with their timers stopped, until there's room.
Type number · Default 3
- pendingAnnouncement
What screen readers hear once a toast's action or close button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
Type string · Default "Loading"
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<ToastRegionState>
- shortcut
The key that moves focus into the region and back, like
"F6"or"alt+t".nullturns it off.Type string | null · Default "F6"
- side
The edge of the viewport it sits on. Swiping toward it dismisses a toast.
Type "top" | "bottom" · Default bottom
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: ToastRegionState) => CSSProperties)
- toaster
The queue it shows. A second toaster, from
createToaster(), gives a second region its own toasts.Type Toaster<any> · Default the shared `toast`
ToastTitle
The toast's message. Empty, it shows the text passed to toast().
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: {}) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: {}) => string)
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<{}>
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: {}) => CSSProperties)