# Count

A small number badge next to a label, a tab or a heading.

Number badge: a count next to a label, tab, chip or heading. Tabular numbers, one line, never shrinks: a huge number widens the badge instead of wrapping. plain is coloured text; boxed sits in a small inset box. Inside a selected chip, tab or segment it takes the selected look by itself.

Kind: component · Category: Display · Status: beta · Targets: React, Vue, Svelte

## Add

```bash
npx fndn add count
```

It writes the folder `count/` into the project's ui folder (`index.ts`, `count.tsx`, `Count.vue`, `Count.svelte`, `count.recipe.ts`). First time: [Installation](/installation.md).

## Usage

```tsx
import { Count } from "@/components/ui/count";
```

```vue
import { Count } from "@/components/ui/count";
```

```svelte
import { Count } from "$lib/components/ui/count";
```

## Parts

In the DOM the root carries `data-slot="count"` and each part `data-slot="count-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `span` | `count` |  | Holds the number as text ("12", "+3", "12,480"). Format it before passing it in. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"plain" \| "boxed"` | `"plain"` | plain: ink-2 text in the surrounding size. boxed: 20px box, radius.chip, inset fill with a hairline, 11px/500 ink-2. |
| `size` | `"sm" \| "md"` | `"md"` | md: plain in the surrounding size, boxed 20px. sm: plain 12px (text.xs), boxed an 18px box. |
| `selected` | `boolean` | `false` | The selected look of a boxed count (field fill, ink); a plain count stays ink-2, the quieter one beside its selected label. Applied automatically inside an element with data-state="checked", aria-selected="true", aria-checked="true" or aria-pressed="true". |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Slots

| Slot | Content |
| --- | --- |
| `default` | The number, formatted |

## States

`selected`

## Keyboard and accessibility

Notes:

- The number is real text and is read with its label. When the label alone does not say what is counted, give the count a visually hidden suffix ("12 unread") or put the meaning in the label.
- Never truncate a number; the badge grows instead.
- A signed value ("+2", "-1") keeps its sign order on a right-to-left page: the badge is a left-to-right island (direction: ltr, unicode-bidi: isolate).

## Motion

- selected: fill and colour cross-fade over duration.base, ease (it follows the chip or tab that is selected)
- no other motion

## Tokens

`color.ink`, `color.ink-2`, `color.inset`, `color.field`, `shadow.hairline`, `radius.chip`, `text.2xs`, `duration.base`, `text.xs`

## Examples

### Pull request filters

React · `pull-request-filters.tsx`

```tsx
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Count } from "@/components/ui/count";

const filters = [
  { value: "open", label: "Open", count: "28" },
  { value: "merged", label: "Merged", count: "1,204" },
  { value: "closed", label: "Closed", count: "96" },
];

/* inside a pressed control the count takes the selected look by itself */
export default function PullRequestFilters() {
  const [filter, setFilter] = useState("open");
  return (
    <div className="flex gap-1">
      {filters.map((f) => (
        <Button key={f.value} size="sm" variant={filter === f.value ? "outline" : "ghost"} aria-pressed={filter === f.value} onClick={() => setFilter(f.value)}>
          {f.label} <Count variant="boxed">{f.count}</Count>
        </Button>
      ))}
    </div>
  );
}
```

Vue · `pull-request-filters.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Button } from "@/components/ui/button";
import { Count } from "@/components/ui/count";

const filters = [
  { value: "open", label: "Open", count: "28" },
  { value: "merged", label: "Merged", count: "1,204" },
  { value: "closed", label: "Closed", count: "96" },
];
const filter = ref("open");
</script>

<!-- inside a pressed control the count takes the selected look by itself -->
<template>
  <div class="flex gap-1">
    <Button v-for="f in filters" :key="f.value" size="sm" :variant="filter === f.value ? 'outline' : 'ghost'" :aria-pressed="filter === f.value" @click="filter = f.value">
      {{ f.label }} <Count variant="boxed">{{ f.count }}</Count>
    </Button>
  </div>
</template>
```

Svelte · `pull-request-filters.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
  import { Count } from "$lib/components/ui/count";

  const filters = [
    { value: "open", label: "Open", count: "28" },
    { value: "merged", label: "Merged", count: "1,204" },
    { value: "closed", label: "Closed", count: "96" },
  ];
  let filter = $state("open");
</script>

<!-- inside a pressed control the count takes the selected look by itself -->
<div class="flex gap-1">
  {#each filters as f (f.value)}
    <Button size="sm" variant={filter === f.value ? "outline" : "ghost"} aria-pressed={filter === f.value} onclick={() => (filter = f.value)}>
      {f.label} <Count variant="boxed">{f.count}</Count>
    </Button>
  {/each}
</div>
```

### Beside a section heading

React · `section-heading.tsx`

```tsx
import { Count } from "@/components/ui/count";

/* plain: the count in the heading's own size, quieter */
export default function SectionHeading() {
  return (
    <div className="flex w-72 flex-col gap-1">
      <h3 className="flex items-baseline gap-2 text-md font-medium text-ink">
        Members <Count>1,284</Count>
      </h3>
      <p className="text-sm text-ink-2">Everyone in Northwind, including 3 pending invites.</p>
    </div>
  );
}
```

Vue · `section-heading.vue`

```vue
<script setup lang="ts">
import { Count } from "@/components/ui/count";
</script>

<!-- plain: the count in the heading's own size, quieter -->
<template>
  <div class="flex w-72 flex-col gap-1">
    <h3 class="flex items-baseline gap-2 text-md font-medium text-ink">
      Members <Count>1,284</Count>
    </h3>
    <p class="text-sm text-ink-2">Everyone in Northwind, including 3 pending invites.</p>
  </div>
</template>
```

Svelte · `section-heading.svelte`

```svelte
<script lang="ts">
  import { Count } from "$lib/components/ui/count";
</script>

<!-- plain: the count in the heading's own size, quieter -->
<div class="flex w-72 flex-col gap-1">
  <h3 class="flex items-baseline gap-2 text-md font-medium text-ink">
    Members <Count>1,284</Count>
  </h3>
  <p class="text-sm text-ink-2">Everyone in Northwind, including 3 pending invites.</p>
</div>
```

### More reviewers than fit

React · `more-reviewers.tsx`

```tsx
import { Avatar } from "@/components/ui/avatar";
import { Count } from "@/components/ui/count";

const reviewers = [
  { name: "Maya Chen", color: "var(--fndn-tag-orange)" },
  { name: "Daniel Okafor", color: "var(--fndn-tag-blue)" },
];

/* the reviewers who do not fit are a count */
export default function MoreReviewers() {
  return (
    <div className="flex items-center gap-2 text-sm text-ink-2">
      Reviewers
      {reviewers.map((r) => (
        <Avatar key={r.name} name={r.name} color={r.color} size="20" />
      ))}
      <Count variant="boxed" size="sm" aria-label="3 more reviewers">+3</Count>
    </div>
  );
}
```

Vue · `more-reviewers.vue`

```vue
<script setup lang="ts">
import { Avatar } from "@/components/ui/avatar";
import { Count } from "@/components/ui/count";

const reviewers = [
  { name: "Maya Chen", color: "var(--fndn-tag-orange)" },
  { name: "Daniel Okafor", color: "var(--fndn-tag-blue)" },
];
</script>

<!-- the reviewers who do not fit are a count -->
<template>
  <div class="flex items-center gap-2 text-sm text-ink-2">
    Reviewers
    <Avatar v-for="r in reviewers" :key="r.name" :name="r.name" :color="r.color" size="20" />
    <Count variant="boxed" size="sm" aria-label="3 more reviewers">+3</Count>
  </div>
</template>
```

Svelte · `more-reviewers.svelte`

```svelte
<script lang="ts">
  import { Avatar } from "$lib/components/ui/avatar";
  import { Count } from "$lib/components/ui/count";

  const reviewers = [
    { name: "Maya Chen", color: "var(--fndn-tag-orange)" },
    { name: "Daniel Okafor", color: "var(--fndn-tag-blue)" },
  ];
</script>

<!-- the reviewers who do not fit are a count -->
<div class="flex items-center gap-2 text-sm text-ink-2">
  Reviewers
  {#each reviewers as r (r.name)}
    <Avatar name={r.name} color={r.color} size="20" />
  {/each}
  <Count variant="boxed" size="sm" aria-label="3 more reviewers">+3</Count>
</div>
```

## Recipe

`count.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* count — number badge. Tabular numbers, one line, never shrinks: a huge number widens the badge.
 * plain is coloured text in the surrounding size; boxed is a 20px inset box; size="sm" makes plain
 * 12px text and boxed an 18px box. A signed value ("+2") reads left to right on a right-to-left
 * page too: the badge is its own left-to-right island.
 *
 * Selected: set by the prop (the `selected` variant), or taken by itself inside a selected chip, tab or
 * segment (data-state="checked", aria-selected / -checked / -pressed), so the host never has to
 * mirror its state. A boxed count takes the field fill and ink. A plain count does not change: it
 * stays ink-2, so beside a selected label (ink) it is the quieter of the two. The colour, fill
 * and ring change at the base pace (motion-colors-base, motion-shadow-base). One element, so the
 * recipe is `base` only. */

export const count = tv({
  /* a width from the host (a 24px slot) never cuts the number: the badge grows past it */
  base: "inline-flex shrink-0 min-w-max items-center justify-center leading-none tabular-nums whitespace-nowrap [unicode-bidi:isolate] [direction:ltr] motion-colors-base motion-shadow-base",
  variants: {
    variant: {
      plain: "text-ink-2",
      /* the square around one digit: min-width replaces max-content, a boxed count takes no width
       * from its host */
      boxed: [
        "min-w-5 h-5 px-1.5 rounded-chip bg-inset shadow-hairline text-2xs font-medium text-ink-2",
        "[:is([data-state=checked],[aria-selected=true],[aria-checked=true],[aria-pressed=true])_&]:bg-field",
        "[:is([data-state=checked],[aria-selected=true],[aria-checked=true],[aria-pressed=true])_&]:shadow-none",
        "[:is([data-state=checked],[aria-selected=true],[aria-checked=true],[aria-pressed=true])_&]:text-ink",
      ],
    },
    size: {
      md: "",
      sm: "text-xs",
    },
    selected: { true: "" },
  },
  compoundVariants: [
    { variant: "boxed", size: "sm", class: "min-w-[18px] h-[18px] px-[5px] text-2xs" },
    { variant: "boxed", selected: true, class: "bg-field shadow-none text-ink" },
  ],
  defaultVariants: { variant: "plain", size: "md", selected: false },
});

export type CountRecipeProps = VariantProps<typeof count>;
```
