# IconButton

## Introduction

IconButton is a compact, icon-only button component designed for actions where a visual symbol alone is sufficient to communicate meaning. It is ideal for toolbars, table row actions, card headers, navigation bars, and any context where space efficiency is critical.

Because IconButton omits visible text, it relies entirely on the icon's clarity and supplementary accessibility attributes to convey its purpose. Always pair it with `aria-label` or a Tooltip to ensure all users can understand the button's function.

```tsx
<IconButton {...args} />
```

| Field    | Description | Default   |
| -------- | ----------- | --------- |
| children | 아이콘 요소      | \<Add />  |
| color    | 버튼 색상       | "primary" |
| variant  | 버튼 스타일 변형   | "solid"   |
| size     | 버튼 크기       | "md"      |
| disabled | 비활성화 상태     | —         |
| loading  | 로딩 상태       | —         |

## Usage

```tsx
import { IconButton } from '@ceed/cds';
import AddIcon from '@mui/icons-material/Add';
import DeleteIcon from '@mui/icons-material/Delete';

function MyComponent() {
  return (
    <div style={{ display: 'flex', gap: '0.5rem' }}>
      <IconButton aria-label="Add item" onClick={handleAdd}>
        <AddIcon />
      </IconButton>

      <IconButton color="danger" aria-label="Delete item" onClick={handleDelete}>
        <DeleteIcon />
      </IconButton>
    </div>
  );
}
```

## Colors

IconButton supports five semantic colors: `primary` (default), `neutral`, `danger`, `success`, and `warning`. Choose a color that reflects the nature of the action.

```tsx
<div
    style={{
      display: "flex",
      gap: "1rem",
      alignItems: "center"
    }}
  >
    <IconButton color="primary">
      <Add />
    </IconButton>
    <IconButton color="neutral">
      <Edit />
    </IconButton>
    <IconButton color="danger">
      <Delete />
    </IconButton>
    <IconButton color="success">
      <FavoriteIcon />
    </IconButton>
    <IconButton color="warning">
      <Settings />
    </IconButton>
  </div>
```

## Variants

Four visual variants are available: `solid` (filled background), `soft` (tinted background), `outlined` (border only), and `plain` (no background). Use `outlined` or `plain` for secondary actions, and `solid` for primary or high-emphasis actions.

```tsx
<div
    style={{
      display: "flex",
      gap: "1rem",
      alignItems: "center"
    }}
  >
    <IconButton variant="solid">
      <Add />
    </IconButton>
    <IconButton variant="soft">
      <Add />
    </IconButton>
    <IconButton variant="outlined">
      <Add />
    </IconButton>
    <IconButton variant="plain">
      <Add />
    </IconButton>
  </div>
```

## Sizes

The `size` prop controls the button dimensions. Available sizes are `sm`, `md` (default), and `lg`. Ensure touch targets meet the minimum 44px recommended size for touch interfaces.

```tsx
<div
    style={{
      display: "flex",
      gap: "1rem",
      alignItems: "center"
    }}
  >
    <IconButton size="sm">
      <Add />
    </IconButton>
    <IconButton size="md">
      <Add />
    </IconButton>
    <IconButton size="lg">
      <Add />
    </IconButton>
  </div>
```

## States

IconButton supports multiple interactive states including normal, disabled, and loading. Use `disabled` to prevent interaction and `loading` to indicate an in-progress async operation.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem",
      alignItems: "center"
    }}
  >
    <div>
      <h4>Normal</h4>
      <div
        style={{
          display: "flex",
          gap: "1rem",
          alignItems: "center"
        }}
      >
        <IconButton>
          <Add />
        </IconButton>
        <IconButton color="danger">
          <Delete />
        </IconButton>
      </div>
    </div>
    <div>
      <h4>Disabled</h4>
      <div
        style={{
          display: "flex",
          gap: "1rem",
          alignItems: "center"
        }}
      >
        <IconButton disabled>
          <Add />
        </IconButton>
        <IconButton disabled color="danger">
          <Delete />
        </IconButton>
      </div>
    </div>
    <div>
      <h4>Loading</h4>
      <div
        style={{
          display: "flex",
          gap: "1rem",
          alignItems: "center"
        }}
      >
        <IconButton loading>
          <Add />
        </IconButton>
        <IconButton loading color="danger">
          <Delete />
        </IconButton>
      </div>
    </div>
  </div>
```

## With Badge

Combine IconButton with Badge to display notification counts, status indicators, or unread markers alongside the button.

```tsx
<div
    style={{
      display: "flex",
      gap: "1rem",
      alignItems: "center"
    }}
  >
    <Badge badgeContent={4} color="danger">
      <IconButton>
        <Settings />
      </IconButton>
    </Badge>
    <Badge badgeContent="99+" color="warning">
      <IconButton color="neutral">
        <MoreVertIcon />
      </IconButton>
    </Badge>
    <Badge variant="solid" color="success">
      <IconButton color="primary">
        <FavoriteIcon />
      </IconButton>
    </Badge>
  </div>
```

## With Tooltip

Wrap IconButton in a Tooltip to provide a text description on hover. This is especially important for icon-only buttons since the icon alone may not be self-explanatory for all users.

```tsx
<div
    style={{
      display: "flex",
      gap: "1rem",
      alignItems: "center"
    }}
  >
    <Tooltip title="새 항목 추가">
      <IconButton>
        <Add />
      </IconButton>
    </Tooltip>
    <Tooltip title="편집">
      <IconButton color="neutral">
        <Edit />
      </IconButton>
    </Tooltip>
    <Tooltip title="삭제">
      <IconButton color="danger">
        <Delete />
      </IconButton>
    </Tooltip>
    <Tooltip title="공유">
      <IconButton color="success">
        <ShareIcon />
      </IconButton>
    </Tooltip>
  </div>
```

## Circular Variants

IconButton renders as a circular shape by default, making it visually distinct from rectangular text buttons and well-suited for floating action buttons and compact action rows.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem",
      alignItems: "center"
    }}
  >
    <div>
      <h4>Default</h4>
      <div
        style={{
          display: "flex",
          gap: "1rem",
          alignItems: "center"
        }}
      >
        <IconButton>
          <Add />
        </IconButton>
        <IconButton variant="outlined">
          <Edit />
        </IconButton>
        <IconButton variant="soft">
          <Delete />
        </IconButton>
      </div>
    </div>
  </div>
```

## Action Buttons

A practical example of IconButton used for common CRUD operations with distinct colors signaling the nature of each action.

```tsx
<div
    style={{
      display: "flex",
      gap: "1rem",
      alignItems: "center"
    }}
  >
    <IconButton onClick={() => alert("Add clicked!")} title="Add new item">
      <Add />
    </IconButton>
    <IconButton onClick={() => alert("Edit clicked!")} color="neutral" title="Edit item">
      <Edit />
    </IconButton>
    <IconButton onClick={() => alert("Delete clicked!")} color="danger" title="Delete item">
      <Delete />
    </IconButton>
    <IconButton onClick={() => alert("Share clicked!")} color="success" title="Share item">
      <ShareIcon />
    </IconButton>
    <IconButton
      onClick={() => alert("Settings clicked!")}
      color="neutral"
      variant="outlined"
      title="Settings"
    >
      <Settings />
    </IconButton>
  </div>
```

## Common Use Cases

### Table Row Actions

```tsx
import { IconButton, Tooltip } from '@ceed/cds';
import EditIcon from '@mui/icons-material/Edit';
import DeleteIcon from '@mui/icons-material/Delete';
import VisibilityIcon from '@mui/icons-material/Visibility';

function RowActions({ onView, onEdit, onDelete }) {
  return (
    <div style={{ display: 'flex', gap: '0.25rem' }}>
      <Tooltip title="View details">
        <IconButton size="sm" variant="plain" color="neutral" onClick={onView}>
          <VisibilityIcon />
        </IconButton>
      </Tooltip>
      <Tooltip title="Edit">
        <IconButton size="sm" variant="plain" color="neutral" onClick={onEdit}>
          <EditIcon />
        </IconButton>
      </Tooltip>
      <Tooltip title="Delete">
        <IconButton size="sm" variant="plain" color="danger" onClick={onDelete}>
          <DeleteIcon />
        </IconButton>
      </Tooltip>
    </div>
  );
}
```

### Dialog Close Button

```tsx
import CloseIcon from '@mui/icons-material/Close';

<IconButton
  variant="plain"
  color="neutral"
  size="sm"
  aria-label="Close dialog"
  onClick={onClose}
  sx={{ position: 'absolute', top: 8, right: 8 }}
>
  <CloseIcon />
</IconButton>;
```

### Navigation Back Button

```tsx
import ArrowBackIcon from '@mui/icons-material/ArrowBack';

<IconButton variant="outlined" color="neutral" aria-label="Go back" onClick={() => router.back()}>
  <ArrowBackIcon />
</IconButton>;
```

## Best Practices

1. **Always provide an accessible label.** Since there is no visible text, use `aria-label` or wrap with a Tooltip to describe the action.

```tsx
// ✅ Accessible icon button
<IconButton aria-label="Delete item" color="danger">
  <DeleteIcon />
</IconButton>

// ❌ No accessible label
<IconButton color="danger">
  <DeleteIcon />
</IconButton>
```

2. **Use semantic colors to convey intent.** Match the color to the action's nature: `danger` for destructive actions, `success` for confirmations, `neutral` for general-purpose actions.

```tsx
// ✅ Color matches the action
<IconButton color="danger" aria-label="Delete">
  <DeleteIcon />
</IconButton>

// ❌ Misleading color
<IconButton color="success" aria-label="Delete">
  <DeleteIcon />
</IconButton>
```

3. **Use the `loading` prop for async operations.** This prevents double-clicks and provides visual feedback during network requests or processing.

```tsx
// ✅ Shows loading spinner during async action
<IconButton loading={isSaving} aria-label="Save" onClick={handleSave}>
  <SaveIcon />
</IconButton>
```

4. **Prefer universally recognized icons.** Choose icons that are widely understood (e.g., trash can for delete, pencil for edit, plus for add). Avoid ambiguous symbols without a Tooltip.

5. **Maintain consistent sizing within the same context.** Use the same `size` and `variant` for all IconButtons in a toolbar or action group to keep the interface clean and predictable.

## Props and Customization

### Key Props

| Prop        | Type                                                           | Default     | Description                              |
| ----------- | -------------------------------------------------------------- | ----------- | ---------------------------------------- |
| `children`  | `ReactNode`                                                    | -           | Icon element to render inside the button |
| `variant`   | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                   | `'plain'`   | Visual style                             |
| `size`      | `'sm' \| 'md' \| 'lg'`                                         | `'md'`      | Button size                              |
| `color`     | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'` | `'neutral'` | Color scheme                             |
| `disabled`  | `boolean`                                                      | `false`     | Disables the button                      |
| `loading`   | `boolean`                                                      | `false`     | Shows a loading indicator                |
| `component` | `ElementType`                                                  | `'button'`  | Root element type (polymorphic)          |
| `onClick`   | `(event: MouseEvent) => void`                                  | -           | Click event handler                      |
| `sx`        | `SxProps`                                                      | -           | Custom styles using the MUI system       |

> **Note**: IconButton also accepts all Joy UI IconButton props and Framer Motion props.

## Accessibility

- **`aria-label` is essential**: Every IconButton must have either an `aria-label`, `aria-labelledby`, or a wrapping Tooltip with `title` to provide a text alternative for screen readers.
- **Keyboard interaction**: IconButton is focusable via `Tab` and can be activated with `Enter` or `Space`. Focus indicators are displayed by default.
- **Disabled state**: When `disabled` is set, the button receives `aria-disabled="true"` and becomes non-interactive. It remains visible in the DOM for screen readers to announce.
- **Touch targets**: Ensure the rendered button is at least 44x44 pixels for touch accessibility. The `sm` size may need additional padding in touch-heavy interfaces.
