# Badge

## Introduction

Badge is a small visual indicator that overlays another UI element to display a count, status, or label. It is commonly used to show unread notification counts, online/offline status indicators, and "NEW" labels on menu items or navigation elements. The Badge wraps a child component (such as an icon button or avatar) and positions itself in the top-right corner by default.

Badge supports multiple colors, variants, and sizes for flexible visual customization. It also provides utility features like a `max` prop for truncating large numbers (e.g., "99+"), a `showZero` prop for controlling zero-count visibility, and a dot-only mode when no `badgeContent` is provided.

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

| Field        | Description         | Default   |
| ------------ | ------------------- | --------- |
| badgeContent | 배지에 표시될 내용          | 4         |
| color        | 배지 색상               | "primary" |
| variant      | 배지 스타일 변형           | "solid"   |
| size         | 배지 크기               | "md"      |
| invisible    | 배지 숨김 여부            | —         |
| showZero     | 0일 때 표시 여부          | —         |
| max          | 최대 표시 숫자 (99 → 99+) | —         |

## Usage

```tsx
import { Badge, IconButton } from '@ceed/cds';
import NotificationsIcon from '@mui/icons-material/Notifications';

function MyComponent() {
  return (
    <Badge badgeContent={4} color="danger">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>
  );
}
```

## Basic

Basic Badge usage showing numeric counts, max overflow, and text content.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem"
    }}
  >
    <Badge badgeContent={4}>
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={100} max={99}>
      <IconButton>
        <EmailIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent="NEW">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>
  </div>
```

## Colors

Apply semantic colors to convey the badge's meaning: `primary` for general counts, `danger` for urgent notifications, `success` for online status, and `warning` for attention items.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem"
    }}
  >
    <Badge badgeContent={4} color="primary">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={4} color="neutral">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={4} color="danger">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={4} color="success">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={4} color="warning">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>
  </div>
```

## Variants

Four style variants are available: `solid` (filled), `soft` (subtle background), `outlined` (border only), and `plain` (minimal).

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem"
    }}
  >
    <Badge badgeContent={4} variant="solid">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={4} variant="soft">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={4} variant="outlined">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={4} variant="plain">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>
  </div>
```

## Sizes

Three sizes -- `sm`, `md`, and `lg` -- allow the badge to scale proportionally with its wrapped element.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem",
      alignItems: "center"
    }}
  >
    <Badge badgeContent={4} size="sm">
      <IconButton size="sm">
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={4} size="md">
      <IconButton size="md">
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={4} size="lg">
      <IconButton size="lg">
        <NotificationsIcon />
      </IconButton>
    </Badge>
  </div>
```

## With Avatar

Badge pairs naturally with Avatar to indicate user status (e.g., notification count or online indicator).

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem"
    }}
  >
    <Badge badgeContent={4} color="danger">
      <Avatar src="https://i.pravatar.cc/40?img=1" />
    </Badge>

    <Badge badgeContent="●" color="success">
      <Avatar src="https://i.pravatar.cc/40?img=2" />
    </Badge>
  </div>
```

## Dot Badge

When no `badgeContent` is provided, Badge renders as a small dot. This is useful for simple presence or status indicators without a specific count.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem"
    }}
  >
    <Badge variant="solid" color="danger">
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge variant="solid" color="success">
      <Avatar src="https://i.pravatar.cc/40?img=3" />
    </Badge>
  </div>
```

## Max Count

Use the `max` prop to cap the displayed number. When `badgeContent` exceeds `max`, it renders as "max+" (e.g., "99+").

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem"
    }}
  >
    <Badge badgeContent={99}>
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={100} max={99}>
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>

    <Badge badgeContent={1000} max={999}>
      <IconButton>
        <NotificationsIcon />
      </IconButton>
    </Badge>
  </div>
```

## Show Zero

By default, Badge hides when `badgeContent` is `0`. Use the `showZero` prop to force it to remain visible.

```tsx
<div
    style={{
      display: "flex",
      gap: "2rem"
    }}
  >
    <div>
      <p>Default (0 숨김)</p>
      <Badge badgeContent={0}>
        <IconButton>
          <NotificationsIcon />
        </IconButton>
      </Badge>
    </div>

    <div>
      <p>Show zero</p>
      <Badge badgeContent={0} showZero>
        <IconButton>
          <NotificationsIcon />
        </IconButton>
      </Badge>
    </div>
  </div>
```

## Common Use Cases

### Notification Bell

```tsx
import { Badge, IconButton } from '@ceed/cds';
import NotificationsIcon from '@mui/icons-material/Notifications';

function NotificationBell({ unreadCount }) {
  return (
    <Badge badgeContent={unreadCount} max={99} color="danger">
      <IconButton aria-label={`${unreadCount} unread notifications`}>
        <NotificationsIcon />
      </IconButton>
    </Badge>
  );
}
```

### Online Status on Avatar

```tsx
import { Badge, Avatar } from '@ceed/cds';

function UserAvatar({ name, avatarUrl, isOnline }) {
  return (
    <Badge
      color={isOnline ? 'success' : 'neutral'}
      variant="solid"
      anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
    >
      <Avatar src={avatarUrl} alt={name} />
    </Badge>
  );
}
```

### New Feature Label

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

function FeatureButton() {
  return (
    <Badge badgeContent="NEW" color="warning" size="sm">
      <Button variant="outlined">Analytics</Button>
    </Badge>
  );
}
```

## Best Practices

- **Use semantic colors consistently.** Reserve `danger` for urgent notifications, `success` for positive status (e.g., online), `warning` for attention-needed items, and `primary` for neutral counts.
  - ✔ `color="danger"` for unread message count
  - ✘ `color="primary"` for critical alert count

- **Set a `max` value for numeric badges.** Avoid displaying very large numbers like "1,247" in a tiny badge. Use `max={99}` or `max={999}` to keep the badge compact and readable.

- **Keep badge content short.** Badges work best with numbers (1-3 digits) or very short text ("NEW", "HOT"). Long text will overflow and look broken.

- **Use dot badges for binary states.** When the specific count does not matter (e.g., "has unread items" vs. "12 unread items"), use a dot badge by omitting `badgeContent`.

- **Do not badge everything.** Only add badges to elements where the count or status information is genuinely useful. Overuse diminishes their attention-grabbing effect.

## Props and Customization

### Key Props

| Prop           | Type                                                             | Default                                    | Description                                                           |
| -------------- | ---------------------------------------------------------------- | ------------------------------------------ | --------------------------------------------------------------------- |
| `badgeContent` | `ReactNode`                                                      | -                                          | Content displayed inside the badge (number, text). Omit for dot badge |
| `children`     | `ReactNode`                                                      | -                                          | The element the badge is attached to                                  |
| `color`        | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'`   | `'danger'`                                 | Badge color                                                           |
| `size`         | `'sm' \| 'md' \| 'lg'`                                           | `'md'`                                     | Badge size                                                            |
| `variant`      | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                     | `'solid'`                                  | Visual style                                                          |
| `max`          | `number`                                                         | `99`                                       | Maximum value before showing "N+"                                     |
| `invisible`    | `boolean`                                                        | `false`                                    | Hides the badge                                                       |
| `showZero`     | `boolean`                                                        | `false`                                    | Shows badge when value is 0                                           |
| `anchorOrigin` | `{ vertical: 'top' \| 'bottom'; horizontal: 'left' \| 'right' }` | `{ vertical: 'top', horizontal: 'right' }` | Badge position                                                        |
| `badgeInset`   | `string`                                                         | -                                          | Custom inset for badge positioning                                    |
| `sx`           | `SxProps`                                                        | -                                          | Custom styles using the MUI system                                    |

> **Note**: Badge also accepts all Joy UI Badge props.

## Accessibility

- Badge automatically applies `aria-label` or equivalent attributes so screen readers can announce the badge content alongside the wrapped element.
- When using Badge on an IconButton, always provide an `aria-label` on the IconButton that includes the badge context (e.g., `aria-label="5 unread notifications"`).
- Avoid relying solely on color to convey badge meaning. Pair colored badges with icons, text, or positional context so color-blind users can also interpret the status.
- Dot badges (without `badgeContent`) should have an accessible label on the parent element that describes the status (e.g., "User is online").
