# Bar List

> Ranked rows, such as top pages, each with a bar as long as its share of the largest.

Page: https://uicontext.com/docs/components/bar-list

Group: Charts.

## When to use

Ranked rows, such as top pages, each with a bar as long as its share of the largest. Use [Bar Chart](https://uicontext.com/docs/components/bar-chart.md) instead when the values need an axis to read.

## Usage

```ts
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

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

### Top pages and countries

The example the page opens with: Bar List in “Top pages and countries”.

```ts title="src/demo.ts" source="www/src/examples/bar-list/demo.ts"
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 share
export 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 }),
		),
	)
```

### Money

`format` writes each value; a value at or below zero draws no bar.

```ts title="src/revenue.ts" source="www/src/examples/bar-list/revenue.ts"
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 zero
export 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 |
| --- | --- | --- |
| `label` (required) | `string` | What's ranked: "Top pages". |
| `items` (required) | `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

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/bar-list
```

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

It writes these files:

- `src/ui/uicontext/components/BarList.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/BarList.ts](https://github.com/checkfu/uicontext/blob/main/src/BarList.ts)

## Related

- [Chart Foundations](https://uicontext.com/docs/components/chart-foundations.md) (`chart-foundations`): The scales, formats, legend and tooltip every chart shares.
- [Line Chart](https://uicontext.com/docs/components/line-chart.md) (`line-chart`): Values over time for one or many series, with dashed comparisons and forecasts.
- [Area Chart](https://uicontext.com/docs/components/area-chart.md) (`area-chart`): Totals over time and what makes them up, stacked or as shares of 100%.
- [Bar Chart](https://uicontext.com/docs/components/bar-chart.md) (`bar-chart`): Compare categories with grouped, stacked or sideways bars.
- [Combo Chart](https://uicontext.com/docs/components/combo-chart.md) (`combo-chart`): Bars and a line on one time axis, each read against its own scale.
- [Donut Chart](https://uicontext.com/docs/components/donut-chart.md) (`donut-chart`): Shares of one total, with the total in the middle.
