Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Kbd
- Label
- Menubar
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Tooltip
- Typography
Get Started
import { Button } from "@/components/ui/button"
import { ArrowUpIcon } from "lucide-react"
安装
pnpm dlx shadcn@latest add button
用法
import { Button } from "@/components/ui/button"<Button variant="outline">Button</Button>光标
Tailwind v4 已将按钮组件的默认光标从 cursor: pointer 改为 cursor: default。
如果你想保留 cursor: pointer 的行为,请把下面的代码添加到你的 CSS 文件中:
@layer base {
button:not(:disabled),
[role="button"]:not(:disabled) {
cursor: pointer;
}
}示例
尺寸
使用 size 属性来改变按钮大小。
import { Button } from "@/components/ui/button"
import { ArrowUpRightIcon } from "lucide-react"
默认
import { Button } from "@/components/ui/button"
export function ButtonDefault() {描边
import { Button } from "@/components/ui/button"
export function ButtonOutline() {次要
import { Button } from "@/components/ui/button"
export function ButtonSecondary() {幽灵
import { Button } from "@/components/ui/button"
export function ButtonGhost() {危险
import { Button } from "@/components/ui/button"
export function ButtonDestructive() {链接
import { Button } from "@/components/ui/button"
export function ButtonLink() {图标
import { Button } from "@/components/ui/button"
import { CircleFadingArrowUpIcon } from "lucide-react"
带图标
记得给图标添加 data-icon="inline-start" 或 data-icon="inline-end" 属性,以获得正确的间距。
import { Button } from "@/components/ui/button"
import { IconGitBranch } from "@tabler/icons-react"
圆角
使用 rounded-full 类让按钮变成圆形。
import { Button } from "@/components/ui/button"
import { ArrowUpIcon } from "lucide-react"
加载状态
在按钮内部渲染 <Spinner /> 组件以显示加载状态。记得给 spinner 添加 data-icon="inline-start" 或 data-icon="inline-end" 属性,以获得正确的间距。
import { Button } from "@/components/ui/button"
import { Spinner } from "@/components/ui/spinner"
按钮组
要创建按钮组,请使用 ButtonGroup 组件。更多细节请参见 Button Group 文档。
"use client"
import * as React from "react"作为链接
你可以使用 buttonVariants 辅助函数让链接看起来像按钮。
不要把 <Button render={<a />} nativeButton={false} /> 用在链接上。 Radix UI 的 Button 组件总是会应用 role="button",这会覆盖 <a> 元素原本的语义链接角色。请改用 buttonVariants 搭配普通的 <a> 标签。
import Link from "next/link"
import { Button } from "@/components/ui/button"
RTL
要在 shadcn/ui 中启用 RTL 支持,请参见 RTL 配置指南。
"use client"
import { Button } from "@/examples/radix/ui-rtl/button"API 参考
Button
Button 组件是对 button 元素的封装,会添加多种样式和功能。
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "ghost" | "destructive" | "secondary" | "link" | "default" |
size | "default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg" | "default" |