Toast
Generates toast notifications.
Usage
<Toast.Provider>
can be wrapped around your entire app, ensuring all toasts are rendered in the same viewport.- F6 lets users jump into the toast viewport landmark region to navigate toasts with keyboard focus.
API reference
Import the component and assemble its parts:
import { Toast } from '@base-ui-components/react/toast';
<Toast.Provider>
<Toast.Viewport>
<Toast.Root>
<Toast.Title />
<Toast.Description />
<Toast.Action />
<Toast.Close />
</Toast.Root>
</Toast.Viewport>
</Toast.Provider>
Provider
Provides a context for creating and managing toasts.
Prop | Type | Default | |
---|---|---|---|
limit |
|
| |
toastManager |
|
| |
timeout |
|
|
Viewport
A container viewport for toasts.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-expanded | Indicates toasts are expanded in the viewport. |
Root
Groups all parts of an individual toast.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
swipeDirection |
|
| |
toast |
|
| |
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-expanded | Present when the toast is expanded in the viewport. | |
data-limited | Present when the toast was removed due to exceeding the limit. | |
data-swipe-direction | The direction the toast was swiped. | |
data-swiping | Present when the toast is being swiped. | |
data-type | The type of the toast. | |
data-starting-style | Present when the toast is animating in. | |
data-ending-style | Present when the toast is animating out. |
CSS Variable | Description | |
---|---|---|
--toast-index | Indicates the index of the toast in the list. | |
--toast-offset-y | Indicates the vertical pixels offset of the toast in the list when expanded. | |
--toast-swipe-movement-x | Indicates the horizontal swipe movement of the toast. | |
--toast-swipe-movement-y | Indicates the vertical swipe movement of the toast. |
Title
A title that labels the toast.
Renders an <h2>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-type | The type of the toast. |
Description
A description that describes the toast.
Can be used as the default message for the toast when no title is provided.
Renders a <p>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-type | The type of the toast. |
Action
Performs an action when clicked.
Renders a <button>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-type | The type of the toast. |
Close
Closes the toast when clicked.
Renders a <button>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-type | The type of the toast. |
useToastManager
Manages toasts, called inside of a Toast.Provider
.
const toastManager = Toast.useToastManager();
Return value
Prop | Type | |
---|---|---|
toasts |
| |
add |
| |
close |
| |
update |
| |
promise |
|
Method options
Prop | Type | Default | |
---|---|---|---|
title |
|
| |
description |
|
| |
type |
|
| |
timeout |
|
| |
priority |
|
| |
onClose |
|
| |
onRemove |
|
| |
actionProps |
|
| |
data |
|
|
add
method
Creates a toast by adding it to the toast list.
Returns a toastId
that can be used to update or close the toast later.
const toastId = toastManager.add({
description: 'Hello, world!',
});
function App() {
const toastManager = Toast.useToastManager();
return (
<button
type="button"
onClick={() => {
toastManager.add({
description: 'Hello, world!',
});
}}
>
Add toast
</button>
);
}
The title
and description
strings are what are used to announce the toast to screen readers. Screen readers do not announce any extra content rendered inside Toast.Root
, including the Toast.Title
or Toast.Description
components.
update
method
Updates the toast with new options.
toastManager.update(toastId, {
description: 'New description',
});
close
method
Closes the toast, removing it from the toast list after any animations complete.
toastManager.close(toastId);
promise
method
Creates an asynchronous toast with three possible states: loading
, success
, and error
.
const toastId = toastManager.promise(
new Promise((resolve) => {
setTimeout(() => resolve('world!'), 1000);
}),
{
// Each are a shortcut for the `description` option
loading: 'Loading...',
success: (data) => `Hello ${data}`,
error: (err) => `Error: ${err}`,
},
);
Each state also accepts the method options object to granularly control the toast for each state:
const toastId = toastManager.promise(
new Promise((resolve) => {
setTimeout(() => resolve('world!'), 1000);
}),
{
loading: {
title: 'Loading...',
description: 'The promise is loading.',
},
success: {
title: 'Success',
description: 'The promise resolved successfully.',
},
error: {
title: 'Error',
description: 'The promise rejected.',
actionProps: {
children: 'Contact support',
onClick() {
// Redirect to support page
},
},
},
},
);
Global manager
A global toast manager can be created by passing the toastManager
prop to the Toast.Provider
. This enables you to queue a toast from anywhere in the app (such as in functions outside the React tree) while still using the same toast renderer.
The created toastManager
object has the same properties and methods as the Toast.useToastManager()
hook.
const toastManager = Toast.createToastManager();
<Toast.Provider toastManager={toastManager}>
Stacking and animations
The --toast-index
CSS variable can be used to determine the stacking order of the toasts. The 0th index toast appears at the front.
.Toast {
z-index: calc(1000 - var(--toast-index));
transform: scale(1 - calc(0.1 * var(--toast-index)));
}
The --toast-offset-y
CSS variable can be used to determine the vertical offset of the toasts when positioned absolutely with a translation offset — this is usually used with the data-expanded
attribute, present when the toast viewport is being hovered or has focus.
.Toast[data-expanded] {
transform: translateY(var(--toast-offset-y));
}
The --toast-swipe-movement-x
and --toast-swipe-movement-y
CSS variables are used to determine the swipe movement of the toasts in order to add a translation offset.
.Toast {
transform: scale(1 - calc(0.1 * var(--toast-index))) + translateX(var(--toast-swipe-movement-x)) +
translateY(calc(var(--toast-swipe-movement-y) + (var(--toast-index) * -20%)));
}
The data-swipe-direction
attribute can be used to determine the swipe direction of the toasts to add a translation offset upon dismissal.
&[data-ending-style] {
opacity: 0;
&[data-swipe-direction='up'] {
transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
}
&[data-swipe-direction='down'] {
transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
}
&[data-swipe-direction='left'] {
transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
}
&[data-swipe-direction='right'] {
transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
}
}
The data-limited
attribute indicates that the toast was removed from the list due to exceeding the limit
option. This is useful for animating the toast differently when it is removed from the list.
Examples
Custom position
The position of the toasts is controlled by your own CSS. To change the toasts’ position, you can modify the Viewport
and Root
styles. A more general component could accept a data-position
attribute, which the CSS handles for each variation. The following shows a top-center position:
Undo action
When adding a toast, the actionProps
option can be used to define props for an action button inside of it—this enables the ability to undo an action associated with the toast.
Promise
An asynchronous toast can be created with three possible states: loading
, success
, and error
. The type
string matches these states to change the styling. Each of the states also accepts the method options object for more granular control.
Custom
A toast with custom data can be created by passing any typed object interface to the data
option. This enables you to pass any data (including functions) you need to the toast and access it in the toast’s rendering logic.