shadcn-templ 2.0 beta. Switch to templUI v1
1.7k

Button Group

A container that groups related buttons together with consistent styling.

package examples import (

Installation

shadcn-templ add button-group

Usage

import "github.com/axadrn/shadcn-templ/v2/components/buttongroup"
@buttongroup.ButtonGroup() {	@button.Button() {		Button 1	}	@button.Button() {		Button 2	}}

Composition

Use the following composition to build a ButtonGroup:

buttongroup.ButtonGroup├── button.Button or input.Input├── buttongroup.Separator└── buttongroup.Text

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.ButtonGroup(buttongroup.Props{Attributes: templ.Attributes{"aria-label": "Button group"}}) {	@button.Button() {		Button 1	}	@button.Button() {		Button 2	}}

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.

Orientation

Set the Orientation prop to change the button group layout.

package examples import (

Size

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

package examples import (

Nested

Nest ButtonGroup components to create button groups with spacing.

package examples import (

Separator

The buttongroup.Separator 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.

package examples import (

Split

Create a split button group by adding two buttons separated by a buttongroup.Separator.

package examples import (

Input

Wrap an Input component with buttons.

package examples import (

Input Group

Wrap an InputGroup component to create complex input layouts.

package examples import (

Create a split button group with a DropdownMenu component.

package examples import (

Select

Pair with a Select component.

package examples import (

Popover

Use with a Popover component.

package examples import (

API Reference

ButtonGroup

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

Prop Type Default
Orientation OrientationHorizontal | OrientationVertical OrientationHorizontal
Class string -
@buttongroup.ButtonGroup() {	@button.Button() {		Button 1	}	@button.Button() {		Button 2	}}

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

@buttongroup.ButtonGroup() {	@buttongroup.ButtonGroup()	@buttongroup.ButtonGroup()}

Separator

The buttongroup.Separator component visually divides buttons within a group.

Prop Type Default
Orientation OrientationHorizontal | OrientationVertical OrientationVertical
Class string -
@buttongroup.ButtonGroup() {	@button.Button() {		Button 1	}	@buttongroup.Separator()	@button.Button() {		Button 2	}}

Text

Use the buttongroup.Text component to display text within a button group.

Prop Type Default
Class string -
@buttongroup.ButtonGroup() {	@buttongroup.Text() {		Text	}	@button.Button() {		Button	}}

Render a label.Label inside it to label an adjacent input.

@buttongroup.ButtonGroup() {	@buttongroup.Text() {		@label.Label(label.Props{For: "name"}) {			Text		}	}	@input.Input(input.Props{ID: "name", Placeholder: "Type something here..."})}