--- metaTitle: Toast metaDescription: A succinct message that is displayed temporarily. name: toast aria: https://www.w3.org/TR/wai-aria/#aria-live --- # Toast A succinct message that is displayed temporarily. ## Anatomy Import the component. ```jsx import { Toast } from "radix-ui"; export default () => ( ); ``` ## API Reference ### Provider The provider that wraps your toasts and toast viewport. It usually wraps the application. An optional container where the toast announcements should be appended. This is useful when working with focus traps or modal dialogs that make other elements inert. ), }, ]} /> ### Viewport 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. Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details. ), }, { name: "hotkey", type: "string[]", default: '["F8"]', description: ( The keys to use as the keyboard shortcut that will move focus to the toast viewport. Use event.code value for each key from{" "} keycode.info . For meta keys, use ctrlKey, shiftKey,{" "} altKey and/or metaKey. ), }, { name: "label", type: "string", default: '"Notifications ({hotkey})"', description: "An author-localized label for the toast region to provide context for screen reader users when navigating page landmarks. The available `{hotkey}` placeholder will be replaced for you.", }, ]} /> ### Root The toast that automatically closes. It should not be held open to [acquire a user response](/primitives/docs/components/toast#action). Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details. ), }, { name: "type", type: '"foreground" | "background"', typeSimple: "enum", default: '"foreground"', description: ( Control the{" "} sensitivity {" "} of the toast for accessibility purposes. For toasts that are the result of a user action, choose foreground. Toasts generated from background tasks should use background. ), }, { name: "duration", type: "number", description: "The time in milliseconds that should elapse before automatically closing the toast. This will override the value supplied to the provider.", }, { name: "defaultOpen", type: "boolean", default: "true", description: "The open state of the dialog when it is initially rendered. Use when you do not need to control its open state.", }, { name: "open", type: "boolean", description: ( The controlled open state of the dialog. Must be used in conjunction with onOpenChange. ), }, { name: "onOpenChange", type: "(open: boolean) => void", typeSimple: "function", description: "Event handler called when the open state of the dialog changes.", }, { name: "onEscapeKeyDown", type: "(event: KeyboardEvent) => void", typeSimple: "function", description: ( Event handler called when the escape key is down. It can be prevented by calling event.preventDefault. ), }, { name: "onPause", type: "() => void", typeSimple: "function", description: "Event handler called when the dismiss timer is paused. This occurs when the pointer is moved over the viewport, the viewport is focused or when the window is blurred.", }, { name: "onResume", type: "() => void", typeSimple: "function", description: "Event handler called when the dismiss timer is resumed. This occurs when the pointer is moved away from the viewport, the viewport is blurred or when the window is focused.", }, { name: "onSwipeStart", type: "(event: SwipeEvent) => void", typeSimple: "function", description: ( Event handler called when starting a swipe interaction. It can be prevented by calling event.preventDefault. ), }, { name: "onSwipeMove", type: "(event: SwipeEvent) => void", typeSimple: "function", description: ( Event handler called during a swipe interaction. It can be prevented by calling event.preventDefault. ), }, { name: "onSwipeEnd", type: "(event: SwipeEvent) => void", typeSimple: "function", description: ( Event handler called at the end of a swipe interaction. It can be prevented by calling event.preventDefault. ), }, { name: "onSwipeCancel", type: "(event: SwipeEvent) => void", typeSimple: "function", description: ( Event handler called when a swipe interaction is cancelled. It can be prevented by calling event.preventDefault. ), }, { name: "forceMount", type: "boolean", description: "Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries.", }, ]} /> ### Title An optional title for the toast. Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details. ), }, ]} /> ### Description The toast message. Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details. ), }, ]} /> ### Action 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](https://www.w3.org/TR/UNDERSTANDING-WCAG20/time-limits-required-behaviors.html). When obtaining a user response is necessary, portal an [`AlertDialog`](/primitives/docs/components/alert-dialog) styled as a toast into the viewport instead. Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details. ), }, { name: "altText", required: true, type: "string", description: ( <> Describe an{" "} alternative way to achieve the action {" "} for screen reader users who cannot access the toast easily. ), }, ]} /> ### Close A button that allows users to dismiss the toast before its duration has elapsed. Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details. ), }, ]} /> ## Examples ### Custom hotkey Override the default hotkey using the `event.code` value for each key from [keycode.info](https://keycode.info/). ```jsx line=3 {/* ... */} ``` ### Custom duration Customise the duration of a toast to override the provider value. ```jsx line=1 Saved! ``` ### Duplicate toasts 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](/primitives/docs/components/toast#imperative-api). ```jsx line=6,11-15 export default () => { const [savedCount, setSavedCount] = React.useState(0); return (
setSavedCount((count) => count + 1)}> {/* ... */}
{Array.from({ length: savedCount }).map((_, index) => ( Saved! ))}
); }; ``` ### Animating swipe gesture 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: ```jsx line=6,7 // index.jsx import { Toast } from "radix-ui"; import "./styles.css"; export default () => ( ... ); ``` ```css line=2,3,5,9,15 /* styles.css */ .ToastRoot[__data-swipe__="move"] { transform: translateX(var(__--radix-toast-swipe-move-x__)); } .ToastRoot[__data-swipe__="cancel"] { transform: translateX(0); transition: transform 200ms ease-out; } .ToastRoot[__data-swipe__="end"] { animation: slideRight 100ms ease-out; } @keyframes slideRight { from { transform: translateX(var(__--radix-toast-swipe-end-x__)); } to { transform: translateX(100%); } } ``` ## Accessibility Adheres to the [`aria-live` requirements](https://www.w3.org/TR/wai-aria/#aria-live). ### Sensitivity 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 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 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. ```jsx line=1,6 File removed successfully. Dismiss We've just released Radix 1.0. Dismiss ``` ### Alternative action 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. ```jsx line=4,10,12 Upgrade Available! We've just released Radix 1.0. Upgrade Dismiss File removed successfully. Undo Alt+U Dismiss ``` ### Close icon button When providing an icon (or font icon), remember to label it correctly for screen reader users. ```jsx line=3-4 Saved! × ``` ### Keyboard Interactions When focus is on a Toast.Action or{" "} Toast.Close, closes the toast. ), }, { keys: ["Enter"], description: ( When focus is on a Toast.Action or{" "} Toast.Close, closes the toast. ), }, { keys: ["Esc"], description: ( When focus is on a Toast, closes the toast. ), }, ]} /> ## Custom APIs ### Abstract parts Create your own API by abstracting the primitive parts into your own component. #### Usage ```jsx import { Toast } from "./your-toast"; export default () => ( ); ``` #### Implementation ```jsx // your-toast.jsx import { Toast as ToastPrimitive } from "radix-ui"; export const Toast = ({ title, content, children, ...props }) => { return ( {title && {title}} {content} {children && ( {children} )} × ); }; ``` ### Imperative API Create your own imperative API to allow [toast duplication](/primitives/docs/components/toast#duplicate-toasts) if preferred. #### Usage ```jsx import { Toast } from "./your-toast"; export default () => { const savedRef = React.useRef(); return (
savedRef.current.publish()}> {/* ... */}
Saved successfully!
); }; ``` #### Implementation ```jsx // your-toast.jsx import * as React from "react"; import { Toast as ToastPrimitive } from "radix-ui"; export const Toast = React.forwardRef((props, forwardedRef) => { const { children, ...toastProps } = props; const [count, setCount] = React.useState(0); React.useImperativeHandle(forwardedRef, () => ({ publish: () => setCount((count) => count + 1), })); return ( <> {Array.from({ length: count }).map((_, index) => ( {children} Dismiss ))} ); }); ```