¥Toast
一条临时显示的简洁消息。
import * as React from "react";import { Toast } from "radix-ui";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.
¥Installation
从命令行安装组件。
¥Install the component from your command line.
¥Anatomy
导入组件。
¥Import the component.
¥API Reference
¥Provider
封装 Toast 和 Toast 视口的提供程序。它通常封装应用。
¥The provider that wraps your toasts and toast viewport. It usually wraps the application.
¥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.
¥Root
自动关闭的提示框。它不应该保持对 获取用户响应 的打开状态。
¥The toast that automatically closes. It should not be held open to acquire a user response.
¥Title
用于提示框的可选标题。
¥An optional title for the toast.
¥Description
提示消息。
¥The toast message.
¥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.
当需要获取用户响应时,将样式为 Toast 的 AlertDialog
传送到视口中。
¥When obtaining a user response is necessary, portal an AlertDialog
styled as a toast into the viewport instead.
¥Close
一个按钮,允许用户在提示框持续时间结束前将其关闭。
¥A button that allows users to dismiss the toast before its duration has elapsed.
¥Examples
¥Custom hotkey
使用 keycode.info 中每个键的 event.code
值覆盖默认热键。
¥Override the default hotkey using the event.code
value for each key from keycode.info.
¥Custom duration
自定义提示框的持续时间以覆盖提供程序的值。
¥Customise the duration of a toast to override the provider value.
¥Duplicate toasts
如果每次用户点击按钮时都必须出现提示框,请使用状态来渲染同一提示框的多个实例(见下文)。或者,你可以抽象各个部分来创建你自己的 命令式 API。
¥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.
¥Animating swipe gesture
将 --radix-toast-swipe-move-[x|y]
和 --radix-toast-swipe-end-[x|y]
CSS 变量与 data-swipe="[start|move|cancel|end]"
属性相结合,即可为滑动关闭手势添加动画效果。这是一个示例:
¥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:
¥Accessibility
遵循 aria-live
要求。
¥Adheres to the aria-live
requirements.
¥Sensitivity
使用 type
属性控制屏幕阅读器提示框的敏感度。
¥Control the sensitivity of the toast for screen readers using the type
prop.
对于由用户操作生成的 Toast,请选择 foreground
。后台任务生成的 Toast 消息应该使用 background
。
¥For toasts that are the result of a user action, choose foreground
. Toasts generated from background tasks should use background
.
¥Foreground
前台 Toast 会立即显示。当出现前台 Toast 消息时,辅助技术可以选择清除之前排队的消息。尽量避免同时堆叠不同的前台 Toast。
¥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.
¥Alternative action
在 Action
上使用 altText
属性来指示向屏幕阅读器用户发出提示的另一种方式。
¥Use the altText
prop on the Action
to instruct an alternative way of actioning the toast to screen reader users.
你可以将用户引导到应用中的永久位置,以便他们执行操作或实现你自己的自定义热键逻辑。如果实现后者,请使用 foreground
类型来立即通知,并增加持续时间以给用户充足的时间。
¥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.
¥Close icon button
提供图标(或字体图标)时,请记住为屏幕阅读器用户正确标记。
¥When providing an icon (or font icon), remember to label it correctly for screen reader users.
¥Keyboard Interactions
¥Custom APIs
¥Abstract parts
通过将原始部分抽象到你自己的组件中来创建你自己的 API。
¥Create your own API by abstracting the primitive parts into your own component.
¥Usage
¥Implementation
¥Imperative API
如果需要,请创建你自己的命令式 API 以允许使用 toast 重复。
¥Create your own imperative API to allow toast duplication if preferred.
¥Usage
¥Implementation