Ranked rows, such as top pages, each with a bar as long as its share of the largest.
- 1.6 kB JS 3.0 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 10 tests
Top pages
Visitors by country
- Germany9,12038%
- United States8,80636%
- Brazil3,41014%
- Japan2,2709.4%
- Egypt6402.6%
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { BarList } from "./ui/uicontext/components/BarList.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ grid: { display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(min(100%, 16rem), 1fr))", gap: space.lg, inlineSize: "100%" }, card: { display: "grid", alignContent: "start", gap: space.sm, padding: space.md, borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel, minInlineSize: 0 }, title: { margin: 0, paddingInline: space.sm, fontSize: font.sm, fontWeight: 600 },})const pages = [ { id: "home", label: "/", value: 18_420, href: "#home" }, { id: "pricing", label: "/pricing", value: 6_210, href: "#pricing" }, { id: "docs", label: "/docs/getting-started", value: 4_985, href: "#docs" }, { id: "changelog", label: "/changelog", value: 1_302, href: "#changelog" }, { id: "careers", label: "/careers/senior-product-designer-remote-europe", value: 418, href: "#careers" },]const countries = [ { id: "de", label: "Germany", value: 9_120 }, { id: "us", label: "United States", value: 8_806 }, { id: "br", label: "Brazil", value: 3_410 }, { id: "jp", label: "Japan", value: 2_270 }, { id: "eg", label: "Egypt", value: 640 },]// a site's analytics: pages link to their own report; countries show their shareexport const TopPages = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.grid)).children( h.section.attrs(sx(styles.card)).children( h.h3.attrs(sx(styles.title)).children("Top pages"), BarList(h, { label: "Top pages", headings: ["Page", "Views"], items: pages }), ), h.section.attrs(sx(styles.card)).children( h.h3.attrs(sx(styles.title)).children("Visitors by country"), BarList(h, { label: "Visitors by country", headings: ["Country", "Visitors"], items: countries, share: true }), ), )When to use
Ranked rows, such as top pages, each with a bar as long as its share of the largest. Use Bar Chart instead when the values need an axis to read.
Usage
import { BarList } from "./ui/uicontext/components/BarList.ts"export const Pages = <M>(h: Html.H<M>) => BarList(h, { label: "Top pages", items: [{ id: "/", label: "/", value: 18420 }, { id: "/pricing", label: "/pricing", value: 6210 }] })Examples
Money
format writes each value; a value at or below zero draws no bar.
- Enterprise$412.0K
- Business$286.4K
- Team$98.3K
- Starter$12.9K
- Legacy (refunds)-$1.8K
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { BarList } from "./ui/uicontext/components/BarList.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ wrap: { inlineSize: "min(100%, 26rem)" } })const usd = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", notation: "compact", maximumFractionDigits: 1 })// revenue by plan in a billing dashboard, written as money; refunds can push a row below zeroexport const RevenueByPlan = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.wrap)).children( BarList(h, { label: "Revenue by plan, November", headings: ["Plan", "Revenue"], format: (n) => usd.format(n), items: [ { id: "enterprise", label: "Enterprise", value: 412_000 }, { id: "business", label: "Business", value: 286_400 }, { id: "team", label: "Team", value: 98_250 }, { id: "starter", label: "Starter", value: 12_900 }, { id: "legacy", label: "Legacy (refunds)", value: -1_800 }, ], }), )API reference
Options
| Name | Type | Description |
|---|---|---|
labelrequired | string | What's ranked: "Top pages". |
itemsrequired | BarListItem<Message>[] | The rows, in any order: the largest goes first. |
format | (value: number) => string | Writes a value, such as "$1.2K" or "38%". Plain numbers in the reader's format otherwise. |
share | boolean | Shows each row's share of the total after its value. |
headings | readonly [string, string] | Column headings over the rows: ["Page", "Views"]. |
sorted | boolean | Keeps the given order instead of putting the largest first. |
limit | number | Draws only this many rows. |
locale | string | — |
style | Style | — |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/bar-list