A brief message that confirms an action or reports its result, then gets out of the way.
import { Toast, useToastManager } from '@oztix/roadie-components/toast'
RoadieProvider mounts Toast.Provider and a Toast.Viewport at your app root, so toasts work once you've followed Installation. The viewport portals to the end of the page. Toasts stack at the bottom end of wide screens and span the bottom of small ones. See Position to change that.
<RoadieProvider link={NextLink} toast={{ position: 'top-end' }}>{children}</RoadieProvider>
Placing providers yourself? Mount one Toast.Provider at the root with a Toast.Viewport inside it.
toast takes Toast.Provider's timeout (5000ms by default) and limit (3 on screen by default) for every toast in the app. This site mounts one, so the examples below work as they would in your app.
Call useToastManager() in any component under the provider, then add a toast. A title on its own is often enough. A thin bar along the bottom shows how long the toast has left.
Pass intent to colour the toast and lead with a matching icon. Without one it stays neutral, with no icon.
Set position once for the whole app, in RoadieProvider's toast or on Toast.Viewport: bottom-end (the default), bottom-center, top-end or top-center. end follows the reading direction. On small screens toasts always span the chosen edge. Top toasts slide in from the top, stack downwards and swipe up to dismiss.
These buttons each use their own provider so you can compare them. Your app mounts one.
A fixed bar, cookie banner or chat button can cover the toasts. Set --toast-viewport-offset-bottom to its height, on Toast.Viewport or any ancestor, and the stack moves up by that much. Top positions read --toast-viewport-offset-top. Reach for the offset before moving toasts to the top.
In a Navigator app, top toasts already clear the tallest Pane.Header along the top of the window and follow it as it collapses. Set --toast-viewport-offset-top only for other fixed UI; it adds to that.
In your own CSS you can set it wherever the bar appears:
:root:has([data-checkout-bar]) {--toast-viewport-offset-bottom: 5rem;}
A toast stays for the provider's timeout, 5000ms by default, or its own timeout if you pass one. The bar along its bottom empties in that time. Hovering or focusing the toasts pauses every timer and the bar with it, and they carry on from the same point afterwards. They also pause while the window is in the background. F6 moves focus to the toasts from anywhere on the page.
timeout: 0 keeps a toast until it's dismissed and drops the bar. So does a promise toast while it's loading. It starts its timer when the work settles.
actionProps adds a small button. It takes any button props, with the label as children. Keep it to one short verb, such as Undo. A toast with a convenience action like Undo still times out, but give it longer to read and reach, about 8000ms.
When the action is the only way forward, such as Retry, set timeout: 0 so the toast waits for them. If the problem needs more than a retry, show a Callout next to it instead.
promise shows a spinner while the work runs, then swaps in the success or error toast. It returns the promise, so you can still await the result.
If the work fails because something needs fixing, such as missing event details, show a Callout next to the problem instead. It stays until the problem is fixed.
add returns an id. Pass it to update to change the toast in place, or to close to dismiss it.
createToastManager makes a manager you can call from anywhere, such as a fetch wrapper or a store. Pass it to Toast.Provider. It has the same add, update, close and promise as the hook.
Toast.Viewport renders every toast for you. To lay a toast out differently, pass your own list as children and build each one from its parts. Toast.Title, Toast.Description and Toast.Action fill themselves from the toast, Toast.Icon picks the intent's icon, and Toast.Progress draws the time-left bar.
Do
Use a toast to confirm something the person just did, or to report how background work finished. Say what happened in a few words: "Link copied", or "Saved" with Undo.
Don’t
Don't put anything the person must see or act on in a toast. It disappears on its own. If an event can't publish until details are fixed, show a Callout next to the problem. Use a Dialog for a decision and inline errors for a form.
Do
Keep the default bottom end unless fixed UI collides with it. Try --toast-viewport-offset-bottom first, and only move to the top if the offset can't clear it.
Don’t
Don't change the position per page or per toast. People learn where to look.
timeout: 8000,actionProps: { children: 'Undo', onClick: undo }
Do
Offer one quick follow-up, like Undo. Let it time out, with about 8000ms so there's time to reach it, and make sure the change can also be reversed somewhere else, such as a bin or history. Hovering or focusing the toast pauses it for anyone who needs longer.
Don’t
Don't stack several buttons in a toast. If the action is the only way forward, like Retry, View order or Update card, set timeout: 0, or better, show a Callout next to the problem.
Do
Use success for work that finished, danger for work that failed, and warning for something that needs attention soon. Leave simple confirmations neutral.
Don’t
Don't use danger for validation. Show that next to the field.
priority: 'high' for an urgent message so it is announced straight away.aria-label to Toast.Close to change it.function useToastManager(): UseToastManagerReturnValue
Reads the nearest Toast.Provider. createToastManager() returns the same methods, without toasts.
| Field | Type | Description |
|---|---|---|
toasts | ToastObject[] | The toasts on screen, newest first. |
add | (options: ToastAddOptions) => string | Show a toast. Returns its id. |
update | (id: string, options: ToastUpdateOptions) => void | Change a toast in place. |
close | (id?: string) => void | Dismiss one toast, or all of them without an id. |
promise | (promise, { loading, success, error }) => Promise | Show a spinner until promise settles, then its result. |
add takes title, description, intent, actionProps, timeout, priority, onClose and data. Pass an existing id to replace that toast.
The toast to render.
Direction(s) in which the toast can be swiped to dismiss.
Defaults to ['down', 'right'].
CSS class applied to the element, or a function that returns a class based on the component's state.
A small Button built from the toast's `actionProps`. Renders nothing without a label.
CSS class applied to the element, or a function that returns a class based on the component's state.
Inherited from NativeButtonProps
Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).
Defaults to true.
An icon button that dismisses the toast.
CSS class applied to the element, or a function that returns a class based on the component's state.
Inherited from NativeButtonProps
Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).
Defaults to true.
Lays out a toast's parts in a row. Fades out while stacked behind the front toast.
No additional props. It forwards all standard HTML attributes to the underlying element.
Shows the toast's `description` unless you pass children.
No additional props. It forwards all standard HTML attributes to the underlying element.
The toast's intent icon, or a spinner while loading. Pass children to use your own icon.
No additional props. It forwards all standard HTML attributes to the underlying element.
A thin bar along the bottom of a timed toast that empties as its time runs out. It pauses whenever the toast's timer does: while the toasts are hovered or focused, or the window is in the background. Renders nothing for a toast that won't close on its own.
No additional props. It forwards all standard HTML attributes to the underlying element.
Holds the app's toasts. Mount once at the root.
A manager from `createToastManager`, to add toasts from outside React.
Inherited from ToastProviderProps
The default amount of time (in ms) before a toast is auto dismissed. A value of `0` will prevent the toast from being dismissed automatically.
Defaults to 5000.
The maximum number of toasts that can be displayed at once. When the limit is exceeded, the oldest toasts are marked as `limited` (via the `data-limited` attribute) rather than removed, so they can be hidden or animated out.
Defaults to 3.
Shows the toast's `title` unless you pass children.
No additional props. It forwards all standard HTML attributes to the underlying element.
The fixed region toasts stack in, at `position` on wider screens and full width along the same edge on small ones. Set `--toast-viewport-offset-bottom` (or `-top`) on it or an ancestor to clear fixed UI. Renders every toast unless you pass children.
CSS class applied to the element, or a function that returns a class based on the component's state.
Where the viewport portals to.
Defaults to document.body.
The edge toasts stack at, set once for the whole app. `end` follows the reading direction. Small screens always span the chosen edge.
Defaults to bottom-end.