{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "app-data-table",
  "type": "registry:block",
  "title": "App, data table",
  "description": "A data table: sort buttons that order by what a column holds (text, numbers, dates; aria-sort), a first column that stays in view while the rest scrolls with an edge fade, comfortable or compact rows, optional row selection with select all, an announced count and bulk actions, and four states.",
  "categories": [
    "app"
  ],
  "dependencies": [
    "lucide-react@^1.49.0",
    "zod@^4.6.5"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/banner.json",
    "https://ui.klarhimmel.com/r/block-kit.json",
    "https://ui.klarhimmel.com/r/button.json",
    "https://ui.klarhimmel.com/r/checkbox.json",
    "https://ui.klarhimmel.com/r/editing.json",
    "https://ui.klarhimmel.com/r/empty-state.json",
    "https://ui.klarhimmel.com/r/kit-app.json",
    "https://ui.klarhimmel.com/r/skeleton.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/blocks/app-data-table/spec.ts",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-data-table/spec.ts",
      "content": "import { z } from 'zod';\nimport { defineSpec, t, req } from '../_shared/schema';\n\nconst schema = z.object({\n  eyebrow: t(60).optional(),\n  title: t(120),\n  description: t(300).optional(),\n  /** Read by screen readers as the table's name. */\n  caption: t(80),\n  /** `kind` says how a column sorts: text by the locale's alphabet, number by value (\"800 kr\" before \"12 000 kr\"), date by the calendar (\"30 May\" before \"2 Jul\"). */\n  columns: z.array(z.object({ label: req(30), align: z.enum(['left', 'right']), sortable: z.boolean(), kind: z.enum(['text', 'number', 'date']) })).min(2).max(6),\n  /** One row per entry, one cell per column. The first cell is the row's header. */\n  /**\n   * One row per entry, one cell per column; the first cell is the row's header. `keys` are optional machine values for\n   * sorting, one per cell (\"2026-06-12\", \"4800\"): when a row has them they win over the text shown.\n   */\n  rows: z.array(z.object({ cells: z.array(t(60)).min(2).max(6), keys: z.array(t(30)).max(6).optional() })).min(1).max(10),\n  /** The alphabet text columns sort by (\"sv\", \"en\"). */\n  locale: t(10),\n  density: z.enum(['comfortable', 'compact']),\n  /** A checkbox column with select all (mixed when some are picked), a count that is announced, and a bar of bulk actions. */\n  selectable: z.boolean(),\n  select: z.object({\n    /** Read before the row's first cell: \"Select Maja Lindqvist\". */\n    row: t(20),\n    all: t(30),\n    selected: t(20),\n    clear: t(30),\n    barLabel: t(40),\n    actions: z.array(t(20)).max(3),\n  }),\n  /** Shown under the table while some of it is cut off at the edge. */\n  scrollHint: t(40),\n  /** Which column is sorted when the block loads: the column's label, or empty for the order given. */\n  sort: z.object({ active: t(30), direction: z.enum(['ascending', 'descending']) }),\n  /** filled: the rows. empty: nothing yet. loading: placeholder rows, announced once. error: a message and a retry. */\n  state: z.enum(['filled', 'empty', 'loading', 'error']),\n  messages: z.object({\n    loading: t(60),\n    emptyTitle: req(80),\n    emptyBody: t(160),\n    emptyAction: t(30),\n    errorTitle: t(80),\n    errorBody: t(160),\n    retry: t(30),\n  }),\n});\n\nexport const spec = defineSpec({\n  type: 'app-data-table',\n  aliases: ['app/data-table'],\n  name: 'App, data table',\n  category: 'app',\n  role: 'body',\n  description: 'A data table: sort buttons that order by what a column holds (text, numbers, dates; aria-sort), a first column that stays in view while the rest scrolls with an edge fade, comfortable or compact rows, optional row selection with select all, an announced count and bulk actions, and four states.',\n  schema,\n  defaults: {\n    \"eyebrow\": \"Name the section\",\n    \"title\": \"Name the table\",\n    \"description\": \"Say what each row stands for and tell people they can click a column to sort.\",\n    \"caption\": \"Name the table and how to sort it\",\n    \"columns\": [\n      {\n        \"label\": \"Person\",\n        \"align\": \"left\",\n        \"sortable\": true,\n        \"kind\": \"text\"\n      },\n      {\n        \"label\": \"Item\",\n        \"align\": \"left\",\n        \"sortable\": true,\n        \"kind\": \"text\"\n      },\n      {\n        \"label\": \"Date\",\n        \"align\": \"left\",\n        \"sortable\": true,\n        \"kind\": \"date\"\n      },\n      {\n        \"label\": \"Count\",\n        \"align\": \"right\",\n        \"sortable\": true,\n        \"kind\": \"number\"\n      },\n      {\n        \"label\": \"Amount\",\n        \"align\": \"right\",\n        \"sortable\": true,\n        \"kind\": \"number\"\n      }\n    ],\n    \"rows\": [\n      {\n        \"cells\": [\n          \"Person one\",\n          \"Name an item\",\n          \"12 Jun\",\n          \"3\",\n          \"$00\"\n        ],\n        \"keys\": [\n          \"\",\n          \"\",\n          \"2026-06-12\",\n          \"3\",\n          \"4800\"\n        ]\n      },\n      {\n        \"cells\": [\n          \"Person two\",\n          \"Name an item\",\n          \"13 Jun\",\n          \"2\",\n          \"$00\"\n        ],\n        \"keys\": [\n          \"\",\n          \"\",\n          \"2026-06-13\",\n          \"2\",\n          \"2100\"\n        ]\n      },\n      {\n        \"cells\": [\n          \"Person three\",\n          \"Name an item\",\n          \"13 Jun\",\n          \"5\",\n          \"$00\"\n        ],\n        \"keys\": [\n          \"\",\n          \"\",\n          \"2026-06-13\",\n          \"5\",\n          \"5250\"\n        ]\n      },\n      {\n        \"cells\": [\n          \"Person four\",\n          \"Name an item\",\n          \"15 Jun\",\n          \"1\",\n          \"$00\"\n        ],\n        \"keys\": [\n          \"\",\n          \"\",\n          \"2026-06-15\",\n          \"1\",\n          \"1050\"\n        ]\n      },\n      {\n        \"cells\": [\n          \"Person five\",\n          \"Name an item\",\n          \"16 Jun\",\n          \"4\",\n          \"$00\"\n        ],\n        \"keys\": [\n          \"\",\n          \"\",\n          \"2026-06-16\",\n          \"4\",\n          \"6400\"\n        ]\n      }\n    ],\n    \"locale\": \"sv\",\n    \"density\": \"comfortable\",\n    \"selectable\": false,\n    \"select\": {\n      \"row\": \"Select\",\n      \"all\": \"Select all rows\",\n      \"selected\": \"selected\",\n      \"clear\": \"Clear selection\",\n      \"barLabel\": \"Name the actions for the picked rows\",\n      \"actions\": [\n        \"Export\",\n        \"Archive\"\n      ]\n    },\n    \"scrollHint\": \"Say there are more columns\",\n    \"sort\": {\n      \"active\": \"Date\",\n      \"direction\": \"ascending\"\n    },\n    \"state\": \"filled\",\n    \"messages\": {\n      \"loading\": \"Say it is loading\",\n      \"emptyTitle\": \"Say there is nothing to show yet\",\n      \"emptyBody\": \"Say what the first row will be and what makes it appear here.\",\n      \"emptyAction\": \"Add the first\",\n      \"errorTitle\": \"Say the rows did not load\",\n      \"errorBody\": \"Say what to check and then retry.\",\n      \"retry\": \"Try again\"\n    }\n  },\n});\n"
    },
    {
      "path": "src/blocks/app-data-table/index.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-data-table/index.tsx",
      "content": "import { useId, useRef } from 'react';\nimport { ArrowDown, ArrowUp, ChevronsUpDown, Inbox } from 'lucide-react';\nimport { Text, withHeadingLevel } from '../../editing';\nimport { cn } from '../../lib/utils';\nimport { Banner } from '../../ui/banner';\nimport { Button } from '../../ui/button';\nimport { Checkbox } from '../../ui/checkbox';\nimport { EmptyState } from '../../ui/empty-state';\nimport { Skeleton } from '../../ui/skeleton';\nimport { Container, Section, SectionHeader } from '../_shared/kit';\nimport { AppPanel, ScrollRegion, StateSlot, compareCells, useSynced } from '../_shared/app-kit';\nimport { spec } from './spec';\nimport type { BlockProps } from '../types';\nimport type { z } from 'zod';\n\nexport { spec };\ntype Props = z.infer<typeof spec.schema>;\n\nfunction AppDataTable({ props }: BlockProps<Props>) {\n  const id = useId();\n  const { state, messages } = props;\n  const [sort, setSort] = useSynced<{ active: string; direction: 'ascending' | 'descending' }>(props.sort);\n  const col = props.columns.findIndex((c) => c.label === sort.active);\n  const order = props.rows.map((row, i) => ({ row, i }));\n  const kind = props.columns[col]?.kind ?? 'text';\n  if (col >= 0) order.sort((a, b) => compareCells(kind, { text: a.row.cells[col] ?? '', key: a.row.keys?.[col] }, { text: b.row.cells[col] ?? '', key: b.row.keys?.[col] }, props.locale) * (sort.direction === 'ascending' ? 1 : -1));\n  const toggle = (label: string) => setSort((s) => ({ active: label, direction: s.active === label && s.direction === 'ascending' ? 'descending' : 'ascending' }));\n  const [picked, setPicked] = useSynced<number[]>([], props.rows);\n  const selectAll = useRef<HTMLButtonElement>(null);\n  const sel = props.selectable && state === 'filled';\n  const all = picked.length === props.rows.length ? true : picked.length ? 'indeterminate' : false;\n  const pad = props.density === 'compact' ? 'py-1.5' : 'py-3';\n  const right = (j: number) => (props.columns[j]?.align === 'right' ? 'text-right' : 'text-left');\n\n  return (\n    <Section labelledBy={id}>\n      <Container>\n        <SectionHeader id={id} eyebrow={props.eyebrow} title={props.title} description={props.description} />\n        <AppPanel className=\"mt-10 overflow-hidden\">\n          <StateSlot show={sel}>\n            <p role=\"status\" className={cn(picked.length > 0 ? 'border-b border-kh-line px-4 py-2 text-kh-small tabular-nums text-kh-muted' : 'sr-only')}>\n              {picked.length > 0 ? (\n                <>\n                  {picked.length} <Text path={['select', 'selected']} value={props.select.selected} as=\"span\" />\n                </>\n              ) : (\n                <span className=\"sr-only\"><Text path={['select', 'selected']} value={props.select.selected} as=\"span\" /></span>\n              )}\n            </p>\n            <div role=\"toolbar\" aria-labelledby={`${id}-bar`} hidden={picked.length === 0} className=\"flex flex-wrap items-center gap-2 border-b border-kh-line bg-kh-surface-2 px-4 py-2\">\n              <span id={`${id}-bar`} className=\"sr-only\">\n                <Text path={['select', 'barLabel']} value={props.select.barLabel} as=\"span\" />\n              </span>\n              {props.select.actions.map((a, i) => (\n                <Button key={i} variant=\"secondary\">\n                  <Text path={['select', 'actions', i]} value={a} as=\"span\" />\n                </Button>\n              ))}\n              <Button\n                variant=\"ghost\"\n                onClick={() => {\n                  setPicked([]);\n                  selectAll.current?.focus();\n                }}\n              >\n                <Text path={['select', 'clear']} value={props.select.clear} as=\"span\" />\n              </Button>\n            </div>\n          </StateSlot>\n          <StateSlot show={false}>\n            {!sel && <Text path={['select', 'all']} value={props.select.all} as=\"p\" />}\n            {!sel && <Text path={['select', 'row']} value={props.select.row} as=\"p\" />}\n          </StateSlot>\n          <ScrollRegion label={props.caption} hint={<Text path={['scrollHint']} value={props.scrollHint} as=\"span\" />}>\n            <table aria-busy={state === 'loading' || undefined} className=\"w-full min-w-[34rem] border-collapse text-kh-small\">\n              <caption className=\"sr-only\">\n                <Text path={['caption']} value={props.caption} as=\"span\" />\n              </caption>\n              <thead>\n                <tr className=\"border-b border-kh-line bg-kh-surface-2\">\n                  {sel && (\n                    <th scope=\"col\" className=\"sticky left-0 z-10 w-12 bg-kh-surface-2 px-2\">\n                      <Checkbox\n                        ref={selectAll}\n                        checked={all}\n                        onCheckedChange={(c) => setPicked(c === true ? props.rows.map((_, i) => i) : [])}\n                        label={<span className=\"sr-only\"><Text path={['select', 'all']} value={props.select.all} as=\"span\" /></span>}\n                      />\n                    </th>\n                  )}\n                  {props.columns.map((c, j) => {\n                    const active = c.label === sort.active && c.sortable;\n                    const label = <Text path={['columns', j, 'label']} value={c.label} as=\"span\" />;\n                    return (\n                      <th key={j} scope=\"col\" aria-sort={c.sortable ? (active ? sort.direction : 'none') : undefined} className={cn('p-0 font-medium text-kh-ink-2', right(j), j === 0 && !sel && 'sticky left-0 z-10 bg-kh-surface-2')}>\n                        {c.sortable ? (\n                          <button\n                            type=\"button\"\n                            onClick={() => toggle(c.label)}\n                            className={cn('inline-flex min-h-11 w-full items-center gap-1.5 px-4 hover:text-kh-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-(--kh-focus)', c.align === 'right' && 'justify-end')}\n                          >\n                            {label}\n                            {active ? sort.direction === 'ascending' ? <ArrowUp aria-hidden className=\"size-3.5\" /> : <ArrowDown aria-hidden className=\"size-3.5\" /> : <ChevronsUpDown aria-hidden className=\"size-3.5 text-kh-muted\" />}\n                          </button>\n                        ) : (\n                          <span className=\"inline-flex min-h-11 items-center px-4\">{label}</span>\n                        )}\n                      </th>\n                    );\n                  })}\n                </tr>\n              </thead>\n              <tbody className=\"divide-y divide-kh-line\">\n                {state === 'filled' &&\n                  order.map(({ row, i }) => (\n                    <tr key={i} className={cn('hover:bg-kh-surface-2', picked.includes(i) && 'bg-kh-surface-2')}>\n                      {sel && (\n                        <td className=\"sticky left-0 w-12 bg-inherit px-2\">\n                          <Checkbox\n                            checked={picked.includes(i)}\n                            onCheckedChange={(c) => setPicked((p) => (c === true ? [...p, i] : p.filter((x) => x !== i)))}\n                            label={<span className=\"sr-only\"><Text path={['select', 'row']} value={props.select.row} as=\"span\" />{' '}{row.cells[0]}</span>}\n                          />\n                        </td>\n                      )}\n                      {row.cells.map((cell, j) =>\n                        j === 0 ? (\n                          <th key={j} scope=\"row\" className={cn('whitespace-nowrap bg-inherit px-4 font-medium text-kh-ink', !sel && 'sticky left-0 bg-kh-surface', pad, right(j))}>\n                            <Text path={['rows', i, 'cells', j]} value={cell} as=\"span\" />\n                          </th>\n                        ) : (\n                          <td key={j} className={cn('whitespace-nowrap px-4 tabular-nums text-kh-ink-2', pad, right(j))}>\n                            <Text path={['rows', i, 'cells', j]} value={cell} as=\"span\" />\n                          </td>\n                        ),\n                      )}\n                    </tr>\n                  ))}\n                {state === 'loading' &&\n                  Array.from({ length: 5 }, (_, r) => (\n                    <tr key={r} aria-hidden>\n                      {props.columns.map((_c, j) => (\n                        <td key={j} className=\"px-4 py-3.5\">\n                          <Skeleton className={cn('h-4', j === 0 ? 'w-32' : 'w-16', props.columns[j]?.align === 'right' && 'ml-auto')} />\n                        </td>\n                      ))}\n                    </tr>\n                  ))}\n              </tbody>\n            </table>\n          </ScrollRegion>\n          <StateSlot show={state === 'loading'} className=\"border-t border-kh-line px-4 py-3 text-kh-small text-kh-muted\">\n            <p role=\"status\">\n              <Text path={['messages', 'loading']} value={messages.loading} as=\"span\" />\n            </p>\n          </StateSlot>\n          <StateSlot show={state === 'empty'} className=\"border-t border-kh-line\">\n            <EmptyState\n              icon={<Inbox />}\n              title={<Text path={['messages', 'emptyTitle']} value={messages.emptyTitle} as=\"span\" />}\n              level={3}\n              action={\n                <Button variant=\"secondary\">\n                  <Text path={['messages', 'emptyAction']} value={messages.emptyAction} as=\"span\" />\n                </Button>\n              }\n            >\n              <Text path={['messages', 'emptyBody']} value={messages.emptyBody} as=\"span\" />\n            </EmptyState>\n          </StateSlot>\n          <StateSlot show={state === 'error'} className=\"border-t border-kh-line p-4\">\n            <Banner tone=\"error\" title={<Text path={['messages', 'errorTitle']} value={messages.errorTitle} as=\"span\" />} action={<Button variant=\"secondary\" size=\"md\"><Text path={['messages', 'retry']} value={messages.retry} as=\"span\" /></Button>}>\n              <Text path={['messages', 'errorBody']} value={messages.errorBody} as=\"span\" />\n            </Banner>\n          </StateSlot>\n        </AppPanel>\n      </Container>\n    </Section>\n  );\n}\n\nexport default withHeadingLevel(AppDataTable);\n"
    }
  ]
}
