# Count badge

> A small ink capsule with a number, or a dot, that sits on the corner of a tab or toolbar icon to say something is waiting, and blurs away when it's cleared.

Docs: https://plumbago.ochotzas.com/components/count-badge

## Installation

```sh
npx plumbago add count-badge
```

This copies `components/ui/count-badge.tsx` into your project, along with `cn`, `count-change`.

## Usage

```tsx
import { Button } from "@/components/ui/button"
import { CountBadge, CountBadgeAnchor } from "@/components/ui/count-badge"

<CountBadgeAnchor>
  <Button size="icon" variant="secondary" aria-label="Orders">
    <ReceiptText />
  </Button>
  <CountBadge count={3} label="3 new orders" />
</CountBadgeAnchor>
```

## Examples

### Preview

```tsx
import { Button } from "@/components/ui/button"
import { CountBadge, CountBadgeAnchor } from "@/components/ui/count-badge"
import { ReceiptText } from "lucide-react"

export default function CountBadgeDemo() {
  return (
    <CountBadgeAnchor>
      <Button size="icon" variant="secondary" aria-label="Orders">
        <ReceiptText strokeWidth={1.75} aria-hidden="true" />
      </Button>
      <CountBadge count={3} label="3 new orders" />
    </CountBadgeAnchor>
  )
}
```

### Dot

```tsx
import { Button } from "@/components/ui/button"
import { CountBadge, CountBadgeAnchor } from "@/components/ui/count-badge"
import { CalendarDays } from "lucide-react"
import { useState } from "react"

export default function CountBadgeDot() {
  const [unseen, setUnseen] = useState(true)

  return (
    <div className="flex items-center justify-center gap-6">
      <CountBadgeAnchor>
        <Button size="icon" variant="secondary" aria-label="Reservations">
          <CalendarDays strokeWidth={1.75} aria-hidden="true" />
        </Button>
        <CountBadge dot open={unseen} label="New reservation" />
      </CountBadgeAnchor>
      <Button variant="secondary" onClick={() => setUnseen((value) => !value)}>
        {unseen ? "Mark as seen" : "New booking"}
      </Button>
    </div>
  )
}
```

### Changing

```tsx
import { Button } from "@/components/ui/button"
import { CountBadge, CountBadgeAnchor } from "@/components/ui/count-badge"
import { ReceiptText } from "lucide-react"
import { useState } from "react"

export default function CountBadgeChanging() {
  const [orders, setOrders] = useState(2)

  return (
    <div className="flex items-center justify-center gap-6">
      <CountBadgeAnchor>
        <Button size="icon" variant="secondary" aria-label="Orders">
          <ReceiptText strokeWidth={1.75} aria-hidden="true" />
        </Button>
        <CountBadge count={orders} label={orders === 1 ? "1 new order" : `${orders} new orders`} />
      </CountBadgeAnchor>
      <div className="flex gap-2">
        <Button variant="secondary" onClick={() => setOrders((count) => count + 1)}>
          New order
        </Button>
        <Button variant="secondary" disabled={orders === 0} onClick={() => setOrders((count) => Math.max(0, count - 1))}>
          Serve one
        </Button>
      </div>
    </div>
  )
}
```

### Overflow

```tsx
import { Button } from "@/components/ui/button"
import { CountBadge, CountBadgeAnchor } from "@/components/ui/count-badge"
import { ReceiptText } from "lucide-react"

export default function CountBadgeOverflow() {
  return (
    <div className="flex items-center justify-center gap-6">
      <CountBadgeAnchor>
        <Button size="icon" variant="secondary" aria-label="Orders">
          <ReceiptText strokeWidth={1.75} aria-hidden="true" />
        </Button>
        <CountBadge count={99} label="99 orders waiting" />
      </CountBadgeAnchor>
      <CountBadgeAnchor>
        <Button size="icon" variant="secondary" aria-label="Orders">
          <ReceiptText strokeWidth={1.75} aria-hidden="true" />
        </Button>
        <CountBadge count={142} label="142 orders waiting" />
      </CountBadgeAnchor>
      <CountBadgeAnchor>
        <Button size="icon" variant="secondary" aria-label="Orders">
          <ReceiptText strokeWidth={1.75} aria-hidden="true" />
        </Button>
        <CountBadge count={142} max={9} label="142 orders waiting" />
      </CountBadgeAnchor>
    </div>
  )
}
```

### Toolbar

```tsx
import { Button } from "@/components/ui/button"
import { CountBadge, CountBadgeAnchor } from "@/components/ui/count-badge"
import { Bell, CalendarDays, ReceiptText, Utensils } from "lucide-react"

export default function CountBadgeToolbar() {
  return (
    <div className="flex items-center justify-center gap-1">
      <CountBadgeAnchor>
        <Button size="icon" variant="plain" aria-label="Orders">
          <ReceiptText strokeWidth={1.75} aria-hidden="true" />
        </Button>
        <CountBadge count={12} label="12 open orders" />
      </CountBadgeAnchor>
      <Button size="icon" variant="plain" aria-label="Menu">
        <Utensils strokeWidth={1.75} aria-hidden="true" />
      </Button>
      <CountBadgeAnchor>
        <Button size="icon" variant="plain" aria-label="Reservations">
          <CalendarDays strokeWidth={1.75} aria-hidden="true" />
        </Button>
        <CountBadge dot label="New reservation" />
      </CountBadgeAnchor>
      <CountBadgeAnchor>
        <Button size="icon" variant="plain" aria-label="Notifications">
          <Bell strokeWidth={1.75} aria-hidden="true" />
        </Button>
        <CountBadge count={0} label="No new notifications" />
      </CountBadgeAnchor>
    </div>
  )
}
```

### In list rows

```tsx
import { Island, ListRow, ListRowText, ListRowTitle, ListRowValue } from "@/components/ui/island"

const counts = [
  { title: "New orders", count: 5 },
  { title: "Waiting tables", count: 2 },
  { title: "Reservations tonight", count: 18 },
]

export default function CountBadgeInListRows() {
  return (
    <div className="flex w-full max-w-md flex-col gap-2">
      <Island variant="list">
        {counts.map((row) => (
          <ListRow key={row.title}>
            <ListRowText>
              <ListRowTitle>{row.title}</ListRowTitle>
            </ListRowText>
            <ListRowValue className="tabular-nums">{row.count}</ListRowValue>
          </ListRow>
        ))}
      </Island>
      <p className="px-4 text-footnote text-ink-2">In a row, a count is a plain number. Badges are for icons.</p>
    </div>
  )
}
```

## Source

```tsx
import { cn } from "@/lib/cn"
import { CountSlot } from "@/lib/count-change"
import { type ComponentProps, useEffect, useState } from "react"

const leavingDuration = 220

export type CountBadgeProps = Omit<ComponentProps<"span">, "className" | "children" | "role" | "aria-label"> & {
  label: string
  count?: number
  max?: number
  dot?: boolean
  open?: boolean
  className?: string
}

export function CountBadge({ label, count, max = 99, dot = false, open = true, className, ...props }: CountBadgeProps) {
  const visible = open && (count === undefined || count > 0)
  const [present, setPresent] = useState(visible)
  const [shownCount, setShownCount] = useState(count)
  const [shownLabel, setShownLabel] = useState(label)

  if (visible && !present) setPresent(true)
  if (visible && count !== shownCount) setShownCount(count)
  if (visible && label !== shownLabel) setShownLabel(label)
  const leaving = present && !visible

  useEffect(() => {
    if (!leaving) return
    const timer = setTimeout(() => setPresent(false), leavingDuration)
    return () => clearTimeout(timer)
  }, [leaving])

  if (!present) return null

  return (
    <span
      data-slot="count-badge"
      role="img"
      data-dot={dot || undefined}
      data-leaving={leaving || undefined}
      aria-label={shownLabel}
      aria-hidden={leaving || undefined}
      className={cn(
        "inline-flex shrink-0 items-center justify-center rounded-full bg-ink text-on-ink",
        "transition-[opacity,scale,filter] duration-spring ease-spring starting:scale-50 starting:opacity-0 motion-safe:starting:blur-[4px]",
        "data-leaving:scale-50 data-leaving:opacity-0 data-leaving:duration-base data-leaving:ease-in motion-safe:data-leaving:blur-[4px]",
        dot ? "size-2" : "h-[18px] min-w-[18px] px-[5px] text-[12px] leading-none font-semibold tabular-nums",
        className,
      )}
      {...props}
    >
      {!dot && shownCount !== undefined && <CountSlot text={shownCount > max ? `${max}+` : String(shownCount)} value={shownCount} />}
    </span>
  )
}

export type CountBadgeAnchorProps = Omit<ComponentProps<"span">, "className"> & {
  className?: string
}

export function CountBadgeAnchor({ className, ...props }: CountBadgeAnchorProps) {
  return (
    <span
      data-slot="count-badge-anchor"
      className={cn(
        "relative inline-flex shrink-0 [&>[data-slot=count-badge]]:pointer-events-none [&>[data-slot=count-badge]]:absolute [&>[data-slot=count-badge]]:top-0 [&>[data-slot=count-badge]]:right-0 [&>[data-slot=count-badge][data-dot]]:top-2.5 [&>[data-slot=count-badge][data-dot]]:right-2.5 [&>[data-slot=count-badge]]:z-10",
        className,
      )}
      {...props}
    />
  )
}
```
