111k

Button

显示一个按钮,或一个看起来像按钮的组件。

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 文件中:

globals.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, IconGitFork } 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} /> 用在链接上。 Base UI 的 Button 组件总是会应用 role="button",这会覆盖 <a> 元素原本的语义链接角色。请改用 buttonVariants 搭配普通的 <a> 标签。

"use client"

import { buttonVariants } from "@/components/ui/button"

RTL

要在 shadcn/ui 中启用 RTL 支持,请参见 RTL 配置指南

"use client"

import { Button } from "@/examples/base/ui-rtl/button"

API 参考

Button

Button 组件是对 button 元素的封装,会添加多种样式和功能。

PropTypeDefault
variant"default" | "outline" | "ghost" | "destructive" | "secondary" | "link""default"
size"default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg""default"