{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "app-dashboard-chart",
  "type": "registry:block",
  "title": "App, chart card",
  "description": "A chart card for a dashboard: a bar or line chart drawn as plain SVG from a short list of readings, a sentence that says what it shows, and a value axis, negative values below a zero line, and a data table behind a disclosure so nobody depends on the picture. Filled, empty, loading and error 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/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"
  ],
  "files": [
    {
      "path": "src/blocks/app-dashboard-chart/spec.ts",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-dashboard-chart/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  kind: z.enum(['bar', 'line']),\n  /** One reading per point. Values are numbers written as text (\"4200\", \"-5 000\", \"1,840\"); a reading with no number in it is drawn as a gap, and the table still shows what was written. */\n  points: z.array(z.object({ label: t(12), value: t(12) })).min(3).max(12),\n  /** The unit shown after a value in the table: \"kr\", \"%\". Optional. */\n  unit: t(10).optional(),\n  /** What the chart shows, in a sentence. Shown under it and read out as its description. */\n  summary: t(200),\n  tableLabel: t(40),\n  /** Shown under the data table while some of it is cut off at the edge. */\n  scrollHint: t(40),\n  tableHead: z.object({ label: t(30), value: t(30) }),\n  state: z.enum(['filled', 'empty', 'loading', 'error']),\n  messages: z.object({ loading: t(60), empty: req(120), error: t(120), retry: t(30) }),\n});\n\nexport const spec = defineSpec({\n  type: 'app-dashboard-chart',\n  aliases: ['app/dashboard-chart'],\n  name: 'App, chart card',\n  category: 'app',\n  role: 'body',\n  description: 'A chart card for a dashboard: a bar or line chart drawn as plain SVG from a short list of readings, a sentence that says what it shows, and a value axis, negative values below a zero line, and a data table behind a disclosure so nobody depends on the picture. Filled, empty, loading and error states.',\n  schema,\n  defaults: {\n    \"eyebrow\": \"Name the topic\",\n    \"title\": \"Name what the chart measures\",\n    \"description\": \"Say what is measured, for whom and over what period.\",\n    \"kind\": \"bar\",\n    \"points\": [\n      {\n        \"label\": \"P1\",\n        \"value\": \"31200\"\n      },\n      {\n        \"label\": \"P2\",\n        \"value\": \"34800\"\n      },\n      {\n        \"label\": \"P3\",\n        \"value\": \"29900\"\n      },\n      {\n        \"label\": \"P4\",\n        \"value\": \"38500\"\n      },\n      {\n        \"label\": \"P5\",\n        \"value\": \"41200\"\n      },\n      {\n        \"label\": \"P6\",\n        \"value\": \"44800\"\n      },\n      {\n        \"label\": \"P7\",\n        \"value\": \"47100\"\n      },\n      {\n        \"label\": \"P8\",\n        \"value\": \"43600\"\n      }\n    ],\n    \"unit\": \"kr\",\n    \"summary\": \"Say in one sentence how the value moved from the first point to the peak and where it ended.\",\n    \"tableLabel\": \"See the data as a table\",\n    \"scrollHint\": \"Say there is more to the side\",\n    \"tableHead\": {\n      \"label\": \"Period\",\n      \"value\": \"Measure\"\n    },\n    \"state\": \"filled\",\n    \"messages\": {\n      \"loading\": \"Say it is loading\",\n      \"empty\": \"Say there is nothing to show for these periods yet.\",\n      \"error\": \"Say the chart did not load and to retry.\",\n      \"retry\": \"Try again\"\n    }\n  },\n});\n"
    },
    {
      "path": "src/blocks/app-dashboard-chart/index.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-dashboard-chart/index.tsx",
      "content": "import { useId } from 'react';\nimport { ChartColumn } from 'lucide-react';\nimport { Text, withHeadingLevel } from '../../editing';\nimport { Banner } from '../../ui/banner';\nimport { Button } from '../../ui/button';\nimport { EmptyState } from '../../ui/empty-state';\nimport { Skeleton } from '../../ui/skeleton';\nimport { Container, Opt, Section, SectionHeader } from '../_shared/kit';\nimport { AppPanel, ScrollRegion, StateSlot, groupThousands, parseAmount } 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\nconst W = 600;\nconst H = 220;\n\nfunction Chart({ kind, points }: { kind: Props['kind']; points: Props['points'] }) {\n  const values = points.map((p) => parseAmount(p.value)); // null: no number in the text, drawn as a gap\n  const real = values.filter((v): v is number => v !== null);\n  const hi = Math.max(0, ...real, 0);\n  const lo = Math.min(0, ...real);\n  const span = hi - lo || 1;\n  const y = (v: number) => ((hi - v) / span) * H;\n  const step = W / points.length;\n  const ticks = [0, 1, 2, 3, 4].map((i) => hi - (span * i) / 4);\n  const marks = values.flatMap((v, i) => (v === null ? [] : [{ v, i }]));\n  return (\n    <div className=\"grid grid-cols-[auto_1fr] gap-x-3\">\n      <ul aria-hidden className=\"relative -top-1.5 flex h-[calc(14rem+0.75rem)] flex-col justify-between text-right text-kh-caption leading-none tabular-nums text-kh-muted\">\n        {ticks.map((t, i) => (\n          <li key={i}>{groupThousands(t)}</li>\n        ))}\n      </ul>\n      <svg viewBox={`0 0 ${W} ${H}`} preserveAspectRatio=\"none\" aria-hidden focusable=\"false\" className=\"h-56 w-full overflow-visible text-kh-accent\">\n        {ticks.map((t, i) => (\n          <line key={i} x1={0} x2={W} y1={y(t)} y2={y(t)} stroke=\"currentColor\" strokeWidth={1} vectorEffect=\"non-scaling-stroke\" className=\"text-kh-line\" />\n        ))}\n        <line x1={0} x2={W} y1={y(0)} y2={y(0)} stroke=\"currentColor\" strokeWidth={1.5} vectorEffect=\"non-scaling-stroke\" className=\"text-kh-line-strong\" />\n        {kind === 'bar' ? (\n          marks.map(({ v, i }) => <rect key={i} x={i * step + step * 0.18} width={step * 0.64} y={Math.min(y(v), y(0))} height={Math.abs(y(v) - y(0))} rx={3} fill=\"currentColor\" />)\n        ) : (\n          <>\n            <polyline fill=\"none\" stroke=\"currentColor\" strokeWidth={2.5} strokeLinejoin=\"round\" strokeLinecap=\"round\" vectorEffect=\"non-scaling-stroke\" points={marks.map(({ v, i }) => `${i * step + step / 2},${y(v)}`).join(' ')} />\n            {marks.map(({ v, i }) => (\n              <path key={i} d={`M${i * step + step / 2} ${y(v)}h0.01`} stroke=\"currentColor\" strokeWidth={9} strokeLinecap=\"round\" vectorEffect=\"non-scaling-stroke\" />\n            ))}\n          </>\n        )}\n      </svg>\n      <span />\n      <ul aria-hidden className=\"mt-3 flex justify-between text-kh-caption tabular-nums text-kh-muted\">\n        {points.map((p, i) => (\n          <li key={i} className=\"min-w-0 flex-1 truncate text-center\">\n            <Text path={['points', i, 'label']} value={p.label} as=\"span\" />\n          </li>\n        ))}\n      </ul>\n    </div>\n  );\n}\n\nfunction AppDashboardChart({ props }: BlockProps<Props>) {\n  const id = useId();\n  const { state, messages } = props;\n  const cap = `${id}-cap`;\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 p-6 @xl:p-8\">\n          <StateSlot show={state === 'filled'}>\n            <figure className=\"m-0\">\n              <div role=\"img\" aria-labelledby={cap}>\n                <Chart kind={props.kind} points={props.points} />\n              </div>\n              <figcaption id={cap} className=\"mt-5 text-kh-small text-kh-ink-2\">\n                <Text path={['summary']} value={props.summary} as=\"span\" />\n              </figcaption>\n            </figure>\n            <details className=\"mt-5 border-t border-kh-line pt-2\">\n              <summary className=\"inline-flex min-h-11 cursor-pointer items-center rounded-kh-sm px-2 text-kh-small font-medium text-kh-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--kh-focus)\">\n                <Text path={['tableLabel']} value={props.tableLabel} as=\"span\" />\n              </summary>\n              <ScrollRegion label={props.tableHead.value} hint={<Text path={['scrollHint']} value={props.scrollHint} as=\"span\" />}>\n                <table className=\"mt-2 w-full text-kh-small\">\n                  <thead>\n                    <tr className=\"border-b border-kh-line text-left text-kh-muted\">\n                      <th scope=\"col\" className=\"py-2 pr-4 font-medium\">\n                        <Text path={['tableHead', 'label']} value={props.tableHead.label} as=\"span\" />\n                      </th>\n                      <th scope=\"col\" className=\"py-2 text-right font-medium\">\n                        <Text path={['tableHead', 'value']} value={props.tableHead.value} as=\"span\" />\n                      </th>\n                    </tr>\n                  </thead>\n                  <tbody className=\"divide-y divide-kh-line\">\n                    {props.points.map((p, i) => (\n                      <tr key={i}>\n                        <th scope=\"row\" className=\"py-2 pr-4 text-left font-normal\">\n                          <Text path={['points', i, 'label']} value={p.label} as=\"span\" />\n                        </th>\n                        <td className=\"py-2 text-right tabular-nums\">\n                          <Text path={['points', i, 'value']} value={p.value} as=\"span\" />\n                          <Opt path={['unit']} value={props.unit} as=\"span\" className=\"ml-1 text-kh-muted\" placeholder=\"Unit\" />\n                        </td>\n                      </tr>\n                    ))}\n                  </tbody>\n                </table>\n              </ScrollRegion>\n            </details>\n          </StateSlot>\n          <StateSlot show={state === 'loading'}>\n            <div aria-busy=\"true\">\n              <Skeleton className=\"h-56 w-full\" />\n            </div>\n            <p role=\"status\" className=\"mt-4 text-kh-small text-kh-muted\">\n              <Text path={['messages', 'loading']} value={messages.loading} as=\"span\" />\n            </p>\n          </StateSlot>\n          <StateSlot show={state === 'empty'}>\n            <EmptyState icon={<ChartColumn />} title={<Text path={['messages', 'empty']} value={messages.empty} as=\"span\" />} level={3} />\n          </StateSlot>\n          <StateSlot show={state === 'error'}>\n            <Banner tone=\"error\" action={<Button variant=\"secondary\"><Text path={['messages', 'retry']} value={messages.retry} as=\"span\" /></Button>}>\n              <Text path={['messages', 'error']} value={messages.error} as=\"span\" />\n            </Banner>\n          </StateSlot>\n        </AppPanel>\n      </Container>\n    </Section>\n  );\n}\n\nexport default withHeadingLevel(AppDashboardChart);\n"
    }
  ]
}
