--- title: Button Group description: A container that groups related buttons together with consistent styling. component: true --- ## Installation CLI Manual ```bash npx shadcn@latest add button-group ``` Install the following dependencies: ```bash npm install @radix-ui/react-slot ``` Copy and paste the following code into your project. Update the import paths to match your project setup. ## Usage ```tsx import { ButtonGroup, ButtonGroupSeparator, ButtonGroupText, } from "@/components/ui/button-group" ``` ```tsx ``` ## Accessibility - The `ButtonGroup` component has the `role` attribute set to `group`. - Use Tab to navigate between the buttons in the group. - Use `aria-label` or `aria-labelledby` to label the button group. ```tsx showLineNumbers ``` ## ButtonGroup vs ToggleGroup - Use the `ButtonGroup` component when you want to group buttons that perform an action. - Use the `ToggleGroup` component when you want to group buttons that toggle a state. ## Examples ### Orientation Set the `orientation` prop to change the button group layout. ### Size Control the size of buttons using the `size` prop on individual buttons. ### Nested Nest `` components to create button groups with spacing. ### Separator The `ButtonGroupSeparator` component visually divides buttons within a group. Buttons with variant `outline` do not need a separator since they have a border. For other variants, a separator is recommended to improve the visual hierarchy. ### Split Create a split button group by adding two buttons separated by a `ButtonGroupSeparator`. ### Input Wrap an `Input` component with buttons. ### Input Group Wrap an `InputGroup` component to create complex input layouts. ### Dropdown Menu Create a split button group with a `DropdownMenu` component. ### Select Pair with a `Select` component. ### Popover Use with a `Popover` component. ## API Reference ### ButtonGroup The `ButtonGroup` component is a container that groups related buttons together with consistent styling. | Prop | Type | Default | | ------------- | ---------------------------- | -------------- | | `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | ```tsx ``` Nest multiple button groups to create complex layouts with spacing. See the [nested](#nested) example for more details. ```tsx ``` ### ButtonGroupSeparator The `ButtonGroupSeparator` component visually divides buttons within a group. | Prop | Type | Default | | ------------- | ---------------------------- | ------------ | | `orientation` | `"horizontal" \| "vertical"` | `"vertical"` | ```tsx ``` ### ButtonGroupText Use this component to display text within a button group. | Prop | Type | Default | | --------- | --------- | ------- | | `asChild` | `boolean` | `false` | ```tsx Text ``` Use the `asChild` prop to render a custom component as the text, for example a label. ```tsx showLineNumbers import { ButtonGroupText } from "@/components/ui/button-group" import { Label } from "@/components/ui/label" export function ButtonGroupTextDemo() { return ( ) } ```