# Group Header

> The line over a group of rows with its count, sticky while they scroll, folding the group.

Page: https://uicontext.com/docs/components/group-header

Group: Records.

## When to use

The line over a group of rows with its count, sticky while they scroll, folding the group. Use [Accordion](https://uicontext.com/docs/components/accordion.md) instead when the sections are content, not groups of rows.

## Usage

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

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

export const DoneHeader = (h: Html.H<typeof Message.Type>, open: boolean) =>
	GroupHeader(h, { title: "Done", count: 12, sticky: true, open, onToggle: Message.ToggledDone(), controls: "done-rows" })
```

## Examples

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

### Sticky groups that fold

The example the page opens with: Group Header in “Sticky groups that fold”.

```ts title="src/demo.ts" source="www/src/examples/group-header/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { GroupHeader } from "./ui/uicontext/components/GroupHeader.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, density, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	box: { inlineSize: "min(100%, 30rem)", blockSize: 320, overflowY: "auto", overscrollBehavior: "contain", borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.app },
	rows: { margin: 0, padding: 0, listStyle: "none" },
	row: { display: "flex", alignItems: "baseline", gap: space.md, paddingBlock: space.sm, paddingInline: space.md, fontSize: font.sm, borderBlockEndWidth: stroke.hairline, borderBlockEndStyle: "solid", borderBlockEndColor: color.line },
	title: { flexGrow: 1, minInlineSize: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" },
	due: { flexShrink: 0, fontSize: font.xs, color: color.muted, fontVariantNumeric: "tabular-nums" },
	done: { color: color.muted, textDecorationLine: "line-through" },
	add: { paddingInline: space.sm, minBlockSize: density.control, minInlineSize: { default: 0, "@media (pointer: coarse)": density.control }, borderWidth: 0, borderRadius: corner.tag, backgroundColor: "transparent", color: color.muted, fontFamily: "inherit", fontSize: font.xs, cursor: "pointer" },
})

const groups = [
	{ id: "overdue", title: "Overdue", tone: "danger" as const, tasks: [["Send the Q4 renewal quote to Northwind", "28 Nov"], ["Review the security questionnaire", "1 Dec"], ["Call back Harborview Supply about seats", "2 Dec"]] },
	{ id: "today", title: "Today", tasks: [["Prep the pricing review deck", "4 Dec"], ["Reply to Priya on the SSO question", "4 Dec"], ["Update the Billing migration status", "4 Dec"], ["Book the offsite venue", "4 Dec"]] },
	{ id: "week", title: "This week", tasks: [["Interview: senior designer", "5 Dec"], ["1:1 with Tomás", "6 Dec"], ["Close out November expenses", "7 Dec"]] },
	{ id: "done", title: "Done", tasks: [["Ship the onboarding checklist", "3 Dec"], ["Merge the invoice PDF fix", "3 Dec"], ["Archive the old help center", "2 Dec"], ["Sign the Halvorsen & Co order form", "1 Dec"], ["Draft the hiring plan", "30 Nov"]] },
] as const

const Message = UiSchema.TaggedEnum({ Toggled: { id: Schema.String } })

// a task list: each group's header sticks while its rows scroll under it, and folds the group
export const TaskGroups = Html.component({
	name: "TaskGroups",
	model: Schema.Struct({ closed: Schema.Array(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ closed: ["done"] }),
	update: { Toggled: ($, { id }) => $.evolve({ closed: (list) => (list.includes(id) ? list.filter((x) => x !== id) : [...list, id]) }) },
	view: (h, model) =>
		h.div.attrs(sx(styles.box)).children(
			groups.map((g) => {
				const open = !model.closed.includes(g.id)
				return h.section.key(g.id).children(
					GroupHeader(h, {
						title: g.title,
						count: g.tasks.length,
						...("tone" in g ? { tone: g.tone } : {}),
						sticky: true,
						open,
						onToggle: Message.Toggled({ id: g.id }),
						controls: `tasks-${g.id}`,
						end: h.button.attrs({ type: "button", "aria-label": `Add a task to ${g.title}`, ...sx(styles.add) }).children("Add"),
					}),
					h.ul.attrs({ id: `tasks-${g.id}`, hidden: open ? undefined : "", ...sx(styles.rows) }).children(
						g.tasks.map(([title, due]) =>
							h.li.key(title).attrs(sx(styles.row)).children(
								h.span.attrs({ dir: "auto", ...sx(styles.title, g.id === "done" && styles.done) }).children(title),
								h.span.attrs(sx(styles.due)).children(due),
							)),
					),
				)
			}),
		),
})
```

### Plain headings

Without `onToggle` it's a heading and a count; `level` places it among the page's headings.

```ts title="src/plain.ts" source="www/src/examples/group-header/plain.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { GroupHeader } from "./ui/uicontext/components/GroupHeader.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	wrap: { inlineSize: "min(100%, 26rem)" },
	rows: { margin: 0, marginBlockEnd: space.md, padding: 0, listStyle: "none" },
	row: { paddingBlock: space.sm, paddingInline: space.sm, fontSize: font.sm, borderBlockEndWidth: stroke.hairline, borderBlockEndStyle: "solid", borderBlockEndColor: color.line },
})

const lanes = [
	{ title: "Needs your review", tone: "accent" as const, rows: ["Dark mode for settings", "Retry failed webhooks"] },
	{ title: "Waiting on others", rows: ["Rate-limit the export API", "New invoice template", "Search by customer email"] },
]

// pull requests grouped by what they wait on: headings without a toggle, one in the accent
export const ReviewLanes = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		lanes.map((lane) =>
			h.section.key(lane.title).children(
				GroupHeader(h, { title: lane.title, count: lane.rows.length, ...(lane.tone === undefined ? {} : { tone: lane.tone }), level: 2 }),
				h.ul.attrs(sx(styles.rows)).children(lane.rows.map((r) => h.li.key(r).attrs(sx(styles.row)).children(r))),
			)),
	)
```

## API reference

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `title` (required) | `string` | — | — |
| `count` | `number` | — | How many rows the group holds, shown after the title. |
| `tone` | `"danger" \| "accent"` | — | Colors the title: `danger` for overdue, `accent` for what needs you. |
| `sticky` | `boolean` | — | Sticks to the top of its scrolling list while its rows pass under it. |
| `level` | `2 \| 3 \| 4` | `3` | The heading level, so the groups show in a screen reader's list of headings. 3 by default. |
| `open` | `boolean` | — | Whether the group is open; with `onToggle`, the header folds it. |
| `onToggle` | `Message` | — | — |
| `controls` | `string` | — | The id of the element holding the group's rows, which the toggle opens and closes. |
| `end` | `View.Content` | — | Things at the end: an "Add" button, a menu. |
| `locale` | `string` | — | — |
| `style` | `Style` | — | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/group-header
```

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

It writes these files:

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

## Related

- [Data Grid](https://uicontext.com/docs/components/data-grid.md) (`data-grid`): A spreadsheet-like grid for editing records, with typed columns, sorting and grouping.
- [Pivot Table](https://uicontext.com/docs/components/pivot-table.md) (`pivot-table`): Records summed two ways, such as revenue by region and quarter, with totals on both edges and cells tinted by size.
- [Table](https://uicontext.com/docs/components/table.md) (`table`): A simple read-only table with a caption, row headers and an empty state.
- [Description List](https://uicontext.com/docs/components/description-list.md) (`description-list`): A record's facts as label and value pairs, in rows or columns, with Copy for ids.
- [Fact Grid](https://uicontext.com/docs/components/fact-grid.md) (`fact-grid`): A few numbers about one thing, such as a customer's plan and seats, as small tiles.
- [Order Summary](https://uicontext.com/docs/components/order-summary.md) (`order-summary`): What an order comes to: its lines, then subtotal, discount, shipping, tax and the total.
