Button(按钮)
显示按钮或外观类似按钮的组件。
Note
Nola 产品使用限制:本页保留通用 shadcn/ui 能力介绍,实际开发以 DESIGN.md 和本地 src/renderer/components/ui/button.tsx 为准。产品按钮统一使用 18px 圆角,浅深色与所有尺寸一致;文字使用 Body,尺寸与密度不改变字号。产品不开放 ghost,不使用全圆角按钮。下文涉及这些能力的通用示例不属于产品推荐用法。
安装
npx shadcn@latest add button1. 安装以下依赖:
npm install @base-ui/react2. 将以下代码复制到你的项目中。
import { Button as ButtonPrimitive } from "@base-ui/react/button"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
const buttonVariants = cva(
"group/button inline-flex shrink-0 items-center justify-center rounded-4xl border border-transparent bg-clip-padding type-body font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-[var(--primary-hover)]",
outline:
"border-border bg-input/30 hover:bg-input/50 hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground",
secondary:
"bg-secondary text-secondary-foreground hover:bg-accent hover:text-accent-foreground aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
ghost:
"hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
destructive:
"bg-destructive/10 text-danger-soft-foreground hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default:
"h-[var(--field-control-height)] gap-1.5 px-3 has-data-[icon=inline-end]:pe-2.5 has-data-[icon=inline-start]:ps-2.5",
xs: "h-6 gap-1 px-2.5 has-data-[icon=inline-end]:pe-2 has-data-[icon=inline-start]:ps-2 [&_svg:not([class*='size-'])]:size-3",
sm: "h-8 gap-1 px-3 has-data-[icon=inline-end]:pe-2 has-data-[icon=inline-start]:ps-2",
lg: "h-10 gap-1.5 px-4 has-data-[icon=inline-end]:pe-3 has-data-[icon=inline-start]:ps-3",
icon: "size-9",
"icon-xs": "size-6 [&_svg:not([class*='size-'])]:size-3",
"icon-sm": "size-8",
"icon-lg": "size-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
function Button({
className,
variant = "default",
size = "default",
...props
}: ButtonPrimitive.Props & VariantProps<typeof buttonVariants>) {
return (
<ButtonPrimitive
data-slot="button"
data-variant={variant}
data-size={size}
className={cn(buttonVariants({ variant, size, className }))}
{...props}
/>
)
}
export { Button, buttonVariants }2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
3. 根据你的项目配置调整导入路径。
用法
import { Button } from "@/components/ui/button"<Button variant="outline">Button</Button>光标
Tailwind v4 将按钮组件的光标样式从 cursor: pointer 改为 cursor: default。
如需保留 cursor: pointer 行为,请在 CSS 文件中添加以下代码:
也可以在初始化项目时通过 npx shadcn@latest init --pointer 启用它。
@layer base {
button:not(:disabled),
[role="button"]:not(:disabled) {
cursor: pointer;
}
}2
3
4
5
6
尺寸
使用 size 属性调整按钮尺寸。
默认
描边
次要
幽灵
此变体属于通用组件 API,Nola 产品未开放,不能直接复制到产品中。图标操作可使用本地组件支持的透明背景形式。
危险操作
链接
图标
带图标
请为图标添加 data-icon="inline-start" 或 data-icon="inline-end" 属性,以确保间距正确。
全圆角
通用组件可以通过 rounded-full 变为全圆角。Nola 产品按钮固定使用 --radius-xl(18px),不采用此覆盖。
// 通用 API 示例,Nola 产品不采用此覆盖。
<Button className="rounded-full">按钮</Button>2
加载指示器
在按钮内部渲染 <Spinner /> 组件以显示加载状态。请为加载指示器添加 data-icon="inline-start" 或 data-icon="inline-end" 属性,以确保间距正确。
按钮组
使用 ButtonGroup 组件创建按钮组。更多信息请参阅按钮组文档。
用作链接
可以使用 buttonVariants 辅助函数为链接应用按钮样式。
请勿使用 <Button render={<a />} nativeButton={false} /> 渲染链接。 Base UI 的 Button 组件始终应用 role="button",会覆盖 <a> 元素的链接语义。请将 buttonVariants 用于普通的 <a> 标签。
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
API 参考
Button
Button 组件包裹 button 元素,并提供多种样式和功能。
| 属性 | 类型 | 默认值 |
|---|---|---|
variant | "default" | "outline" | "ghost" | "destructive" | "secondary" | "link" | "default" |
size | "default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg" | "default" |