How many are waiting, as a number or a dot, beside a nav item or on an icon.
- 1.4 kB JS 2.7 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 9 tests
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 0export 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 }), ), )), ), )When to use
How many are waiting, as a number or a dot, beside a nav item or on an icon. Use Status instead when the mark is a state, not a number.
Usage
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
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.
More than 99 unread notificationsAONew activity
Press the bell to read them.
This example also uses Avatar. Install it too:
npx shadcn@latest add checkfu/uicontext/avatarimport { 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 countexport 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
CountBadgeView 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 | — | — |
CountOverlayView 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
pnpm dlx shadcn@latest add checkfu/uicontext/count-badge