原语 / Animate UI
Copy Button · Animate UI
复制文本到剪贴板的按钮,图标会在复制成功后换成对勾。
安装
自有原语直接来自 @animate-ui/react,不依赖 Radix UI、Base UI 或 React Aria;装上包即可使用。
bun add @animate-ui/react motion用法
直接从 @animate-ui/react 的对应子路径引入,下面就是完整示例;样式由你的类名决定。
import {
CopyButton,
CopyButtonIcon,
} from '@animate-ui/react/primitives/buttons/copy';
export function CopyCommand() {
return (
<CopyButton content="npx animate-ui init" aria-label="Copy">
<CopyButtonIcon icon={<CopyIcon />} copiedIcon={<CheckIcon />} />
</CopyButton>
);
}动效
- 复制后复制图标缩到一半大小、进入 2px 模糊并淡出,对勾从同一模糊中长出,0.15s ease-out。
- delay 之后以同样方式换回;减少动效下直接切换图标。
所有时长都会乘以 AnimateUIProvider 的 tempo,并遵守系统的减少动效设置。
API
动效相关的 transition 与 exitTransition 是 Animate UI 唯一新增的属性;其余 props 见上表。
| 属性 | 类型 | 默认值 |
|---|---|---|
| content | string | — |
| copied | boolean | — |
| defaultCopied | boolean | false |
| onCopiedChange | (copied: boolean, content?: string) => void | — |
| delay | number | 3000 |
| hoverScale | number | 1 |
| tapScale | number | 0.97 |
| transition | Transition | { type: 'spring', stiffness: 550, damping: 30, restSpeed: 10 } |
| asChild | boolean | false |
| …props | HTMLMotionProps<'button'> | — |
| icon | React.ReactNode | — |
| copiedIcon | React.ReactNode | — |
| transition | Transition | { duration: 0.15, ease: [0.23, 1, 0.32, 1] } |
| …props | React.ComponentProps<'span'> | — |
| data-copied | present | — |
未列出的属性与上游 API 完全一致,可在 官方文档 查阅。
内容对应官方文档 2026-10-18 版本。