原语 / Base UI
Toast · Base UI
生成轻量提示通知的容器,支持堆叠与自动消失。
安装
Base UI 的原语保持上游 API 不变,安装只需要三步:加包、加组件、把 import 换掉。
bun add @animate-ui/react motion
bunx shadcn@latest add toast
bunx @animate-ui/cli@latest animate toast用法
从 Animate UI 引入原语,其余结构、属性与 data 属性都保持 Base UI 的写法。也可以按子路径单独引入:
import { Toast } from '@animate-ui/react/primitives/base-ui/toast';import { Toast } from '@animate-ui/react/primitives/base-ui/toast';
function ToastList() {
const { toasts } = Toast.useToastManager();
return toasts.map((toast) => (
<Toast.Root key={toast.id} toast={toast}>
<Toast.Content>
<Toast.Title />
<Toast.Description />
<Toast.Close aria-label="Close">×</Toast.Close>
</Toast.Content>
</Toast.Root>
));
}
export function Toaster({ children }: { children: React.ReactNode }) {
return (
<Toast.Provider>
{children}
<Toast.Portal>
<Toast.Viewport>
<ToastList />
</Toast.Viewport>
</Toast.Portal>
</Toast.Provider>
);
}<Toast.Provider progressBar={false}>…</Toast.Provider>动效
- 提示从所在边缘升起一个自身高度,从 0.96 与 4px 模糊中淡入,弹簧 stiffness 280 / damping 24,约 4% 过冲后落定。
- 关闭时沿原路更快离场(0.2s),立即加速,不拖尾。
所有时长都会乘以 AnimateUIProvider 的 tempo,并遵守系统的减少动效设置。
API
动效相关的 transition 与 exitTransition 是 Animate UI 唯一新增的属性;其余 props 与 Base UI 完全一致。
| 属性 | 类型 | 默认值 |
|---|---|---|
| progressBar | boolean | true |
| transition | Transition | { type: 'spring', stiffness: 280, damping: 24 }; anchored: { type: 'spring', stiffness: 400, damping: 29 } |
| exitTransition | Transition | { duration: 0.2, ease: [0.33, 0.17, 0.67, 0.5] }; anchored: transition |
| progressBar | boolean | the Provider's progressBar |
| transition | Transition | { duration: 0.2, ease: [0.23, 1, 0.32, 1] } |
未列出的属性与上游 API 完全一致,可在 官方文档 查阅。