组件 · 反馈
Toast
A dark floating notice that confirms the result of something the person just did, disappears after a few seconds, and can offer View or Undo.
import { Toast } from "@botiverse/raft-ui";A toast is the receipt for something the person just did: a message sent, a setting saved, an agent finishing the job they just gave it. It floats above the page on the dark HUD, says one sentence, and goes away after a few seconds. It can carry one text action, such as View or Undo.
When to use
| Situation | Use |
|---|---|
| Something the person just did has a result | Toast |
| An action that can be reversed in place | Do it; when a toast offers Undo is in Feedback |
| The state of the whole server | NotificationCenter |
| Something that happened elsewhere | ActivityInbox |
| Something to confirm or decide | Dialog |
| Something to read before acting | Banner |
Never put anything in a toast that would confuse people if they missed it (Choosing a component).
Anatomy
ToastProvider: holds the toast manager and, by default, renders the viewport. Mount it once near the root.toast: the command API (toast.success,toast.error, …) for the default manager.ToastViewport: where toasts appear on screen.ToastPositionerandToastArrowanchor a toast to an element instead.ToastRootandToastContent: one toast and its layout.ToastIcon: optional. Who did it (an avatar), or a status pixel when there is no "who".ToastBody, holdingToastTitle(required) and an optionalToastDescription, both intext-callout(Typography): 14/20 on a phone, 14/22 on a desktop, like a banner.ToastIconis one line of that text tall, so the icon stays on the title's line: a status light in it is 9px and an icon 16px, centered in a 20px column 12px before the title, as is an avatar (A mark that leads text).ToastActions: optional, holding oneToastActionandToastClose.ToastProgress: an optional line that shows the time left before it closes.
Leave out a part you don't need instead of rendering it empty.
Variants
The intent sets the pixel in ToastIcon. When the toast is about someone, show their avatar there instead.
| Intent | Use for |
|---|---|
| A person or agent did it | Their avatar in ToastIcon: "Bugen finished reviewing #25." |
success | Something finished or saved |
warning, error | Something went wrong. error is announced at a higher priority |
info | A neutral result, such as "3 links copied." |
| A message sent as a task | The Todo pixel in ToastIcon |
With the toast command API, pass the avatar as leading. It takes the pixel's place in ToastIcon, hidden from assistive technology.
Behavior
- A toast closes on its own after the provider's
timeout; hovering or focusing it pauses the timer. - People can swipe a toast away, or close it with
ToastClose. - Stacked toasts appear at the bottom center by default. An anchored toast opens next to the control that caused it.
Content
- Write one sentence: who, what they did, and the result. Put anything more in
ToastDescription, inforeground-on-hud-muted. - Label the action View or Undo, or another verb in Title Case.
- Set IDs, counts and times in Space Mono (Typography).
Accessibility
Toast wraps Base UI's Toast, which announces each toast in a live region and keeps it open while it has focus.
- Make
ToastTitlecarry the whole meaning. The pixel inToastIconis hidden from assistive technology, and so isToastProgress. - Hide an avatar in
ToastIconwitharia-hidden; the name is already in the text. The command API'sleadingdoes this for you. - Pass
loadingLabelto aToastActionthat shows a loading state, so its spinner says what it waits for.
Rules
- Show at most one toast at a time.
- Use
layer-hudfor every toast, withforeground-on-hudtext. Don't tint a toast with an intent color, and never use yellow: notices are neutral (Color). - Make the action a bold text action that underlines on hover (
ToastAction), never a button. Never put a colored button in a toast. - Give a toast at most one action besides closing it.
API
ToastAction
| Prop | 类型 | 默认值 |
|---|---|---|
| boolean | false | |
| string | "Loading" | |
| 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 (for example, `<div>`). | boolean | true |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ToastActions
接受其元素的全部属性。
ToastArrow
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ToastBody
接受其元素的全部属性。
ToastClose
| Prop | 类型 | 默认值 |
|---|---|---|
| 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 (for example, `<div>`). | boolean | true |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ToastContent, ToastDescription
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ToastIcon
接受其元素的全部属性。
ToastPortal
| Prop | 类型 | 默认值 |
|---|---|---|
| A parent element to render the portal element into. | HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ToastPositioner
| Prop | 类型 | 默认值 |
|---|---|---|
| How to align the popup relative to the specified side. | Align | 'center' |
| 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. | number | OffsetFunction | 0 |
| An element to position the toast against. | Element | |
| Minimum distance to maintain between the arrow and the edges of the popup. | number | 5 |
| Determines how to handle collisions when positioning the popup. | CollisionAvoidance | |
| An element or a rectangle that delimits the area that the popup is confined to. | Boundary | 'clipping-ancestors' |
| Additional space to maintain from the edge of the collision boundary. | Padding | 5 |
| Whether to disable the popup from tracking any layout shift of its positioning anchor. | boolean | false |
| Determines which CSS `position` property to use. | "fixed" | "absolute" | 'absolute' |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) | |
| Which side of the anchor element to align the toast against. May automatically change to avoid collisions. | Side | 'top' |
| 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. | number | OffsetFunction | 0 |
| Whether to maintain the popup in the viewport after the anchor element was scrolled out of view. | boolean | false |
| The toast object associated with the positioner. | ToastObject<any> |
Also takes the attributes of its element.
ToastProgress
| Prop | 类型 | 默认值 |
|---|---|---|
| number | DEFAULT_TOAST_TIMEOUT_MS |
Also takes the attributes of its element.
ToastProvider
| Prop | 类型 | 默认值 |
|---|---|---|
| HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | ||
| boolean | true | |
| string | ||
| "none" | "bottom-right" | "top-right" | "bottom-center" | "top-center" | "bottom-center" | |
| "fixed" | "absolute" | "fixed" | |
| ReactNode | ||
| The maximum number of toasts that can be displayed at once. When the limit is exceeded, the oldest toasts are marked as `limited` (via the `data-limited` attribute) rather than removed, so they can be hidden or animated out. | number | 3 |
| 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. | number | DEFAULT_TOAST_TIMEOUT_MS |
| A global manager for toasts to use outside of a React component. | ToastManager<any> | defaultToastManager |
ToastRoot
| Prop | 类型 | 默认值 |
|---|---|---|
| "inline" | "stacked" | "stacked" | |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) | |
| Direction(s) in which the toast can be swiped to dismiss. | "up" | "down" | "left" | "right" | ("up" | "down" | "left" | "right")[] | ['down', 'right'] |
| The toast to render. | ToastRootToastObject<any> |
Also takes the attributes of its element.
ToastTitle
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ToastViewport
| Prop | 类型 | 默认值 |
|---|---|---|
| "none" | "bottom-right" | "top-right" | "bottom-center" | "top-center" | "bottom-center" | |
| "fixed" | "absolute" | "fixed" | |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
相关组件
改动
- 0.9.6 · Added
toast.add/info/success/warning/error(title, { leading })puts a node, such as a person's avatar, inToastIconin place of the status pixel. It isaria-hiddenand still shows withicon: false. Calls withoutleadingrender exactly as before. - 0.5.35 · Changed
ToastContentmeasures its rendered content and writesdata-has-icon,data-has-descriptionanddata-has-actionson itself; the content layout keys off these attributes instead of descendant selectors.