mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-22 12:15:43 +00:00
139 lines
2.9 KiB
Plaintext
139 lines
2.9 KiB
Plaintext
---
|
|
title: Toggle Group
|
|
description: A set of two-state buttons that can be toggled on or off.
|
|
component: true
|
|
links:
|
|
doc: https://www.radix-ui.com/docs/primitives/components/toggle-group
|
|
api: https://www.radix-ui.com/docs/primitives/components/toggle-group#api-reference
|
|
---
|
|
|
|
<ComponentPreview name="toggle-group-spacing" />
|
|
|
|
## Installation
|
|
|
|
<CodeTabs>
|
|
|
|
<TabsList>
|
|
<TabsTrigger value="cli">CLI</TabsTrigger>
|
|
<TabsTrigger value="manual">Manual</TabsTrigger>
|
|
</TabsList>
|
|
<TabsContent value="cli">
|
|
|
|
```bash
|
|
npx shadcn@latest add toggle-group
|
|
```
|
|
|
|
</TabsContent>
|
|
|
|
<TabsContent value="manual">
|
|
|
|
<Steps>
|
|
|
|
<Step>Install the following dependencies:</Step>
|
|
|
|
```bash
|
|
npm install @radix-ui/react-toggle-group
|
|
```
|
|
|
|
<Step>Copy and paste the following code into your project.</Step>
|
|
|
|
<ComponentSource name="toggle-group" title="components/ui/toggle-group.tsx" />
|
|
|
|
<Step>Update the import paths to match your project setup.</Step>
|
|
|
|
</Steps>
|
|
|
|
</TabsContent>
|
|
|
|
</CodeTabs>
|
|
|
|
## Usage
|
|
|
|
```tsx
|
|
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
|
|
```
|
|
|
|
```tsx
|
|
<ToggleGroup type="single">
|
|
<ToggleGroupItem value="a">A</ToggleGroupItem>
|
|
<ToggleGroupItem value="b">B</ToggleGroupItem>
|
|
<ToggleGroupItem value="c">C</ToggleGroupItem>
|
|
</ToggleGroup>
|
|
```
|
|
|
|
## Examples
|
|
|
|
### Outline
|
|
|
|
<ComponentPreview
|
|
name="toggle-group-outline"
|
|
description="A toggle group using the outline variant."
|
|
/>
|
|
|
|
### Single
|
|
|
|
<ComponentPreview
|
|
name="toggle-group-single"
|
|
description="A toggle group with single selection."
|
|
/>
|
|
|
|
### Small
|
|
|
|
<ComponentPreview
|
|
name="toggle-group-sm"
|
|
description="A toggle group using the small size."
|
|
/>
|
|
|
|
### Large
|
|
|
|
<ComponentPreview
|
|
name="toggle-group-lg"
|
|
description="A toggle group using the large size."
|
|
/>
|
|
|
|
### Disabled
|
|
|
|
<ComponentPreview
|
|
name="toggle-group-disabled"
|
|
description="A disabled toggle group."
|
|
/>
|
|
|
|
### Spacing
|
|
|
|
Use `spacing={2}` to add spacing between toggle group items.
|
|
|
|
<ComponentPreview
|
|
name="toggle-group-spacing"
|
|
description="A toggle group with spacing."
|
|
/>
|
|
|
|
## API Reference
|
|
|
|
### ToggleGroup
|
|
|
|
The main component that wraps toggle group items.
|
|
|
|
| Prop | Type | Default |
|
|
| ----------- | --------------------------- | ----------- |
|
|
| `type` | `"single" \| "multiple"` | `"single"` |
|
|
| `variant` | `"default" \| "outline"` | `"default"` |
|
|
| `size` | `"default" \| "sm" \| "lg"` | `"default"` |
|
|
| `spacing` | `number` | `0` |
|
|
| `className` | `string` | |
|
|
|
|
```tsx
|
|
<ToggleGroup type="single" variant="outline" size="sm">
|
|
<ToggleGroupItem value="a">A</ToggleGroupItem>
|
|
<ToggleGroupItem value="b">B</ToggleGroupItem>
|
|
</ToggleGroup>
|
|
```
|
|
|
|
### ToggleGroupItem
|
|
|
|
Individual toggle items within a toggle group. Remember to add an `aria-label` to each item for accessibility.
|
|
|
|
| Prop | Type | Default |
|
|
| ----------- | -------- | -------- |
|
|
| `value` | `string` | Required |
|
|
| `className` | `string` | |
|