Toast

A message that appears on the screen to provide feedback on an action.

Loading...

Anatomy

const toaster = createToaster({ placement: 'bottom-end' })

<Toaster toaster={toaster}>
  {(toast) => (
    <Toast.Root>
      <Toast.Title />
      <Toast.Description />
      <Toast.ActionTrigger />
      <Toast.CloseTrigger />
    </Toast.Root>
  )}
</Toaster>

Setup

Create the toast engine with createToaster. It manages placement and grouping, and returns the object you use to create toasts.

const toaster = createToaster({
  placement: 'bottom-end',
  overlap: true,
  gap: 24,
})

Examples

Create a toast with toast.create.

Types

Toasts come in success, error, warning and info types. For example:

toaster.success({
  title: 'Success!',
  description: 'Your changes have been saved.',
})

Promise

toaster.promise() takes options for the loading, success and error states. It updates the toast when the promise resolves or rejects.

Update

Update a toast with toast.update.

Duration

Set duration in milliseconds, or Infinity to keep the toast until it's dismissed.

Max Visible

Set max in createToaster to cap the number of visible toasts. Extra toasts queue until one is dismissed.

Placement

Set placement in createToaster, such as top-start, top-end, bottom-start or bottom-end.

Action

Add an action with the toast.action property.

Undo

Archive a file, then restore it from Toast.ActionTrigger.

Guides

Toast in Effects

Creating a toast inside React effects (useEffect, useLayoutEffect, or handlers that run during render) can trigger a "flushSync" warning. Wrap the call in queueMicrotask:

import { useEffect } from 'react'

export const EffectToast = () => {
  useEffect(() => {
    // ❌ This may cause flushSync warnings
    // toaster.create({ title: 'Effect triggered!' })

    // ✅ Wrap in queueMicrotask to avoid warnings
    queueMicrotask(() => {
      toaster.create({
        title: 'Effect triggered!',
        description: 'This toast was called safely from an effect',
        type: 'success',
      })
    })
  }, [])

  return <div>Component content</div>
}

This defers the toast until the current execution context finishes.

Styling

Toasts need some minimal styling to work correctly.

Toast root

The toast root gets these CSS variables at runtime:

  • --x - The x position
  • --y - The y position
  • --scale - The scale
  • --z-index - The z-index
  • --height - The height
  • --opacity - The opacity
  • --gap - The gap between toasts
[data-toast-root] {
  translate: var(--x) var(--y);
  scale: var(--scale);
  z-index: var(--z-index);
  height: var(--height);
  opacity: var(--opacity);
  will-change: translate, opacity, scale;
  transition:
    translate 400ms,
    scale 400ms,
    opacity 400ms,
    height 400ms,
    box-shadow 200ms;
  transition-timing-function: cubic-bezier(0.21, 1.02, 0.73, 1);

  &[data-state='closed'] {
    transition:
      translate 400ms,
      scale 400ms,
      opacity 200ms;
    transition-timing-function: cubic-bezier(0.06, 0.71, 0.55, 1);
  }
}

Styling based on type

Style by the data-type attribute.

[data-toast-root] {
  &[data-type='error'] {
    background: red;
    color: white;
  }

  &[data-type='info'] {
    background: blue;
    color: white;
  }

  &[data-type='warning'] {
    background: orange;
  }

  &[data-type='success'] {
    background: green;
    color: white;
  }
}

Mobile considerations

Make toasts span the full screen width on mobile.

@media (max-width: 640px) {
  [data-toast-group] {
    width: 100%;
  }

  [data-toast-root] {
    inset-inline: 0;
    width: calc(100% - var(--gap) * 2);
  }
}

API Reference

Props

Root

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<ToastRootState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-toast-root]<uid>
[data-state]"open" | "closed"
[data-type]The type of the item
[data-placement]The placement of the toast
[data-align]
[data-side]The side of the trigger that the toast is positioned on
[data-mounted]Present when mounted
[data-paused]Present when paused
[data-first]
[data-sibling]
[data-stack]
[data-overlap]Present when overlapping
[data-swiping]
[data-swipe-direction]

ActionTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

CloseTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Description

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Store

PropDefaultType
duration
number

The duration of the toast. By default, it is determined by the type of the toast.

gap16
number

The gap between the toasts

hotkey'['altKey', 'KeyT']'
string[]

The hotkey that will move focus to the toast group

max24
number

The maximum number of toasts. When the number of toasts exceeds this limit, the new toasts are queued.

offsets'1rem'
string | Record<'top' | 'bottom' | 'left' | 'right', string>

The offset from the safe environment edge of the viewport

overlap
boolean

Whether to overlap the toasts

pauseOnPageIdletrue
boolean

Whether to pause toast when the user leaves the browser tab

placement'bottom'
Placement

The placement of the toast

removeDelay200
number

The duration for the toast to kept alive before it is removed. Useful for exit transitions.

swipeDirections
SwipeDirection[]

The directions toasts can be swiped to dismiss. Pass `[]` to disable swiping. Defaults to the edges nearest the placement, e.g. `["down", "right"]` for `bottom-end`.

Title

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Toaster

PropDefaultType
toaster
CreateToasterReturn<any>

dir'ltr'
'ltr' | 'rtl'

The document's text/writing direction.

getRootNode
() => ShadowRoot | Node | Document

A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.

label'Notifications'
string

The human-readable label for the toast region.

Context

API

PropertyType
getCount
() => number

The total number of toasts

getToasts
() => ToastProps[]

The toasts

subscribe
(callback: (toasts: Options<O>[]) => void) => VoidFunction

Subscribe to the toast group

getGroupState
() => GroupState

Returns the state of the group