A succinct message that is displayed temporarily.
import * as React from "react";import * as Toast from "@radix-ui/react-toast";import "./styles.css";const ToastDemo = () => {const [open, setOpen] = React.useState(false);const eventDateRef = React.useRef(new Date());const timerRef = React.useRef(0);React.useEffect(() => {return () => clearTimeout(timerRef.current);}, []);return (<Toast.Provider swipeDirection="right"><button className="Button large violet" onClick={() => { setOpen(false); window.clearTimeout(timerRef.current); timerRef.current = window.setTimeout(() => { eventDateRef.current = oneWeekAway(); setOpen(true); }, 100); }} >Add to calendar</button><Toast.Root className="ToastRoot" open={open} onOpenChange={setOpen}><Toast.Title className="ToastTitle">Scheduled: Catch up</Toast.Title><Toast.Description asChild><time className="ToastDescription" dateTime={eventDateRef.current.toISOString()} >{prettyDate(eventDateRef.current)}</time></Toast.Description><Toast.Action className="ToastAction" asChild altText="Goto schedule to undo" ><button className="Button small green">Undo</button></Toast.Action></Toast.Root><Toast.Viewport className="ToastViewport" /></Toast.Provider>);};function oneWeekAway(date) {const now = new Date();const inOneWeek = now.setDate(now.getDate() + 7);return new Date(inOneWeek);}function prettyDate(date) {return new Intl.DateTimeFormat("en-US", {dateStyle: "full",timeStyle: "short",}).format(date);}export default ToastDemo;
Automatically closes.
Pauses closing on hover, focus and window blur.
Supports hotkey to jump to toast viewport.
Supports closing via swipe gesture.
Exposes CSS variables for swipe gesture animations.
Can be controlled or uncontrolled.
Install the component from your command line.
Import the component.
The provider that wraps your toasts and toast viewport. It usually wraps the application.
The fixed area where toasts appear. Users can jump to the viewport by pressing a hotkey. It is up to you to ensure the discoverability of the hotkey for keyboard users.
The toast that automatically closes. It should not be held open to acquire a user response.
An optional title for the toast.
The toast message.
An action that is safe to ignore to ensure users are not expected to complete tasks with unexpected side effects as a result of a time limit.
When obtaining a user response is necessary, portal an AlertDialog
styled as a toast into the viewport instead.
A button that allows users to dismiss the toast before its duration has elapsed.
Override the default hotkey using the event.code
value for each key from keycode.info.
Customise the duration of a toast to override the provider value.
When a toast must appear every time a user clicks a button, use state to render multiple instances of the same toast (see below). Alternatively, you can abstract the parts to create your own imperative API.
Combine --radix-toast-swipe-move-[x|y]
and --radix-toast-swipe-end-[x|y]
CSS variables with data-swipe="[start|move|cancel|end]"
attributes to animate a swipe to close gesture. Here's an example:
Adheres to the aria-live
requirements.
Control the sensitivity of the toast for screen readers using the type
prop.
For toasts that are the result of a user action, choose foreground
. Toasts generated from background tasks should use background
.
Foreground toasts are announced immediately. Assistive technologies may choose to clear previously queued messages when a foreground toast appears. Try to avoid stacking distinct foreground toasts at the same time.
Background toasts are announced at the next graceful opportunity, for example, when the screen reader has finished reading its current sentence. They do not clear queued messages so overusing them can be perceived as a laggy user experience for screen reader users when used in response to a user interaction.
Use the altText
prop on the Action
to instruct an alternative way of actioning the toast to screen reader users.
You can direct the user to a permanent place in your application where they can action it or implement your own custom hotkey logic. If implementing the latter, use foreground
type to announce immediately and increase the duration to give the user ample time.
When providing an icon (or font icon), remember to label it correctly for screen reader users.
Create your own API by abstracting the primitive parts into your own component.
Create your own imperative API to allow toast duplication if preferred.