> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/shadcn-ui/ui/llms.txt
> Use this file to discover all available pages before exploring further.

# Button Group

> Groups multiple buttons together with shared borders.

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npx shadcn@latest add button-group
  ```

  ```bash yarn theme={null}
  yarn dlx shadcn@latest add button-group
  ```

  ```bash pnpm theme={null}
  pnpm dlx shadcn@latest add button-group
  ```
</CodeGroup>

## Usage

```tsx theme={null}
import { ButtonGroup } from "@/components/ui/button-group"
import { Button } from "@/components/ui/button"
```

```tsx theme={null}
<ButtonGroup>
  <Button variant="outline">Left</Button>
  <Button variant="outline">Middle</Button>
  <Button variant="outline">Right</Button>
</ButtonGroup>
```

## Component Code

```tsx theme={null}
import { cva, type VariantProps } from "class-variance-authority"
import { Slot } from "radix-ui"
import { cn } from "@/lib/utils"
import { Separator } from "@/components/ui/separator"

const buttonGroupVariants = cva(
  "flex w-fit items-stretch has-[>[data-slot=button-group]]:gap-2 [&>*]:focus-visible:relative [&>*]:focus-visible:z-10",
  {
    variants: {
      orientation: {
        horizontal:
          "[&>*:not(:first-child)]:rounded-l-none [&>*:not(:first-child)]:border-l-0 [&>*:not(:last-child)]:rounded-r-none",
        vertical:
          "flex-col [&>*:not(:first-child)]:rounded-t-none [&>*:not(:first-child)]:border-t-0 [&>*:not(:last-child)]:rounded-b-none",
      },
    },
    defaultVariants: {
      orientation: "horizontal",
    },
  }
)

function ButtonGroup({
  className,
  orientation,
  ...props
}: React.ComponentProps<"div"> & VariantProps<typeof buttonGroupVariants>) {
  return (
    <div
      role="group"
      data-slot="button-group"
      data-orientation={orientation}
      className={cn(buttonGroupVariants({ orientation }), className)}
      {...props}
    />
  )
}

function ButtonGroupText({
  className,
  asChild = false,
  ...props
}: React.ComponentProps<"div"> & {
  asChild?: boolean
}) {
  const Comp = asChild ? Slot.Root : "div"

  return (
    <Comp
      className={cn(
        "flex items-center gap-2 rounded-md border bg-muted px-4 text-sm font-medium shadow-xs",
        className
      )}
      {...props}
    />
  )
}

function ButtonGroupSeparator({
  className,
  orientation = "vertical",
  ...props
}: React.ComponentProps<typeof Separator>) {
  return (
    <Separator
      data-slot="button-group-separator"
      orientation={orientation}
      className={cn(
        "relative m-0! self-stretch bg-input data-[orientation=vertical]:h-auto",
        className
      )}
      {...props}
    />
  )
}

export {
  ButtonGroup,
  ButtonGroupSeparator,
  ButtonGroupText,
  buttonGroupVariants,
}
```

## Examples

<Tabs>
  <Tab title="Basic">
    A basic button group with multiple buttons.

    ```tsx theme={null}
    <ButtonGroup>
      <Button variant="outline">Button 1</Button>
      <Button variant="outline">Button 2</Button>
      <Button variant="outline">Button 3</Button>
    </ButtonGroup>
    ```
  </Tab>

  <Tab title="Vertical">
    Use the `orientation="vertical"` prop for a vertical layout.

    ```tsx theme={null}
    <ButtonGroup orientation="vertical">
      <Button variant="outline">Top</Button>
      <Button variant="outline">Middle</Button>
      <Button variant="outline">Bottom</Button>
    </ButtonGroup>
    ```
  </Tab>

  <Tab title="With Separator">
    Use `ButtonGroupSeparator` to add visual separation.

    ```tsx theme={null}
    <ButtonGroup>
      <Button variant="outline">Left</Button>
      <ButtonGroupSeparator />
      <Button variant="outline">Right</Button>
    </ButtonGroup>
    ```
  </Tab>

  <Tab title="With Text">
    Use `ButtonGroupText` to add text labels.

    ```tsx theme={null}
    <ButtonGroup>
      <ButtonGroupText>Label</ButtonGroupText>
      <Button variant="outline">Action</Button>
    </ButtonGroup>
    ```
  </Tab>
</Tabs>

## Composition

Button groups work with any button variant and can be combined with other components like inputs and selects.

```tsx theme={null}
<ButtonGroup>
  <Button variant="outline">Button</Button>
  <Input type="text" placeholder="Search" />
  <Button variant="outline">Search</Button>
</ButtonGroup>
```

## API Reference

### ButtonGroup

| Prop          | Type                         | Default        |
| ------------- | ---------------------------- | -------------- |
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` |
| `className`   | `string`                     | -              |

### ButtonGroupText

| Prop        | Type      | Default |
| ----------- | --------- | ------- |
| `asChild`   | `boolean` | `false` |
| `className` | `string`  | -       |

### ButtonGroupSeparator

| Prop          | Type                         | Default      |
| ------------- | ---------------------------- | ------------ |
| `orientation` | `"horizontal" \| "vertical"` | `"vertical"` |
| `className`   | `string`                     | -            |
