@biem/ui

Toast

A short message that reports the result of an action and closes by itself.

A toast reports the result of an action in a small panel above the page, so users stay in their task. It appears at the bottom of the screen, in the right corner on a wide viewport, and closes by itself after five seconds. Users close it earlier with the close button, with a swipe down or to the right, or with the Escape key while focus is inside it. Several toasts form a stack, which expands and pauses the timeouts while the pointer rests on it or keyboard focus is inside it. Toaster is rendered once around the app, and the components inside it add toasts with useToastManager.

Usage

toast-default.tsx
"use client";

import { Button } from "@biem/ui/components/ui/button";
import { Toaster, useToastManager } from "@biem/ui/components/ui/toast";

function TransferButton() {
	const toastManager = useToastManager();

	function sendTransfer() {
		toastManager.add({
			title: "Transfer sent",
			description: "The warehouse ships 12 cases on Monday.",
		});
	}

	return <Button variant="outline" onClick={sendTransfer}>Send transfer</Button>;
}

export function ToastDefault() {
	return (
		<Toaster>
			<TransferButton/>
		</Toaster>
	);
}

Examples

Info

Not every message reports a success or a problem, and some simply keep users up to date on work that continues elsewhere. With type set to "info", the toast marks its message as neutral information, so users take note of it without feeling asked to act.

Success

After a completed action, users want to know that it went through before they move on. Setting type to "success" marks the toast as that confirmation, and users recognize the positive outcome at a glance.

Warning

Between success and failure lie results that need attention soon without stopping the task. A toast with type set to "warning" flags such a result, so users notice it early and decide when to deal with it.

Error

When an action fails, users need to know at once that their step had no effect. Through type set to "error", the toast reports the failure as such, leaving the description to say what went wrong and what users can try next.

Loading

Work that users start does not always finish at once, and its end is often unknown in advance. Set to "loading", type keeps the toast open without a timeout and shows that the work is still running, so users keep track of it while they continue their task. Once the work ends, code updates the toast with the outcome or closes it.

With Action

Many results invite a direct response, such as reversing a step that users took too quickly. Through actionProps the toast offers that response as an action of its own, so users act where the message appears instead of searching the page for the matching control.

Disabled Action

An action is sometimes unavailable for a moment, for example until the step before it has finished. As long as actionProps contain disabled, the toast keeps the action in view, so users know it exists, but neither a click nor the keyboard triggers it.

Promise

Requests to a server take a moment and end either in success or in failure. Passed to promise, the request gets a single toast that reports the work while it runs and turns into a confirmation or an error once the request settles, so users learn the outcome in the place where they saw it begin.

Custom Timeout

Five seconds suffice for a short confirmation, but a message with details that users want to note needs more time. Given a timeout of its own in milliseconds, such a toast stays open for exactly that time, while every other toast keeps the timeout of the Toaster.

Without Timeout

A message that asks users to do something later must not vanish before they have read it. With timeout set to 0, the toast stays open until users close it themselves.

Deduplicated

Repeated actions can stack the same message several times when users trigger them in quick succession. Under a fixed id, the toast that is still open updates in place and restarts its timeout, so users see one message that stays current.

Update from Code

Over the course of a process, its message changes from work in progress to a result. Through update, code gives the open toast with a given id new content and a new type, so users follow the process in one place instead of in a new toast for every step. As soon as the type is no longer "loading", the toast closes by itself after its timeout.

Close from Code

A message about running work stays open until that work ends, and sometimes the work stops before it finishes. Called with the id of the toast, close takes the message away at once, so users never rely on a status that no longer applies. Without an id, close ends every open toast.

Limit

Several actions in quick succession can fill the screen with messages and cover the content users work with. Here, limit set to 1 keeps only the newest toast in view, while older ones stay open with their timeouts running and return as newer toasts close.

Custom Toast

Some results need more than a title and a description, such as values that users compare with what they expected. Composed from ToastProvider and the parts of the toast instead of Toaster, a list of its own decides what every toast contains. Values that belong to a single toast travel in data, so each message holds exactly what users need to judge the result.

Swipe Direction

Where a vertical gesture already has a meaning, such as scrolling on a touch screen, a swipe down should not dismiss a message by accident. Set to "right", swipeDirection lets users close the toast only with a swipe to the right, and a swipe in any other direction leaves it open.

Disabled Close

Like any control, the close button can be unavailable for a moment. Marked as disabled, it stays in view but no longer closes the toast, while the timeout, a swipe and the Escape key still do.

API Reference

Toaster

ProptoastManager
TypeToastManager
Defaulttoast
DescriptionSets the manager whose toasts the toaster shows, next to the toasts that the components inside it add with useToastManager. Create a manager with createToastManager.
Proptimeout
Typenumber
Default5000
DescriptionSets the time in milliseconds after which a toast closes by itself. With 0, toasts stay open until users close them. A toast with a timeout of its own ignores this value.
Proplimit
Typenumber
Default3
DescriptionSets how many toasts are visible at once. Older toasts beyond the limit stay open but hidden, with their timeouts running, and come back into view as newer toasts close.
Propchildren
TypeReactNode
Default–
DescriptionHolds the app or the components that add toasts with useToastManager.

useToastManager

useToastManager returns the toasts of the nearest Toaster or ToastProvider and the functions that manage them. toasts is the array of open toasts, newest first, which a custom list passes to Toast one by one. add shows a toast with the options below and returns its id. update takes an id and the options to replace, or a function that receives the current toast and returns them, and it accepts every option except id. close closes the toast with the given id, or all toasts when it is called without one. promise takes a promise and the options for loading, success and error: it shows a toast of the type "loading", turns it into "success" or "error" when the promise settles, and returns a promise that settles in the same way. Each of the three is a string, which becomes the description, or an object with the options that update accepts, and success and error also accept a function that receives the result or the error and returns one of the two. It is a hook, so it renders no element and has no props of its own. Outside Toaster and ToastProvider, it throws an error.

Propid
Typestring
Default–
DescriptionIdentifies the toast for update and close. Without it, add creates an id. Adding a toast with the id of an open toast updates that toast in place and restarts its timeout instead of opening another one.
Proptitle
TypeReactNode
Default–
DescriptionSets the title, which names what happened.
Propdescription
TypeReactNode
Default–
DescriptionSets the description, which says what follows or what users can do next.
Proptype
Typestring
Default–
DescriptionNames the kind of the toast. Toaster shows an icon for "info", "success", "warning", "error" and "loading", and no icon for any other value. The icon of "error" has the destructive color, and the icon of "loading" spins. A toast of the type "loading" doesn't close by itself.
Proptimeout
Typenumber
Default–
DescriptionSets the time in milliseconds after which the toast closes by itself. With 0, it stays open until users close it. Without it, the timeout of the Toaster or the ToastProvider applies.
Proppriority
Type"low" | "high"
Default"low"
DescriptionSets how urgently screen readers announce the toast. With "low", they announce it when they are idle. With "high", they announce it at once.
PropactionProps
TypeComponentPropsWithoutRef<"button">
Default–
DescriptionSets the props of the action button, such as children for its label, onClick and disabled. Toaster shows the action only when these props contain children.
ProponClose
Type() => void
Default–
DescriptionRuns when the toast starts to close, whether its timeout ran out, users closed it or code closed it.
ProponRemove
Type() => void
Default–
DescriptionRuns when the toast is removed from toasts, after its closing animation has finished.
Propdata
TypeData
Default–
DescriptionCarries custom values to a custom list, which reads them from the entry in toasts. Pass the type of the values to useToastManager as its type argument. Toaster doesn't use data.

toast

toast is the manager whose toasts a Toaster shows when it receives no toastManager. It has the functions add, update, close and promise that useToastManager returns, but not toasts, and it works outside a component, for example in a function that handles a failed request. Every Toaster without a toastManager shows its toasts, so an app that adds toasts with toast renders exactly one such Toaster. It is an object, so it renders no element and has no props of its own.

createToastManager

createToastManager returns a new manager with the functions add, update, close and promise. Pass it to toastManager of a Toaster or a ToastProvider, which then shows the toasts that code adds through the manager. It is a function, so it renders no element and has no props of its own, and it takes no arguments.

ToastProvider

ProptoastManager
TypeToastManager
Default–
DescriptionSets a manager whose toasts the provider holds, next to the toasts that the components inside it add with useToastManager. Unlike Toaster, the provider uses no manager unless this prop is set.
Proptimeout
Typenumber
Default5000
DescriptionSets the time in milliseconds after which a toast closes by itself. With 0, toasts stay open until users close them. A toast with a timeout of its own ignores this value.
Proplimit
Typenumber
Default3
DescriptionSets how many toasts are visible at once. Toast hides the older toasts beyond the limit, and they come back into view as newer toasts close.
Propchildren
TypeReactNode
Default–
DescriptionHolds the components that add toasts with useToastManager and the ToastPortal with the custom list. The provider itself renders no element.

ToastPortal

Propcontainer
TypeHTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | null
Default–
DescriptionSets the element that the portal is rendered into. Without it, the portal is appended to the body.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the portal with another component.

ToastViewport

Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the viewport with another component.

Toast

Proptoast
TypeToastObject
Default–
DescriptionSets the toast that the part shows. Pass one entry of toasts, which useToastManager returns. The part requires this prop.
PropswipeDirection
Type"up" | "down" | "left" | "right" | ("up" | "down" | "left" | "right")[]
Default["down", "right"]
DescriptionSets the directions in which users swipe the toast to close it.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the toast with another component.

ToastContent

Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the content with another component.

ToastTitle

Propchildren
TypeReactNode
Default–
DescriptionReplaces the title that the toast was added with. Without both, the part renders nothing.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the title with another component.

ToastDescription

Propchildren
TypeReactNode
Default–
DescriptionReplaces the description that the toast was added with. Without both, the part renders nothing.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the description with another component.

ToastAction

Propchildren
TypeReactNode
Default–
DescriptionSets the label of the action for a toast whose actionProps contain no children. Without both, the part renders nothing.
Proprender
TypeReactElement | render function
Default<Button variant="outline" size="sm"/>
DescriptionReplaces the Button that the action renders or composes the action with another component.
PropnativeButton
Typeboolean
Defaulttrue
DescriptionDeclares whether the rendered element is a native button. Set it to false when render returns a different element.
Propdisabled
Typeboolean
Defaultfalse
DescriptionPrevents users from triggering the action.

ToastClose

Propchildren
TypeReactNode
Default<IconX/>
DescriptionReplaces the X icon of the close button. In both cases, the button carries the label "Close toast" for screen readers.
Proprender
TypeReactElement | render function
Default<Button variant="ghost" size="icon-sm"/>
DescriptionReplaces the Button that the close button renders or composes the close button with another component.
PropnativeButton
Typeboolean
Defaulttrue
DescriptionDeclares whether the rendered element is a native button. Set it to false when render returns a different element.
Propdisabled
Typeboolean
Defaultfalse
DescriptionPrevents users from closing the toast with this button.