跳到正文
目录与搜索
⌘K

原语 / Base UI

Checkbox · Base UI

在选中与未选中之间切换的复选框,勾选标记会自己画出来。

安装

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

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

用法

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

tsx
import { Checkbox } from '@animate-ui/react/primitives/base-ui/checkbox';
checkbox.tsx
import { Checkbox } from '@animate-ui/react/primitives/base-ui/checkbox';

export function Terms() {
  return (
    <label className="flex items-center gap-2">
      <Checkbox.Root defaultChecked>
        <Checkbox.Indicator>
          <CheckIcon />
        </Checkbox.Indicator>
      </Checkbox.Root>
      Accept terms and conditions
    </label>
  );
}
checkbox.tsx
<Checkbox.Root defaultChecked>
  <Checkbox.Indicator animation="pop">
    <CheckIcon />
  </Checkbox.Indicator>
</Checkbox.Root>

动效

  • 勾选时描边 pathLength 与透明度一起从 0 到 1,延迟 0.2s、历时 0.3s;取消勾选 0.3s 擦除后卸载,指示元素本身不移动。
  • animation="pop"(或 Provider 的 checkAnimation="pop")改为从一半大小、2px 模糊中弹入,弹簧 stiffness 500 / damping 24。

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

API

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

属性类型默认值
animation'draw' | 'pop'the closest AnimateUIProvider's checkAnimation, else 'draw'
transitionTransitiondraw: { duration: 0.3, delay: 0.2, ease: [0, 0, 0.58, 1] }; pop: { type: 'spring', stiffness: 500, damping: 24, delay: 0.2 }
exitTransitionTransitiondraw: { duration: 0.3, ease: [0, 0, 0.58, 1] }; pop: { duration: 0.15, ease: [0.23, 1, 0.32, 1] }

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

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