原语 / React Aria
Select · React Aria
可展开的选项列表,允许用户选择其中一项。
安装
React Aria 的原语保持上游 API 不变,安装只需要三步:加包、加组件、把 import 换掉。
bun add @animate-ui/react motion
bunx shadcn@latest add select
bunx @animate-ui/cli@latest animate select用法
从 Animate UI 引入原语,其余结构、属性与 data 属性都保持 React Aria 的写法。也可以按子路径单独引入:
import {
Button,
ListBox,
ListBoxItem,
Popover,
Select,
SelectionIndicator,
SelectValue,
} from '@animate-ui/react/primitives/react-aria';import {
Button,
ListBox,
ListBoxItem,
Popover,
Select,
SelectionIndicator,
SelectValue,
} from '@animate-ui/react/primitives/react-aria';
export function FontPicker() {
return (
<Select aria-label="Font" defaultValue="geist">
<Button>
<SelectValue />
</Button>
<Popover placement="bottom start" offset={4}>
<ListBox>
<ListBoxItem id="inter" textValue="Inter">
Inter
<SelectionIndicator>✓</SelectionIndicator>
</ListBoxItem>
<ListBoxItem id="geist" textValue="Geist">
Geist
<SelectionIndicator>✓</SelectionIndicator>
</ListBoxItem>
<ListBoxItem id="lora" textValue="Lora">
Lora
<SelectionIndicator>✓</SelectionIndicator>
</ListBoxItem>
</ListBox>
</Popover>
</Select>
);
}<ListBoxItem id="geist" textValue="Geist">
Geist
<SelectionIndicator animation="pop">✓</SelectionIndicator>
</ListBoxItem>动效
- 列表从触发元素的锚点以 0.9 缩放淡入,220ms,关闭同路返回后卸载。
- 关闭过程中重新打开会从当前位置接续,不会跳回起点。
所有时长都会乘以 AnimateUIProvider 的 tempo,并遵守系统的减少动效设置。
API
动效相关的 transition 与 exitTransition 是 Animate UI 唯一新增的属性;其余 props 与 React Aria 完全一致。
| 属性 | 类型 | 默认值 |
|---|---|---|
| transition | Transition | { duration: 0.22, ease: [0.215, 0.61, 0.355, 1] } |
| exitTransition | Transition | transition |
| animation | 'draw' | 'pop' | the closest AnimateUIProvider's checkAnimation, else 'draw' |
| transition | Transition | draw: 0.3s on Motion's easeOut both ways (a Checkbox's, without its delay); pop: a spring (stiffness 500, damping 24) in, 0.15s out, and the highlight's spring (stiffness 500, damping 40) for the glide |
未列出的属性与上游 API 完全一致,可在 官方文档 查阅。