Toast

Generates toast notifications.

View as Markdown

Anatomy

Import the component and assemble its parts:

Anatomy

API reference

Provider

Provides a context for creating and managing toasts.

limit

number

3

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
number | undefined
Default

3

toastManager

createToastManager.ToastManager

Description

A global manager for toasts to use outside of a React component.

Type
| {
    subscribe: (
      listener: (data: ToastManagerEvent) => void,
    ) => () => void
    add: (
      options: useToastManager.AddOptions<{}>,
    ) => string
    close: (id: string) => void
    update: (
      id: string,
      updates: useToastManager.UpdateOptions<{}>,
    ) => void
    promise: (
      promiseValue: Promise<Value>,
      options: useToastManager.PromiseOptions<Value, {}>,
    ) => Promise<Value>
  }
| undefined
timeout

number

5000

Description

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.

Type
number | undefined
Default

5000

children

ReactNode

Type
React.ReactNode

Viewport

A container viewport for toasts. Renders a <div> element.

style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((
    state: Toast.Viewport.State,
  ) => CSSProperties | undefined)
| undefined
className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
| string
| ((state: Toast.Viewport.State) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: Toast.Viewport.State,
  ) => ReactElement)
data-expanded

Indicates toasts are expanded in the viewport.

--toast-frontmost-height

Indicates the height of the frontmost toast.

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.

style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((state: any) => CSSProperties | undefined)
| undefined
container

Union

Description

A parent element to render the portal element into.

Type
| HTMLElement
| ShadowRoot
| React.RefObject<HTMLElement | ShadowRoot | null>
| null
| undefined
className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
string | ((state: any) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((props: HTMLProps, state: any) => ReactElement)

Root

Groups all parts of an individual toast. Renders a <div> element.

style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((state: Toast.Root.State) => CSSProperties | undefined)
| undefined
swipeDirection

Union

['down', 'right']

Description

Direction(s) in which the toast can be swiped to dismiss.

Type
| 'left'
| 'right'
| 'up'
| 'down'
| ('left' | 'right' | 'up' | 'down')[]
| undefined
Default

['down', 'right']

toast*

Toast.Root.ToastObject<any>

Name
Description

The toast to render.

Type
id: string
  ref?: RefObject<HTMLElement | null>
  title?: ReactNode
  type?: string
  description?: ReactNode
  timeout?: number
  priority?: 'high' | 'low'
  transitionStatus?: 'starting' | 'ending'
  limited?: boolean
  height?: number
  onClose?: () => void
  onRemove?: () => void
  actionProps?: Omit<
    DetailedHTMLProps<
      ButtonHTMLAttributes<HTMLButtonElement>,
      HTMLButtonElement
    >,
    'ref'
  >
  positionerProps?: Toast.Positioner.Options
  data?: any
}
className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
| string
| ((state: Toast.Root.State) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: Toast.Root.State,
  ) => ReactElement)
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.

Content

A container for the contents of a toast. Renders a <div> element.

style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((
    state: Toast.Content.State,
  ) => CSSProperties | undefined)
| undefined
className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
| string
| ((state: Toast.Content.State) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: Toast.Content.State,
  ) => ReactElement)
data-behind

Present when the toast is behind the frontmost toast in the stack.

data-expanded

Present when the toast viewport is expanded.

Title

A title that labels the toast. Renders an <h2> element.

style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((state: Toast.Title.State) => CSSProperties | undefined)
| undefined
className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
| string
| ((state: Toast.Title.State) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: Toast.Title.State,
  ) => ReactElement)
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.

style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((
    state: Toast.Description.State,
  ) => CSSProperties | undefined)
| undefined
className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
| string
| ((state: Toast.Description.State) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: Toast.Description.State,
  ) => ReactElement)
data-type

The type of the toast.

Action

Performs an action when clicked. Renders a <button> element.

nativeButton

boolean

true

Description

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 (e.g. <div>).

Type
boolean | undefined
Default

true

style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((state: Toast.Action.State) => CSSProperties | undefined)
| undefined
className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
| string
| ((state: Toast.Action.State) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: Toast.Action.State,
  ) => ReactElement)
data-type

The type of the toast.

Close

Closes the toast when clicked. Renders a <button> element.

nativeButton

boolean

true

Description

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 (e.g. <div>).

Type
boolean | undefined
Default

true

style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((state: Toast.Close.State) => CSSProperties | undefined)
| undefined
className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
| string
| ((state: Toast.Close.State) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: Toast.Close.State,
  ) => ReactElement)
data-type

The type of the toast.

Positioner

Positions the toast against the anchor. Renders a <div> element.

collisionAvoidance

CollisionAvoidance

Description

Determines how to handle collisions when positioning the popup.

Type
| {
    side?: 'none' | 'flip'
    align?: 'none' | 'flip' | 'shift'
    fallbackAxisSide?: 'none' | 'end' | 'start'
  }
| {
    side?: 'none' | 'shift'
    align?: 'none' | 'shift'
    fallbackAxisSide?: 'none' | 'end' | 'start'
  }
| undefined
Example
style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((
    state: Toast.Positioner.State,
  ) => CSSProperties | undefined)
| undefined
toast*

ToastObject<any>

Name
Description

The toast object associated with the positioner.

Type
id: string
  ref?: RefObject<HTMLElement | null>
  title?: ReactNode
  type?: string
  description?: ReactNode
  timeout?: number
  priority?: 'high' | 'low'
  transitionStatus?: 'starting' | 'ending'
  limited?: boolean
  height?: number
  onClose?: () => void
  onRemove?: () => void
  actionProps?: Omit<
    DetailedHTMLProps<
      ButtonHTMLAttributes<HTMLButtonElement>,
      HTMLButtonElement
    >,
    'ref'
  >
  positionerProps?: Toast.Positioner.Options
  data?: any
}
trigger

Element | null

Description

The trigger element for the toast. Falls back to the anchor element when not provided.

Type
Element | null | undefined
align

Align

'center'

Name
Description

How to align the popup relative to the specified side.

Type
'center' | 'end' | 'start' | undefined
Default

'center'

alignOffset

number | OffsetFunction

0

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 data object parameter with the following properties:

  • data.anchor: the dimensions of the anchor element with properties width and height.
  • data.positioner: the dimensions of the positioner element with properties width and height.
  • 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
| number
| ((data: {
    side: Side
    align: Align
    anchor: { width: number; height: number }
    positioner: { width: number; height: number }
  }) => number)
| undefined
Default

0

Example
side

Side

'top'

Name
Description

Which side of the anchor element to align the toast against. May automatically change to avoid collisions.

Type
| 'left'
| 'right'
| 'bottom'
| 'top'
| 'inline-end'
| 'inline-start'
| undefined
Default

'top'

sideOffset

number | OffsetFunction

0

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 data object parameter with the following properties:

  • data.anchor: the dimensions of the anchor element with properties width and height.
  • data.positioner: the dimensions of the positioner element with properties width and height.
  • 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
| number
| ((data: {
    side: Side
    align: Align
    anchor: { width: number; height: number }
    positioner: { width: number; height: number }
  }) => number)
| undefined
Default

0

Example
arrowPadding

number

5

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
number | undefined
Default

5

anchor

Element | null

Name
Description

An element to position the toast against.

Type
Element | null | undefined
collisionBoundary

Boundary

'clipping-ancestors'

Description

An element or a rectangle that delimits the area that the popup is confined to.

Type
| Element
| 'clipping-ancestors'
| Element[]
| Rect
| undefined
Default

'clipping-ancestors'

collisionPadding

Padding

5

Description

Additional space to maintain from the edge of the collision boundary.

Type
| {
    top?: number
    right?: number
    bottom?: number
    left?: number
  }
| number
| undefined
Default

5

sticky

boolean

false

Name
Description

Whether to maintain the popup in the viewport after the anchor element was scrolled out of view.

Type
boolean | undefined
Default

false

positionMethod

'fixed' | 'absolute'

'absolute'

Description

Determines which CSS position property to use.

Type
'fixed' | 'absolute' | undefined
Default

'absolute'

trackAnchor

boolean

true

Description

Whether the popup tracks any layout shift of its positioning anchor.

Type
boolean | undefined
Default

true

className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
| string
| ((state: Toast.Positioner.State) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: Toast.Positioner.State,
  ) => ReactElement)

Arrow

Displays an element positioned against the toast anchor. Renders a <div> element.

style

React.CSSProperties | function

Name
Type
| React.CSSProperties
| ((state: Toast.Arrow.State) => CSSProperties | undefined)
| undefined
className

string | function

Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
| string
| ((state: Toast.Arrow.State) => string | undefined)
render

ReactElement | function

Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: Toast.Arrow.State,
  ) => ReactElement)
data-uncentered

Present when the toast arrow is uncentered.

data-anchor-hidden

Present when the anchor is hidden.

data-align

Indicates how the popup is aligned relative to specified side.

data-side

Indicates which side the popup is positioned relative to the anchor.

useToastManager

Manages toasts, called inside of a <Toast.Provider>.

Usage

Return value

toasts

Toast.Root.ToastObject[]

Name
Description

The array of toast objects.

Type
Toast.Root.ToastObject[]
add

(options: useToast.AddOptions) => string

Name
Description

Add a toast to the toast list.

Type
(options: useToast.AddOptions) => string
close

(toastId: string) => void

Name
Description

Closes and removes a toast from the toast list.

Type
(toastId: string) => void
update

(toastId: string, options: useToast.UpdateOptions) => void

Name
Description

Update a toast in the toast list.

Type
(toastId: string, options: useToast.UpdateOptions) => void
promise

<Value>(promise: Promise<Value>, options: useToast.PromiseOptions) => Promise<Value>

Description

Create a toast that resolves with a value, with three possible states for the toast: loading, success, and error.

Type
<Value>(promise: Promise<Value>, options: useToast.PromiseOptions) => Promise<Value>

Method options

title

string

Name
Description

The title of the toast.

Type
string
description

string

Description

The description of the toast.

Type
string
type

string

Name
Description

The type of the toast. Used to conditionally style the toast or render different elements.

Type
string
timeout

number

Description

The amount of time (in ms) before the toast is auto dismissed.

Type
number
priority

'low' | 'high'

'low'

Description

The priority of the toast.

  • low - The toast will be announced politely.
  • high - The toast will be announced urgently.
Type
'low' | 'high'
Default

'low'

onClose

function

Description

A callback invoked when the toast is closed.

Type
() => void
onRemove

function

Description

A callback invoked when the toast is removed from the list after animations complete when closed.

Type
() => void
actionProps

React.ComponentPropsWithRef<'button'>

Description

The props of the action button.

Type
React.ComponentPropsWithRef<'button'>
data

Record<string, unknown>

Name
Description

The data of the toast.

Type
Record<string, unknown>

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.

Usage
Example

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.

Usage

close method

Closes the toast, removing it from the toast list after any animations complete.

Usage

promise method

Creates an asynchronous toast with three possible states: loading, success, and error.

Description configuration

Each state also accepts the method options object to granularly control the toast for each state:

Method options configuration

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.

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.

Creating a manager instance
Using the instance

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.

z-index stacking

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.

Expanded offset

<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:

Collapsed content

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.

Swipe offset

The data-swipe-direction attribute can be used to determine the swipe direction of the toasts to add a translation offset upon dismissal.

Swipe direction

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

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:

Mixing stacked and anchored toasts

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.

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.