Components
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>光标
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, IconGitFork } from "@tabler/icons-react"
圆角
使用 rounded-full 类来使按钮变为圆形。
import { Button } from "@/components/ui/button"
import { ArrowUpIcon } from "lucide-react"
加载
在按钮内部渲染 <Spinner /> 组件以显示加载状态。记得给加载动画添加 data-icon="inline-start" 或 data-icon="inline-end" 属性,以便正确设置间距。
import { Button } from "@/components/ui/button"
import { Spinner } from "@/components/ui/spinner"
按钮组
要创建按钮组,请使用 ButtonGroup 组件。更多详情请查看 按钮组 文档。
"use client"
import * as React from "react"链接
你可以在 <Button /> 组件上使用 render 属性,使其他组件看起来像按钮。下面是一个看起来像按钮的链接示例。
如果返回的不是按钮元素,记得将 nativeButton 属性设置为 false。
import { Button } from "@/components/ui/button"
export function ButtonRender() {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" |