{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "app-scheduling-slots",
  "type": "registry:block",
  "title": "App, time-slot picker",
  "description": "Pick a time: days across, times down, as a keyboard grid (arrow keys, Home and End, Space picks). A taken slot says so in words, the choice is repeated in a status line, and idle, error, confirmed and empty states. Never submits.",
  "categories": [
    "app"
  ],
  "dependencies": [
    "lucide-react@^1.49.0",
    "zod@^4.6.5"
  ],
  "registryDependencies": [
    "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/kit-app.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/blocks/app-scheduling-slots/spec.ts",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-scheduling-slots/spec.ts",
      "content": "import { z } from 'zod';\nimport { defineSpec, req, t } from '../_shared/schema';\n\nconst schema = z.object({\n  eyebrow: t(60).optional(),\n  title: t(120),\n  description: t(300).optional(),\n  /** Names the grid of times. */\n  legend: t(60),\n  /** What the keyboard does, shown under the grid. */\n  help: t(160),\n  days: z.array(z.object({ label: t(20), unavailable: z.array(t(10)).max(8) })).min(2).max(7),\n  times: z.array(t(10)).min(2).max(8),\n  /** The slot the grid starts on (a day label and a time from the lists above). Not editable copy. */\n  selected: z.object({ day: t(20), time: t(10) }).optional(),\n  /** Shown under the grid when there are more than five days: the grid scrolls sideways. */\n  scrollHint: t(60),\n  /** A taken slot says this in words (and in its name), never only by its look. */\n  fullLabel: t(20),\n  summary: t(40),\n  none: t(60),\n  at: t(10),\n  confirm: req(30),\n  /** After a booking is confirmed this unlocks the grid again so it can be changed. */\n  change: req(30),\n  /** idle, error (Confirm was pressed with nothing chosen), confirmed (the choice is locked and said), empty (no times this week). */\n  state: z.enum(['idle', 'error', 'confirmed', 'empty']),\n  messages: z.object({ error: t(120), confirmed: t(120), empty: t(160), taken: t(120) }),\n});\n\nexport const spec = defineSpec({\n  type: 'app-scheduling-slots',\n  aliases: ['app/scheduling-slots'],\n  name: 'App, time-slot picker',\n  category: 'app',\n  role: 'body',\n  description: 'Pick a time: days across, times down, as a keyboard grid (arrow keys, Home and End, Space picks). A taken slot says so in words, the choice is repeated in a status line, and idle, error, confirmed and empty states. Never submits.',\n  schema,\n  defaults: {\n    \"eyebrow\": \"Name the service owner\",\n    \"title\": \"Say what the person is choosing a time for\",\n    \"description\": \"Say how long it takes and until when it can be changed.\",\n    \"legend\": \"Available times\",\n    \"help\": \"Say which keys move between times and which one picks.\",\n    \"days\": [\n      {\n        \"label\": \"Day one\",\n        \"unavailable\": [\n          \"09:00\",\n          \"10:00\"\n        ]\n      },\n      {\n        \"label\": \"Day two\",\n        \"unavailable\": []\n      },\n      {\n        \"label\": \"Day three\",\n        \"unavailable\": [\n          \"13:00\",\n          \"14:00\",\n          \"15:00\"\n        ]\n      },\n      {\n        \"label\": \"Day four\",\n        \"unavailable\": [\n          \"11:00\"\n        ]\n      },\n      {\n        \"label\": \"Day five\",\n        \"unavailable\": []\n      }\n    ],\n    \"times\": [\n      \"09:00\",\n      \"10:00\",\n      \"11:00\",\n      \"13:00\",\n      \"14:00\",\n      \"15:00\"\n    ],\n    \"selected\": {\n      \"day\": \"Day two\",\n      \"time\": \"10:00\"\n    },\n    \"scrollHint\": \"Say there are more days to the side.\",\n    \"fullLabel\": \"Full\",\n    \"summary\": \"Selected\",\n    \"none\": \"Say nothing is chosen yet\",\n    \"at\": \"at\",\n    \"confirm\": \"Confirm booking\",\n    \"change\": \"Change booking\",\n    \"state\": \"idle\",\n    \"messages\": {\n      \"error\": \"Say to choose a time first.\",\n      \"confirmed\": \"Say it is booked and where the confirmation goes.\",\n      \"empty\": \"Say no times are free and what to try next.\",\n      \"taken\": \"Say that time is taken and to choose another.\"\n    }\n  },\n});\n"
    },
    {
      "path": "src/blocks/app-scheduling-slots/index.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-scheduling-slots/index.tsx",
      "content": "import { useId, useRef, type KeyboardEvent } from 'react';\nimport { Check } from 'lucide-react';\nimport { Text, withHeadingLevel } from '../../editing';\nimport { cn } from '../../lib/utils';\nimport { Button } from '../../ui/button';\nimport { Container, Section, SectionHeader, useEditing } from '../_shared/kit';\nimport { AppPanel, StateSlot, 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>;\ntype Local = null | 'error' | 'confirmed' | 'taken';\n\n/** Days across, times down. One tab stop (the current cell); arrows, Home and End move; Space or Enter picks. A taken slot says so in words. */\nfunction AppSchedulingSlots({ props }: BlockProps<Props>) {\n  const id = useId();\n  const editing = useEditing();\n  const cells = useRef(new Map<string, HTMLButtonElement>());\n  const taken = (d: number, r: number) => props.days[d]!.unavailable.includes(props.times[r]!);\n  const start = (() => {\n    const d = props.days.findIndex((x) => x.label === props.selected?.day);\n    const r = props.times.indexOf(props.selected?.time ?? '');\n    return d >= 0 && r >= 0 && !taken(d, r) ? { d, r } : null;\n  })();\n  // The pick, the roving cell and what the person has done follow the props: the panel, a parent re-rendering and the States\n  // previews all change them while the block stays mounted.\n  const model = [props.days, props.times, props.selected];\n  const [sel, setSel] = useSynced<{ d: number; r: number } | null>(start, model);\n  const [pos, setPos] = useSynced(start ?? { d: 0, r: 0 }, model);\n  const [local, setLocal] = useSynced<Local>(null, [props.state, model]);\n  const eff: Local | 'empty' = local ?? (props.state === 'error' ? 'error' : props.state === 'confirmed' ? 'confirmed' : props.state === 'empty' ? 'empty' : null);\n  const confirmBtn = useRef<HTMLButtonElement>(null);\n  const locked = eff === 'confirmed';\n  const cols = props.days.length;\n  const rows = props.times.length;\n  const go = (d: number, r: number) => {\n    const next = { d: Math.max(0, Math.min(cols - 1, d)), r: Math.max(0, Math.min(rows - 1, r)) };\n    setPos(next);\n    cells.current.get(`${next.d}:${next.r}`)?.focus();\n  };\n  const onKey = (e: KeyboardEvent) => {\n    const k = e.key;\n    const to: Record<string, [number, number]> = {\n      ArrowLeft: [pos.d - 1, pos.r], ArrowRight: [pos.d + 1, pos.r], ArrowUp: [pos.d, pos.r - 1], ArrowDown: [pos.d, pos.r + 1],\n      Home: [0, e.ctrlKey ? 0 : pos.r], End: [cols - 1, e.ctrlKey ? rows - 1 : pos.r],\n    };\n    if (to[k]) {\n      e.preventDefault();\n      go(...to[k]!);\n    }\n  };\n  const choose = (d: number, r: number) => {\n    if (locked) return;\n    if (taken(d, r)) return setLocal('taken'); // said in words, not just ignored\n    setSel({ d, r });\n    setLocal((l) => (l === 'error' || l === 'taken' ? null : l));\n  };\n  const firstFree = () => {\n    for (let c = 0; c < cols; c += 1) for (let r = 0; r < rows; r += 1) if (!taken(c, r)) return { d: c, r };\n    return pos;\n  };\n  const confirm = () => {\n    if (locked) return; // aria-disabled, not disabled: the button keeps focus\n    if (!sel) {\n      setLocal('error');\n      const f = taken(pos.d, pos.r) ? firstFree() : pos; // never park focus on a full slot\n      go(f.d, f.r);\n    } else setLocal('confirmed');\n  };\n  const change = () => {\n    setLocal(null);\n    if (sel) setPos(sel);\n    confirmBtn.current?.focus();\n  };\n  const summary = sel ? `${props.summary}: ${props.days[sel.d]!.label} ${props.at} ${props.times[sel.r]}` : `${props.summary}: ${props.none}`;\n  return (\n    <Section labelledBy={id}>\n      <Container width=\"narrow\">\n        <SectionHeader id={id} eyebrow={props.eyebrow} title={props.title} description={props.description} />\n        <AppPanel className=\"mt-10 p-4 @xl:p-6\">\n          <StateSlot show={eff !== 'empty'}>\n            <p id={`${id}-g`} className=\"mb-3 text-kh-small font-medium\">\n              <Text path={['legend']} value={props.legend} as=\"span\" />\n            </p>\n            <div className=\"overflow-x-auto\">\n              <table role=\"grid\" aria-labelledby={`${id}-g`} aria-readonly={locked || undefined} onKeyDown={onKey} className={cn('w-full border-separate border-spacing-1 text-center', cols > 5 ? 'min-w-[30rem]' : 'min-w-[18rem]')}>\n                <thead>\n                  <tr>\n                    {props.days.map((d, c) => (\n                      <th key={c} scope=\"col\" className=\"px-1 pb-1 text-kh-caption font-medium text-kh-muted\">\n                        <Text path={['days', c, 'label']} value={d.label} as=\"span\" />\n                      </th>\n                    ))}\n                  </tr>\n                </thead>\n                <tbody>\n                  {props.times.map((time, r) => (\n                    <tr key={r}>\n                      {props.days.map((d, c) => {\n                        const full = taken(c, r);\n                        const on = sel?.d === c && sel.r === r;\n                        return (\n                          <td key={c} role=\"gridcell\" aria-selected={on} className=\"p-0\">\n                            <button\n                              type=\"button\"\n                              ref={(el) => {\n                                if (el) cells.current.set(`${c}:${r}`, el);\n                              }}\n                              tabIndex={pos.d === c && pos.r === r ? 0 : -1}\n                              aria-disabled={full || locked || undefined}\n                              aria-label={`${d.label} ${time}${full ? `, ${props.fullLabel}` : ''}`}\n                              onFocus={() => setPos({ d: c, r })}\n                              onClick={() => choose(c, r)}\n                              className={cn(\n                                'flex min-h-11 w-full flex-col items-center justify-center rounded-kh border px-1 py-1 text-kh-small tabular-nums',\n                                'focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-(--kh-focus)',\n                                full ? 'border-dashed border-kh-line-strong bg-kh-surface-3 text-kh-muted' : on ? 'border-kh-ink bg-kh-ink text-kh-on-ink' : 'border-kh-line-strong bg-kh-surface text-kh-ink hover:bg-kh-surface-2',\n                                (full || locked) && 'cursor-not-allowed',\n                              )}\n                            >\n                              <span aria-hidden className={cn('inline-flex items-center gap-1', full && 'line-through')}>\n                                {on && <Check className=\"size-3.5\" />}\n                                {c === 0 ? <Text path={['times', r]} value={time} as=\"span\" /> : time}\n                              </span>\n                              {full && (\n                                <span aria-hidden className=\"text-kh-caption\">\n                                  <Text path={['fullLabel']} value={props.fullLabel} as=\"span\" />\n                                </span>\n                              )}\n                            </button>\n                          </td>\n                        );\n                      })}\n                    </tr>\n                  ))}\n                </tbody>\n              </table>\n            </div>\n            {(cols > 5 || editing) && (\n              <p className=\"mt-2 text-kh-caption text-kh-muted\">\n                <Text path={['scrollHint']} value={props.scrollHint} as=\"span\" />\n              </p>\n            )}\n            <p className=\"mt-3 text-kh-caption text-kh-muted\">\n              <Text path={['help']} value={props.help} as=\"span\" />\n            </p>\n            <p role=\"status\" className=\"mt-4 text-kh-small font-medium\">\n              {summary}\n            </p>\n            {editing && (\n              <ul className=\"mt-2 text-kh-caption text-kh-muted\">\n                {props.days.map((d, c) => (\n                  <li key={c}>\n                    {d.label}: {d.unavailable.map((u, k) => <Text key={k} path={['days', c, 'unavailable', k]} value={u} as=\"span\" className=\"mr-2\" />)}\n                  </li>\n                ))}\n              </ul>\n            )}\n            <div hidden={!editing} className=\"text-kh-caption text-kh-muted\">\n              <Text path={['fullLabel']} value={props.fullLabel} as=\"span\" /> <Text path={['summary']} value={props.summary} as=\"span\" /> <Text path={['none']} value={props.none} as=\"span\" /> <Text path={['at']} value={props.at} as=\"span\" />\n            </div>\n            <div className=\"mt-4 flex flex-wrap items-center gap-3\">\n              <Button ref={confirmBtn} type=\"button\" onClick={confirm} aria-disabled={locked || undefined} className=\"aria-disabled:opacity-50\">\n                <Text path={['confirm']} value={props.confirm} as=\"span\" />\n              </Button>\n              <Button type=\"button\" variant=\"ghost\" hidden={!locked && !editing} onClick={change}>\n                <Text path={['change']} value={props.change} as=\"span\" />\n              </Button>\n            </div>\n          </StateSlot>\n          <StateSlot show={eff === 'error'}>\n            <p role=\"alert\" className=\"mt-3 rounded-kh-lg border border-kh-danger px-4 py-3 text-kh-small\">\n              <Text path={['messages', 'error']} value={props.messages.error} as=\"span\" />\n            </p>\n          </StateSlot>\n          <StateSlot show={eff === 'taken'}>\n            <p role=\"alert\" className=\"mt-3 rounded-kh-lg border border-kh-line-strong px-4 py-3 text-kh-small\">\n              <Text path={['messages', 'taken']} value={props.messages.taken} as=\"span\" />\n            </p>\n          </StateSlot>\n          <StateSlot show={eff === 'confirmed'}>\n            <p role=\"status\" className=\"mt-3 rounded-kh-lg bg-kh-accent-soft px-4 py-3 text-kh-small text-kh-accent-ink\">\n              <Text path={['messages', 'confirmed']} value={props.messages.confirmed} as=\"span\" />\n            </p>\n          </StateSlot>\n          <StateSlot show={eff === 'empty'}>\n            <p role=\"status\" className=\"px-2 py-6 text-center text-kh-small text-kh-muted\">\n              <Text path={['messages', 'empty']} value={props.messages.empty} as=\"span\" />\n            </p>\n          </StateSlot>\n        </AppPanel>\n      </Container>\n    </Section>\n  );\n}\n\nexport default withHeadingLevel(AppSchedulingSlots);\n"
    }
  ]
}
