Toast
Generates toast notifications.
View as MarkdownAnatomy
Import the component and assemble its parts:
General 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.
- The
data-base-ui-swipe-ignoreattribute can be manually added to elements inside of a toast to prevent swipe-to-dismiss gestures on them. Interactive elements are automatically prevented.
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.
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.
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.Content> is used to hide overflow from taller toasts while the stack is collapsed.
The data-behind attribute marks content that sits behind the frontmost toast and pairs with the data-expanded attribute so the content fades back in when the viewport expands:
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.
The data-swipe-direction attribute can be used to determine the swipe direction of the toasts to add a translation offset upon dismissal.
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.
The updateKey property increments when a toast is updated or upserted.
This can be used to replay attention-grabbing styles by switching animation names or, when remounting is acceptable, by including it in a React key.
Examples
Anchored toasts
Toasts can be anchored to a specific element using <Toast.Positioner> and the positionerProps option when adding a toast. This is useful for showing contextual feedback like transient “Copied” toasts that appear near the button that triggered the action.
Anchored toasts should be rendered in a separate <Toast.Provider> from stacked toasts. A global toast manager can be created for each to manage them separately throughout your app:
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.
Deduplicated toast
When you upsert the same toast by id, the updateKey property increments so a custom renderer can replay a visual animation. This demo alternates CSS animation names from updateKey, which keeps the same toast mounted while replaying the pulse.
Varying heights
Toasts with varying heights are stacked by ensuring that the <Toast.Content> element has overflow: hidden set, along with all toasts’ heights matching the frontmost toast at index 0.
This prevents taller toasts from overflowing the stack when collapsed.
API reference
Provider
Provides a context for creating and managing toasts.
limitnumber3
- Name
- Description
The maximum number of toasts that can be displayed at once. When the limit is reached, the oldest toast will be removed to make room for the new one.
- Type
- Default
3
toastManagerToastManager—
- Name
- Description
A global manager for toasts to use outside of a React component.
- Type
timeoutnumber5000
- Name
- Description
The default amount of time (in ms) before a toast is auto dismissed. A value of
0will prevent the toast from being dismissed automatically.- Type
- Default
5000
childrenReact.ReactNode—
- Name
- Type
Toast.Provider.StateHide
Viewport
A container viewport for toasts.
Renders a <div> element.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-expanded
Indicates toasts are expanded in the viewport.
| Attribute | Description | |
|---|---|---|
data-expanded | Indicates toasts are expanded in the viewport. | |
--toast-frontmost-height
Indicates the height of the frontmost toast.
| CSS Variable | Description | |
|---|---|---|
--toast-frontmost-height | Indicates the height of the frontmost toast. | |
Toast.Viewport.StateHide
Portal
A portal element that moves the viewport to a different part of the DOM.
By default, the portal element is appended to <body>.
Renders a <div> element.
containerUnion—
- Name
- Description
A parent element to render the portal element into.
- Type
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
Toast.Portal.StateHide
Root
Groups all parts of an individual toast.
Renders a <div> element.
swipeDirectionUnion['down', 'right']
- Name
- Description
Direction(s) in which the toast can be swiped to dismiss.
- Type
- Default
['down', 'right']
toast*—
- Name
- Description
The toast to render.
- Type
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
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.
| 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. | |
--toast-height
Indicates the measured natural height of the toast in pixels.
--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.
| CSS Variable | Description | |
|---|---|---|
--toast-height | Indicates the measured natural height of the toast in pixels. | |
--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. | |
Toast.Root.StateHide
Toast.Root.ToastObjectHide
Content
A container for the contents of a toast.
Renders a <div> element.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-behind
Present when the toast is behind the frontmost toast in the stack.
data-expanded
Present when the toast viewport is expanded.
| Attribute | Description | |
|---|---|---|
data-behind | Present when the toast is behind the frontmost toast in the stack. | |
data-expanded | Present when the toast viewport is expanded. | |
Toast.Content.StateHide
Title
A title that labels the toast.
Renders an <h2> element.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-type
The type of the toast.
| Attribute | Description | |
|---|---|---|
data-type | The type of the toast. | |
Toast.Title.StateHide
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.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-type
The type of the toast.
| Attribute | Description | |
|---|---|---|
data-type | The type of the toast. | |
Toast.Description.PropsHide
Re-Export of Description props as ToastDescriptionProps
Toast.Description.StateHide
Action
Performs an action when clicked.
Renders a <button> element.
nativeButtonbooleantrue
- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set tofalseif the rendered element is not a button (for example,<div>).- Type
- Default
true
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-type
The type of the toast.
| Attribute | Description | |
|---|---|---|
data-type | The type of the toast. | |
Toast.Action.StateHide
Close
Closes the toast when clicked.
Renders a <button> element.
nativeButtonbooleantrue
- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set tofalseif the rendered element is not a button (for example,<div>).- Type
- Default
true
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-type
The type of the toast.
| Attribute | Description | |
|---|---|---|
data-type | The type of the toast. | |
Toast.Close.StateHide
Positioner
Positions the toast against the anchor.
Renders a <div> element.
disableAnchorTrackingbooleanfalse
- Description
Whether to disable the popup from tracking any layout shift of its positioning anchor.
- Type
- Default
false
toast*—
- Name
- Description
The toast object associated with the positioner.
- Type
alignAlign'center'
- Name
- Description
How to align the popup relative to the specified side.
- Type
- Default
'center'
alignOffsetnumber | OffsetFunction0
- Name
- Description
Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a
dataobject parameter with the following properties:data.anchor: the dimensions of the anchor element with propertieswidthandheight.data.positioner: the dimensions of the positioner element with propertieswidthandheight.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side.
- Type
- Default
0- Example
sideSide'top'
- Name
- Description
Which side of the anchor element to align the toast against. May automatically change to avoid collisions.
- Type
- Default
'top'
sideOffsetnumber | OffsetFunction0
- Name
- Description
Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a
dataobject parameter with the following properties:data.anchor: the dimensions of the anchor element with propertieswidthandheight.data.positioner: the dimensions of the positioner element with propertieswidthandheight.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side.
- Type
- Default
0- Example
arrowPaddingnumber5
- Name
- Description
Minimum distance to maintain between the arrow and the edges of the popup.
Use it to prevent the arrow element from hanging out of the rounded corners of a popup.
- Type
- Default
5
anchorElement | null—
- Name
- Description
An element to position the toast against.
- Type
collisionAvoidanceCollisionAvoidance—
- Description
Determines how to handle collisions when positioning the popup.
sidecontrols overflow on the preferred placement axis (top/bottomorleft/right):'flip': keep the requested side when it fits; otherwise try the opposite side (topandbottom, orleftandright).'shift': never change side; keep the requested side and move the popup within the clipping boundary so it stays visible.'none': do not correct side-axis overflow.
aligncontrols overflow on the alignment axis (start/center/end):'flip': keep side, but swapstartandendwhen the requested alignment overflows.'shift': keep side and requested alignment, then nudge the popup along the alignment axis to fit.'none': do not correct alignment-axis overflow.
fallbackAxisSidecontrols fallback behavior on the perpendicular axis when the preferred axis cannot fit:'start': allow perpendicular fallback and try the logical start side first (topbeforebottom, orleftbeforerightin LTR).'end': allow perpendicular fallback and try the logical end side first (bottombeforetop, orrightbeforeleftin LTR).'none': do not fallback to the perpendicular axis.
When
sideis'shift', explicitly settingalignonly supports'shift'or'none'. Ifalignis omitted, it defaults to'flip'.- Type
- Example
collisionBoundaryBoundary'clipping-ancestors'
- Description
An element or a rectangle that delimits the area that the popup is confined to.
- Type
- Default
'clipping-ancestors'
collisionPaddingPadding5
- Name
- Description
Additional space to maintain from the edge of the collision boundary.
- Type
- Default
5
stickybooleanfalse
- Name
- Description
Whether to maintain the popup in the viewport after the anchor element was scrolled out of view.
- Type
- Default
false
positionMethod'absolute' | 'fixed''absolute'
- Name
- Description
Determines which CSS
positionproperty to use.- Type
- Default
'absolute'
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-anchor-hidden
Present when the anchor is hidden.
data-align
Indicates how the toast is aligned relative to specified side.
data-side
Indicates which side the toast is positioned relative to the trigger.
| Attribute | Description | |
|---|---|---|
data-anchor-hidden | Present when the anchor is hidden. | |
data-align | Indicates how the toast is aligned relative to specified side. | |
data-side | Indicates which side the toast is positioned relative to the trigger. | |
--anchor-height
The anchor’s height.
--anchor-width
The anchor’s width.
--available-height
The available height between the anchor and the edge of the viewport.
--available-width
The available width between the anchor and the edge of the viewport.
--transform-origin
The coordinates that this element is anchored to. Used for animations and transitions.
| CSS Variable | Description | |
|---|---|---|
--anchor-height | The anchor’s height. | |
--anchor-width | The anchor’s width. | |
--available-height | The available height between the anchor and the edge of the viewport. | |
--available-width | The available width between the anchor and the edge of the viewport. | |
--transform-origin | The coordinates that this element is anchored to. Used for animations and transitions. | |
Toast.Positioner.PropsHide
Re-Export of Positioner props as ToastPositionerProps
Toast.Positioner.StateHide
Arrow
Displays an element positioned against the toast anchor.
Renders a <div> element.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-uncentered
Present when the toast arrow is uncentered.
data-align
Indicates how the toast is aligned relative to specified side.
data-side
Indicates which side the toast is positioned relative to the anchor.
| Attribute | Description | |
|---|---|---|
data-uncentered | Present when the toast arrow is uncentered. | |
data-align | Indicates how the toast is aligned relative to specified side. | |
data-side | Indicates which side the toast is positioned relative to the anchor. | |
Toast.Arrow.StateHide
useToastManager
Manages toasts, called inside of a <Toast.Provider>.
Return value
UseToastManagerReturnValue
toasts
- Name
- Type
addfunction
- Name
- Type
closefunction
- Name
- Type
updatefunction
- Name
- Type
promisefunction
- Name
- Type
add method
Creates a toast by adding it to the toast list.
If you pass an id that already exists, the existing toast is updated in place instead of creating a duplicate.
Returns a toastId that can be used to update or close the toast later.
For high priority toasts, 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, unless they intentionally navigate to the toast viewport.
update method
Updates the toast with new options.
close method
Closes the toast, removing it from the toast list after any animations complete.
Or you can close all toasts at once by not passing an ID:
promise method
Creates an asynchronous toast with three possible states: loading, success, and error.
Each state also accepts the method options object to granularly control the toast for each state:
See full promise method