# ButtonGroup

## Introduction

ButtonGroup is a layout component that visually and semantically groups related buttons into a single cohesive unit. It automatically handles border merging, spacing, and consistent styling across all child buttons, making it ideal for toolbars, action sets, and segmented controls.

By applying shared props like `color`, `variant`, `size`, and `orientation` at the group level, you ensure visual consistency without having to repeat props on every individual button. ButtonGroup is built on top of Joy UI's ButtonGroup and inherits all of its capabilities.

```tsx
<ButtonGroup {...args}>
    <Button>첫번째</Button>
    <Button>두번째</Button>
    <Button>세번째</Button>
  </ButtonGroup>
```

| Field       | Description | Default      |
| ----------- | ----------- | ------------ |
| color       | 버튼 그룹 색상    | "primary"    |
| variant     | 버튼 스타일 변형   | "solid"      |
| size        | 버튼 크기       | "md"         |
| orientation | 방향          | "horizontal" |
| disabled    | 비활성화 상태     | false        |

## Usage

```tsx
import { ButtonGroup, Button } from '@ceed/cds';

function MyComponent() {
  return (
    <ButtonGroup>
      <Button>First</Button>
      <Button>Second</Button>
      <Button>Third</Button>
    </ButtonGroup>
  );
}
```

## Colors

Apply a unified color theme to all buttons in the group using the `color` prop. Available colors are `primary`, `neutral`, `danger`, `success`, and `warning`.

```tsx
<div
    style={{
      display: "flex",
      gap: "1rem",
      flexDirection: "column"
    }}
  >
    <ButtonGroup color="primary">
      <Button>Primary</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>

    <ButtonGroup color="neutral">
      <Button>Neutral</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>

    <ButtonGroup color="danger">
      <Button>Danger</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>

    <ButtonGroup color="success">
      <Button>Success</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>

    <ButtonGroup color="warning">
      <Button>Warning</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>
  </div>
```

## Variants

ButtonGroup supports four visual variants: `solid`, `soft`, `outlined`, and `plain`. The variant prop propagates to all child buttons, ensuring a consistent look.

```tsx
<div
    style={{
      display: "flex",
      gap: "1rem",
      flexDirection: "column"
    }}
  >
    <ButtonGroup variant="solid">
      <Button>Solid</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>

    <ButtonGroup variant="soft">
      <Button>Soft</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>

    <ButtonGroup variant="outlined">
      <Button>Outlined</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>

    <ButtonGroup variant="plain">
      <Button>Plain</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>
  </div>
```

## Sizes

Control the size of all buttons in the group with the `size` prop. Available sizes are `sm`, `md` (default), and `lg`.

```tsx
<div
    style={{
      display: "flex",
      gap: "1rem",
      flexDirection: "column",
      alignItems: "flex-start"
    }}
  >
    <ButtonGroup size="sm">
      <Button>Small</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>

    <ButtonGroup size="md">
      <Button>Medium</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>

    <ButtonGroup size="lg">
      <Button>Large</Button>
      <Button>Button</Button>
      <Button>Group</Button>
    </ButtonGroup>
  </div>
```

## Orientations

Buttons can be arranged horizontally (default) or vertically. Use `orientation="vertical"` for stacked layouts such as side panels or narrow containers.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem",
      alignItems: "flex-start"
    }}
  >
    <div>
      <h4>Horizontal</h4>
      <ButtonGroup orientation="horizontal">
        <Button>첫번째</Button>
        <Button>두번째</Button>
        <Button>세번째</Button>
      </ButtonGroup>
    </div>

    <div>
      <h4>Vertical</h4>
      <ButtonGroup orientation="vertical">
        <Button>첫번째</Button>
        <Button>두번째</Button>
        <Button>세번째</Button>
      </ButtonGroup>
    </div>
  </div>
```

## With Icons

Combine text labels with icons using the `startDecorator` prop on individual buttons for a richer, more descriptive interface.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem",
      flexDirection: "column"
    }}
  >
    <ButtonGroup>
      <Button startDecorator={<Add />}>추가</Button>
      <Button startDecorator={<Edit />}>편집</Button>
      <Button startDecorator={<Delete />}>삭제</Button>
    </ButtonGroup>

    <ButtonGroup variant="outlined">
      <Button startDecorator={<Save />} />
      <Button startDecorator={<Share />} />
      <Button startDecorator={<Edit />} />
    </ButtonGroup>
  </div>
```

## States

ButtonGroup supports disabled states at the group level (affecting all children) or on individual buttons for mixed-state scenarios.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem",
      flexDirection: "column"
    }}
  >
    <div>
      <h4>Normal</h4>
      <ButtonGroup>
        <Button>첫번째</Button>
        <Button>두번째</Button>
        <Button>세번째</Button>
      </ButtonGroup>
    </div>

    <div>
      <h4>Disabled</h4>
      <ButtonGroup disabled>
        <Button>첫번째</Button>
        <Button>두번째</Button>
        <Button>세번째</Button>
      </ButtonGroup>
    </div>

    <div>
      <h4>Mixed States</h4>
      <ButtonGroup>
        <Button>활성</Button>
        <Button disabled>비활성</Button>
        <Button>활성</Button>
      </ButtonGroup>
    </div>
  </div>
```

## Toggle Group

By dynamically switching the `variant` prop on individual buttons based on selection state, you can create a segmented toggle control for exclusive selection patterns.

```tsx
<div>
    <h4>Toggle Group (Alignment)</h4>
    <ButtonGroup variant="outlined">
      <Button
        variant={selected === "left" ? "solid" : "outlined"}
        onClick={() => setSelected("left")}
      >
        왼쪽 정렬
      </Button>
      <Button
        variant={selected === "center" ? "solid" : "outlined"}
        onClick={() => setSelected("center")}
      >
        가운데 정렬
      </Button>
      <Button
        variant={selected === "right" ? "solid" : "outlined"}
        onClick={() => setSelected("right")}
      >
        오른쪽 정렬
      </Button>
    </ButtonGroup>
    <p>선택된 정렬: {selected}</p>
  </div>
```

## Common Use Cases

### Toolbar Actions

```tsx
import { ButtonGroup, Button } from '@ceed/cds';
import SaveIcon from '@mui/icons-material/Save';
import UndoIcon from '@mui/icons-material/Undo';
import RedoIcon from '@mui/icons-material/Redo';

<ButtonGroup variant="outlined">
  <Button startDecorator={<SaveIcon />}>Save</Button>
  <Button startDecorator={<UndoIcon />}>Undo</Button>
  <Button startDecorator={<RedoIcon />}>Redo</Button>
</ButtonGroup>;
```

### View Switcher

```tsx
const [view, setView] = useState('list');

<ButtonGroup variant="outlined" color="neutral">
  <Button variant={view === 'list' ? 'solid' : 'outlined'} onClick={() => setView('list')}>
    List
  </Button>
  <Button variant={view === 'grid' ? 'solid' : 'outlined'} onClick={() => setView('grid')}>
    Grid
  </Button>
  <Button variant={view === 'card' ? 'solid' : 'outlined'} onClick={() => setView('card')}>
    Card
  </Button>
</ButtonGroup>;
```

### Pagination Controls

```tsx
<ButtonGroup variant="outlined" size="sm">
  <Button disabled={page === 1} onClick={() => setPage(page - 1)}>
    Previous
  </Button>
  <Button disabled>{page}</Button>
  <Button onClick={() => setPage(page + 1)}>Next</Button>
</ButtonGroup>
```

## Best Practices

1. **Group only related actions together.** Each ButtonGroup should represent a logically connected set of operations.

```tsx
// ✅ Related file operations grouped together
<ButtonGroup variant="outlined">
  <Button>Save</Button>
  <Button>Save As</Button>
  <Button>Export</Button>
</ButtonGroup>

// ❌ Unrelated actions mixed in one group
<ButtonGroup>
  <Button>Save</Button>
  <Button>Delete Account</Button>
  <Button>Help</Button>
</ButtonGroup>
```

2. **Keep the button count between 2 and 5.** Too many buttons reduce clarity and overwhelm users.

```tsx
// ✅ Concise group
<ButtonGroup>
  <Button>Bold</Button>
  <Button>Italic</Button>
  <Button>Underline</Button>
</ButtonGroup>

// ❌ Too many options in a single group
<ButtonGroup>
  <Button>Bold</Button>
  <Button>Italic</Button>
  <Button>Underline</Button>
  <Button>Strikethrough</Button>
  <Button>Superscript</Button>
  <Button>Subscript</Button>
  <Button>Code</Button>
</ButtonGroup>
```

3. **Use consistent variant and color across the group.** Avoid overriding individual button styles unless implementing toggle selection.

```tsx
// ✅ Consistent styling via group props
<ButtonGroup variant="outlined" color="neutral">
  <Button>A</Button>
  <Button>B</Button>
</ButtonGroup>

// ❌ Mixed styles with no semantic reason
<ButtonGroup>
  <Button variant="solid">A</Button>
  <Button variant="outlined">B</Button>
  <Button variant="plain">C</Button>
</ButtonGroup>
```

4. **Clearly indicate the active state in toggle groups.** Use a distinct variant (e.g., `solid` vs `outlined`) so users can immediately see which option is selected.

5. **Consider vertical orientation for narrow layouts.** On mobile or in sidebars, `orientation="vertical"` prevents horizontal overflow and improves usability.

## Props and Customization

### Key Props

| Prop          | Type                                                           | Default        | Description                         |
| ------------- | -------------------------------------------------------------- | -------------- | ----------------------------------- |
| `children`    | `ReactNode`                                                    | -              | Button elements inside the group    |
| `variant`     | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                   | `'outlined'`   | Visual style applied to all buttons |
| `size`        | `'sm' \| 'md' \| 'lg'`                                         | `'md'`         | Size applied to all buttons         |
| `color`       | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'` | `'neutral'`    | Color applied to all buttons        |
| `disabled`    | `boolean`                                                      | `false`        | Disables all buttons in the group   |
| `orientation` | `'horizontal' \| 'vertical'`                                   | `'horizontal'` | Layout direction                    |
| `spacing`     | `number \| string`                                             | `0`            | Gap between buttons                 |
| `sx`          | `SxProps`                                                      | -              | Custom styles using the MUI system  |

> **Note**: ButtonGroup accepts all Joy UI ButtonGroup props.

## Accessibility

- **Keyboard navigation**: All buttons within the group are focusable and navigable using the `Tab` key. Each button is a standard focusable element that responds to `Enter` and `Space`.
- **ARIA grouping**: Wrap the ButtonGroup in a container with `role="group"` and an `aria-label` when the group represents a distinct set of controls (e.g., text formatting toolbar).
- **Disabled state**: When the entire group is disabled, all child buttons receive `aria-disabled="true"` automatically, preventing interaction and communicating the state to assistive technologies.
- **Toggle groups**: When using ButtonGroup as a toggle, consider adding `aria-pressed` to each button to communicate the selection state to screen readers.
