Button Group

A container that groups related buttons together with consistent styling.

@@directive("'use client'")

@react.component

Installation

npx shadcn@latest add @rescript-shadcn/ButtonGroup

Usage

<ButtonGroup>
  <Button> {"Button 1"->React.string} </Button>
  <Button> {"Button 2"->React.string} </Button>
</ButtonGroup>

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.
<ButtonGroup aria-label="Button group">
  <Button> {"Button 1"->React.string} </Button>
  <Button> {"Button 2"->React.string} </Button>
</ButtonGroup>

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.

@react.component
let make = () =>
  <ButtonGroup orientation=Vertical ariaLabel="Media controls" className="h-fit">

Size

Control the size of buttons using the size prop on individual buttons.

@react.component
let make = () =>
  <div className="flex flex-col items-start gap-8">

Nested

Nest <ButtonGroup> components to create button groups with spacing.

@react.component
let make = () =>
  <ButtonGroup>

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.

@react.component
let make = () =>
  <ButtonGroup>

Split

Create a split button group by adding two buttons separated by a ButtonGroupSeparator.

module TablerIcons = {
  type props = {className?: string}

Input

Wrap an Input component with buttons.

@react.component
let make = () =>
  <ButtonGroup>

Input Group

Wrap an InputGroup component to create complex input layouts.

@@directive("'use client'")

@react.component

Create a split button group with a DropdownMenu component.

@@directive("'use client'")

module LocalIcons = {

Select

Pair with a Select component.

@@directive("'use client'")

let currencies: array<BaseUi.Select.Item.t<string>> = [

Popover

Use with a Popover component.

@react.component
let make = () => {
  <ButtonGroup>

API Reference

ButtonGroup

The ButtonGroup component is a container that groups related buttons together with consistent styling.

PropTypeDefault
orientationHorizontal | VerticalHorizontal
<ButtonGroup>
  <Button> {"Button 1"->React.string} </Button>
  <Button> {"Button 2"->React.string} </Button>
</ButtonGroup>

Nest multiple button groups to create complex layouts with spacing. See the nested example for more details.

<ButtonGroup>
  <ButtonGroup />
  <ButtonGroup />
</ButtonGroup>

ButtonGroupSeparator

The ButtonGroupSeparator component visually divides buttons within a group.

PropTypeDefault
orientationHorizontal | VerticalVertical
<ButtonGroup>
  <Button>Button 1</Button>
  <ButtonGroupSeparator />
  <Button>Button 2</Button>
</ButtonGroup>

ButtonGroupText

Use this component to display text within a button group.

PropTypeDefault
renderReact.element
<ButtonGroup>
  <ButtonGroupText>Text</ButtonGroupText>
  <Button>Button</Button>
</ButtonGroup>

Use the render prop to render a custom component as the text, for example a label.

 
 
module ButtonGroupTextDemo = {
  @react.component
  let make = () => {
    <ButtonGroup>
      <ButtonGroupText render={<Label htmlFor="name"/>}>
        {"Text"->React.string}
      </ButtonGroupText>
      <Input placeholder="Type something here..." id="name" />
    </ButtonGroup>
  }
}