跳到正文
目录与搜索
⌘K

原语 / Base UI

Alert Dialog · Base UI

要求用户必须作出回应的模态对话框,键盘焦点与 ARIA 语义完全沿用 Base UI。

安装

Base UI 的原语保持上游 API 不变,安装只需要三步:加包、加组件、把 import 换掉。

终端
bun add @animate-ui/react motion
bunx shadcn@latest add alert-dialog
bunx @animate-ui/cli@latest animate alert-dialog

用法

从 Animate UI 引入原语,其余结构、属性与 data 属性都保持 Base UI 的写法。也可以按子路径单独引入:

tsx
import { AlertDialog } from '@animate-ui/react/primitives/base-ui/alert-dialog';
alert-dialog.tsx
import { AlertDialog } from '@animate-ui/react/primitives/base-ui/alert-dialog';

export function DeleteProject() {
  return (
    <AlertDialog.Root>
      <AlertDialog.Trigger>Delete project</AlertDialog.Trigger>
      <AlertDialog.Portal>
        <AlertDialog.Backdrop />
        <AlertDialog.Popup>
          <AlertDialog.Title>Delete this project?</AlertDialog.Title>
          <AlertDialog.Description>
            This action cannot be undone.
          </AlertDialog.Description>
          <AlertDialog.Close>Cancel</AlertDialog.Close>
        </AlertDialog.Popup>
      </AlertDialog.Portal>
    </AlertDialog.Root>
  );
}

动效

  • 遮罩背景色加深、backdrop 模糊从 0 增长,400ms 柔和 ease-out;弹层从 perspective(500px) rotateX(-20deg) scale(0.8)、4px 模糊中翻正,弹簧 stiffness 150 / damping 25。
  • 关闭时立即沿原路加速离场(0.2s),在遮罩清空之前消失;减少动效下只淡入淡出,不倾斜、不缩放。

所有时长都会乘以 AnimateUIProvider 的 tempo,并遵守系统的减少动效设置。

API

动效相关的 transition 与 exitTransition 是 Animate UI 唯一新增的属性;其余 props 与 Base UI 完全一致。

属性类型默认值
transitionTransition{ duration: 0.4, ease: [0.215, 0.61, 0.355, 1] }
exitTransitionTransitiontransition
from'top' | 'bottom' | 'left' | 'right''top'
transitionTransition{ type: 'spring', stiffness: 150, damping: 25, opacity: { duration: 0.4, ease: [0.215, 0.61, 0.355, 1] } }
exitTransitionTransition{ duration: 0.2, ease: [0.33, 0.17, 0.67, 0.5] }

未列出的属性与上游 API 完全一致,可在 官方文档 查阅。

内容对应官方文档 2026-10-18 版本。