Files
shadcn-ui/apps/www/content/docs/components/button.mdx
2024-10-17 00:21:03 +04:00

151 lines
2.7 KiB
Plaintext

---
title: Button
description: Displays a button or a component that looks like a button.
featured: true
component: true
---
<ComponentPreview name="button-demo" description="A button" />
## Installation
<Tabs defaultValue="cli">
<TabsList>
<TabsTrigger value="cli">CLI</TabsTrigger>
<TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>
<TabsContent value="cli">
```bash
npx shadcn@latest add button
```
</TabsContent>
<TabsContent value="manual">
<Steps>
<Step>Install the following dependencies:</Step>
```bash
npm install @radix-ui/react-slot
```
<Step>Copy and paste the following code into your project.</Step>
<ComponentSource name="button" />
<Step>Update the import paths to match your project setup.</Step>
</Steps>
</TabsContent>
</Tabs>
## Usage
```tsx
import { Button } from "@/components/ui/button"
```
```tsx
<Button variant="outline">Button</Button>
```
## Link
You can use the `buttonVariants` helper to create a link that looks like a button.
```tsx
import { buttonVariants } from "@/components/ui/button"
```
```tsx
<Link className={buttonVariants({ variant: "outline" })}>Click here</Link>
```
Alternatively, you can set the `asChild` parameter and nest the link component.
```tsx
<Button asChild>
<Link href="/login">Login</Link>
</Button>
```
## Examples
### Primary
<ComponentPreview name="button-demo" description="A primary button" />
### Secondary
<ComponentPreview name="button-secondary" description="A secondary button" />
### Destructive
<ComponentPreview
name="button-destructive"
description="A destructive button"
/>
### Outline
<ComponentPreview
name="button-outline"
description="A button using the outline variant."
/>
### Ghost
<ComponentPreview
name="button-ghost"
description="A button using the ghost variant"
/>
### Link
<ComponentPreview
name="button-link"
description="A button using the link variant."
/>
### Icon
<ComponentPreview name="button-icon" description="An icon button" />
### With Icon
<ComponentPreview name="button-with-icon" description="A button with an icon" />
### Loading
<ComponentPreview
name="button-loading"
description="A button with a loading state."
/>
### As Child
<ComponentPreview
name="button-as-child"
description="A button wrapping a custom Link component"
/>
## Changelog
### 2024-10-16 Classes for icons
Added `gap-2 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0` to the button to automatically style icon inside the button.
Add the following classes to the `cva` call in your `button.tsx` file.
```diff showLineNumbers title="button.tsx"
const buttonVariants = cva(
"inline-flex ... gap-2 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0"
)
```