# Count Badge

> How many are waiting, as a number or a dot, beside a nav item or on an icon.

Page: https://uicontext.com/docs/components/count-badge

Group: Primitives.

## When to use

How many are waiting, as a number or a dot, beside a nav item or on an icon. Use [Status](https://uicontext.com/docs/components/status.md) instead when the mark is a state, not a number.

## Usage

```ts
import { CountBadge } from "./ui/uicontext/components/CountBadge.ts"

export const InboxLink = <M>(h: Html.H<M>, unread: number) =>
	h.a.attrs({ href: "/inbox" }).children("Inbox ", CountBadge(h, { count: unread, noun: "unread message" }))
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### A sidebar's counts

The example the page opens with: Count Badge in “A sidebar's counts”.

```ts title="src/demo.ts" source="www/src/examples/count-badge/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { CountBadge } from "./ui/uicontext/components/CountBadge.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, density, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	nav: { inlineSize: "min(100%, 16rem)" },
	list: { display: "grid", gap: 2, margin: 0, padding: 0, listStyle: "none" },
	link: {
		display: "flex",
		alignItems: "center",
		gap: space.sm,
		minBlockSize: density.control,
		paddingInline: space.sm,
		borderRadius: corner.control,
		color: color.fg,
		fontSize: font.sm,
		textDecoration: "none",
		backgroundColor: { default: "transparent", ":hover": { default: null, "@media (hover: hover) and (pointer: fine)": color.hover } },
		outline: { default: "none", ":focus-visible": `2px solid ${color.focusRing}` },
	},
	current: { backgroundColor: color.selected, fontWeight: 500 },
	name: { flexGrow: 1 },
})

const items = [
	{ id: "inbox", name: "Inbox", count: 3, noun: "unread conversation", tone: "accent" as const },
	{ id: "mentions", name: "Mentions", count: 128, noun: "mention", tone: "accent" as const },
	{ id: "assigned", name: "Assigned to me", count: 12, noun: "open ticket", tone: "quiet" as const },
	{ id: "breached", name: "SLA breached", count: 2, noun: "ticket", tone: "danger" as const },
	{ id: "drafts", name: "Drafts", count: 0, noun: "draft", tone: "quiet" as const },
]

// a help desk's sidebar: unread in the accent, a plain tally quiet, breaches in red, nothing at 0
export const HelpdeskNav = <M>(h: Html.H<M>) =>
	h.nav.attrs({ "aria-label": "Views", ...sx(styles.nav) }).children(
		h.ul.attrs(sx(styles.list)).children(
			items.map((item, i) =>
				h.li.key(item.id).children(
					h.a.attrs({ href: `#${item.id}`, ...(i === 0 ? { "aria-current": "page" } : {}), ...sx(styles.link, i === 0 && styles.current) }).children(
						h.span.attrs(sx(styles.name)).children(item.name),
						CountBadge(h, { count: item.count, noun: item.noun, tone: item.tone }),
					),
				)),
		),
	)
```

### Over an icon

`CountOverlay` pins it to the corner. A button's `aria-label` hides what's inside it, so put `countWords` in the label too.

This example also uses Avatar. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/avatar
```

```ts title="src/overlay.ts" source="www/src/examples/count-badge/overlay.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Avatar } from "./ui/uicontext/components/Avatar.ts"
import { CountOverlay, countWords } from "./ui/uicontext/components/CountBadge.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, density, font, motion, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	row: { display: "flex", alignItems: "center", gap: space.xl },
	icon: {
		display: "inline-flex",
		alignItems: "center",
		justifyContent: "center",
		inlineSize: density.control,
		blockSize: density.control,
		padding: 0,
		borderWidth: 0,
		borderRadius: corner.control,
		backgroundColor: { default: "transparent", ":hover": { default: null, "@media (hover: hover) and (pointer: fine)": color.hover } },
		color: color.fg,
		cursor: "pointer",
		outline: { default: "none", ":focus-visible": `2px solid ${color.focusRing}` },
		transform: { default: "none", ":active": `scale(${motion.press})` },
		transitionProperty: "background-color, transform",
		transitionDuration: motion.fast,
	},
	said: { margin: 0, fontSize: font.sm, color: color.muted },
	wrap: { display: "grid", gap: space.md },
})

const Bell = <M>(h: Html.H<M>) =>
	h.svg.attrs({ width: "18", height: "18", viewBox: "0 0 18 18", fill: "none", "aria-hidden": "true" }).children(
		h.path.attrs({ d: "M4.5 12.5V8a4.5 4.5 0 019 0v4.5l1.2 1.5H3.3l1.2-1.5zM7.3 15.5a1.8 1.8 0 003.4 0", stroke: "currentColor", "stroke-width": "1.5", "stroke-linejoin": "round" }),
	)

const Message = UiSchema.TaggedEnum({ Read: {} })

// a bell with a count and an avatar with a dot; the button's own label carries the count
export const TopBar = Html.component({
	name: "TopBarCounts",
	model: Schema.Struct({ unread: Schema.Number }),
	message: Message,
}).make({
	init: () => ({ unread: 142 }),
	update: { Read: ($) => $.evolve({ unread: 0 }) },
	view: (h, model) => {
		const count = { count: model.unread, noun: "unread notification" }
		return h.div.attrs(sx(styles.wrap)).children(
			h.div.attrs(sx(styles.row)).children(
				CountOverlay(h, { ...count, tone: "danger" }, h.button.attrs({ type: "button", "aria-label": model.unread === 0 ? "Notifications" : `Notifications, ${countWords(count)}`, ...sx(styles.icon) }).on("click", Message.Read()).children(Bell(h))),
				CountOverlay(h, { dot: true, label: "New activity" }, Avatar(h, { id: "ada", name: "Ada Okafor", size: "md" })),
			),
			h.p.attrs({ role: "status", ...sx(styles.said) }).children(model.unread === 0 ? "All caught up." : "Press the bell to read them."),
		)
	},
})
```

## API reference

### CountBadge (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `count` | `number` | — | How many. Nothing is drawn at 0, unless `showZero`. |
| `noun` | `string` | — | What it counts, singular: "unread message". The plural adds an s; give `nouns` otherwise. |
| `nouns` | `string` | — | The plural when it isn't the noun and an s: "replies". |
| `max` | `number` | `99` | Above this it reads "99+". 99 by default. |
| `dot` | `boolean` | — | A dot instead of a number. |
| `label` | `string` | — | What the dot means, for a screen reader: "New activity". |
| `tone` | `"accent" \| "danger" \| "quiet"` | — | `accent` (the default) for what needs you, `danger` for what's wrong, `quiet` for a plain tally. |
| `showZero` | `boolean` | — | — |
| `locale` | `string` | — | — |
| `style` | `Style` | — | — |

### CountOverlay (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `count` | `number` | — | How many. Nothing is drawn at 0, unless `showZero`. |
| `noun` | `string` | — | What it counts, singular: "unread message". The plural adds an s; give `nouns` otherwise. |
| `nouns` | `string` | — | The plural when it isn't the noun and an s: "replies". |
| `max` | `number` | `99` | Above this it reads "99+". 99 by default. |
| `dot` | `boolean` | — | A dot instead of a number. |
| `label` | `string` | — | What the dot means, for a screen reader: "New activity". |
| `tone` | `"accent" \| "danger" \| "quiet"` | — | `accent` (the default) for what needs you, `danger` for what's wrong, `quiet` for a plain tally. |
| `showZero` | `boolean` | — | — |
| `locale` | `string` | — | — |
| `style` | `Style` | — | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/count-badge
```

With npm: `npx shadcn@latest add checkfu/uicontext/count-badge`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/count-badge`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/count-badge`.

It writes these files:

- `src/ui/uicontext/components/CountBadge.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/CountBadge.ts](https://github.com/checkfu/uicontext/blob/main/src/CountBadge.ts)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
