跳到正文
目录与搜索
⌘K

原语 / React Aria

Checkbox · React Aria

在选中与未选中之间切换的复选框。

安装

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

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

用法

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

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

export function Terms() {
  return (
    <Checkbox defaultSelected>
      {({ isSelected }) => (
        <>
          <span data-slot="checkbox-indicator">
            {isSelected && <CheckIcon />}
          </span>
          Accept terms and conditions
        </>
      )}
    </Checkbox>
  );
}
checkbox.tsx
<Checkbox defaultSelected animation="pop">
  {({ isSelected }) => (
    <>
      <span data-slot="checkbox-indicator">{isSelected && <CheckIcon />}</span>
      Accept terms and conditions
    </>
  )}
</Checkbox>

动效

  • 指示器中的描边在勾选后延迟 0.2s 绘制,pathLength 与透明度 0.3s 从 0 到 1;取消勾选 0.3s 擦除。
  • animation="pop" 时从一半大小、2px 模糊中弹入,弹簧 stiffness 500 / damping 24。

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

API

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

属性类型默认值
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 版本。