{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "app-notifications-inbox",
  "type": "registry:block",
  "title": "App, notification inbox",
  "description": "An inbox of notifications grouped by day: unread items marked with a dot and the word, an unread count in a live region, All and Unread filters, and Mark all as read. Opening an item marks it read in place. Filled and empty states.",
  "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/empty-state.json",
    "https://ui.klarhimmel.com/r/kit-app.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/blocks/app-notifications-inbox/spec.ts",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-notifications-inbox/spec.ts",
      "content": "import { z } from 'zod';\nimport { defineSpec, t, req } from '../_shared/schema';\n\nconst item = z.object({ title: t(80), body: t(160), time: t(20), unread: z.boolean(), href: t(300) });\n\nconst schema = z.object({\n  eyebrow: t(60).optional(),\n  title: t(120),\n  description: t(300).optional(),\n  /** Names the filter buttons' group. Not shown. */\n  filterLabel: t(40),\n  filters: z.object({ all: req(20), unread: req(20) }),\n  /** Follows the unread count: \"3 unread\". */\n  unreadSuffix: t(20),\n  /** Said beside an unread item for screen readers, and drawn as a dot. */\n  unreadWord: t(20),\n  markAll: req(30),\n  /** One heading per day, newest first. */\n  groups: z.array(z.object({ label: req(30), items: z.array(item).min(1).max(5) })).min(1).max(3),\n  /** filled: the list. empty: nothing has arrived. */\n  state: z.enum(['filled', 'empty']),\n  messages: z.object({ emptyTitle: req(80), emptyBody: t(160), allRead: t(80) }),\n});\n\nexport const spec = defineSpec({\n  type: 'app-notifications-inbox',\n  aliases: ['app/notifications-inbox'],\n  name: 'App, notification inbox',\n  category: 'app',\n  role: 'body',\n  description: 'An inbox of notifications grouped by day: unread items marked with a dot and the word, an unread count in a live region, All and Unread filters, and Mark all as read. Opening an item marks it read in place. Filled and empty states.',\n  schema,\n  defaults: {\n    \"eyebrow\": \"Inbox\",\n    \"title\": \"Notifications\",\n    \"description\": \"Say what happened while the person was away.\",\n    \"filterLabel\": \"Show\",\n    \"filters\": {\n      \"all\": \"All\",\n      \"unread\": \"Unread\"\n    },\n    \"unreadSuffix\": \"unread\",\n    \"unreadWord\": \"Unread\",\n    \"markAll\": \"Mark all as read\",\n    \"groups\": [\n      {\n        \"label\": \"Today\",\n        \"items\": [\n          {\n            \"title\": \"Say what happened, in a short line\",\n            \"body\": \"Add the detail that matters.\",\n            \"time\": \"00:00\",\n            \"unread\": true,\n            \"href\": \"#n1\"\n          },\n          {\n            \"title\": \"Say what happened next\",\n            \"body\": \"Add the amount or the name.\",\n            \"time\": \"00:00\",\n            \"unread\": true,\n            \"href\": \"#n2\"\n          }\n        ]\n      },\n      {\n        \"label\": \"Yesterday\",\n        \"items\": [\n          {\n            \"title\": \"Say what changed\",\n            \"body\": \"Say what that frees up or sets in motion.\",\n            \"time\": \"00:00\",\n            \"unread\": true,\n            \"href\": \"#n3\"\n          },\n          {\n            \"title\": \"Say what is ready for them\",\n            \"body\": \"Add the key number.\",\n            \"time\": \"00:00\",\n            \"unread\": false,\n            \"href\": \"#n4\"\n          }\n        ]\n      }\n    ],\n    \"state\": \"filled\",\n    \"messages\": {\n      \"emptyTitle\": \"Nothing new\",\n      \"emptyBody\": \"Say which events show up here.\",\n      \"allRead\": \"Say everything has been seen.\"\n    }\n  },\n});\n"
    },
    {
      "path": "src/blocks/app-notifications-inbox/index.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-notifications-inbox/index.tsx",
      "content": "import { useEffect, useId, useRef } from 'react';\nimport { BellOff } from 'lucide-react';\nimport { Text, withHeadingLevel } from '../../editing';\nimport { cn } from '../../lib/utils';\nimport { Button, safeHref } from '../../ui/button';\nimport { EmptyState } from '../../ui/empty-state';\nimport { Container, Section, SectionHeader, Named } 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>;\n\nfunction AppNotificationsInbox({ props }: BlockProps<Props>) {\n  const id = useId();\n  const key = (g: number, i: number) => `${g}.${i}`;\n  const [read, setRead] = useSynced<string[]>(props.groups.flatMap((g, gi) => g.items.flatMap((it, i) => (it.unread ? [] : [key(gi, i)]))));\n  const [only, setOnly] = useSynced(false, props.groups);\n  const links = useRef<Record<string, HTMLAnchorElement | null>>({});\n  const unreadBtn = useRef<HTMLButtonElement>(null);\n  const after = useRef<string | 'filter' | null>(null);\n  // Opening an item under the Unread filter removes its row: focus goes to the next row's link, or to the filter button.\n  useEffect(() => {\n    const a = after.current;\n    after.current = null;\n    if (a === null) return;\n    if (a === 'filter') unreadBtn.current?.focus();\n    else (links.current[a] ?? unreadBtn.current)?.focus();\n  }, [read]);\n  const isRead = (g: number, i: number) => read.includes(key(g, i));\n  const unread = props.groups.reduce((n, g, gi) => n + g.items.filter((_, i) => !isRead(gi, i)).length, 0);\n  const everyRead = unread === 0;\n  const toggle = 'inline-flex min-h-11 items-center rounded-kh px-4 text-kh-small font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--kh-focus)';\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\">\n          <StateSlot show={props.state === 'filled'}>\n            <div className=\"flex flex-wrap items-center justify-between gap-3 border-b border-kh-line px-4 py-3 @xl:px-6\">\n              <div role=\"group\" aria-labelledby={`${id}-f`} className=\"flex gap-1\">\n                <span id={`${id}-f`} className=\"sr-only\">\n                  <Text path={['filterLabel']} value={props.filterLabel} as=\"span\" />\n                </span>\n                {([['all', false], ['unread', true]] as const).map(([k, v]) => (\n                  <Named as=\"button\" key={k} ref={v ? unreadBtn : undefined} type=\"button\" aria-pressed={only === v} onClick={() => setOnly(v)} className={cn(toggle, only === v ? 'border-2 border-kh-ink bg-kh-surface-3 font-semibold text-kh-ink' : 'border-2 border-transparent text-kh-ink-2 hover:bg-kh-surface-2')}>\n                    <Text path={['filters', k]} value={props.filters[k]} as=\"span\" />\n                  </Named>\n                ))}\n              </div>\n              <p role=\"status\" className=\"text-kh-small tabular-nums text-kh-muted\">\n                {unread} <Text path={['unreadSuffix']} value={props.unreadSuffix} as=\"span\" />\n              </p>\n              <Button variant=\"secondary\" aria-disabled={everyRead || undefined} className=\"aria-disabled:opacity-50\" onClick={() => !everyRead && setRead(props.groups.flatMap((g, gi) => g.items.map((_, i) => key(gi, i))))}>\n                <Text path={['markAll']} value={props.markAll} as=\"span\" />\n              </Button>\n            </div>\n            <StateSlot show={only && everyRead} className=\"px-6 py-8 text-center text-kh-small text-kh-muted\">\n              <Text path={['messages', 'allRead']} value={props.messages.allRead} as=\"p\" />\n            </StateSlot>\n            {props.groups.map((g, gi) => {\n              const items = g.items.map((it, i) => ({ it, i })).filter(({ i }) => !(only && isRead(gi, i)));\n              const gid = `${id}-g${gi}`;\n              return (\n                <section key={gi} aria-labelledby={gid} hidden={items.length === 0}>\n                  <h3 id={gid} className=\"bg-kh-surface-2 px-4 py-2 text-kh-caption font-medium uppercase tracking-wide text-kh-muted @xl:px-6\">\n                    <Text path={['groups', gi, 'label']} value={g.label} as=\"span\" />\n                  </h3>\n                  <ul className=\"divide-y divide-kh-line\">\n                    {items.map(({ it, i }) => {\n                      const u = !isRead(gi, i);\n                      return (\n                        <li key={i} className=\"flex gap-3 px-4 py-3 @xl:px-6\">\n                          <span aria-hidden className={cn('mt-2.5 size-2 shrink-0 rounded-kh-pill', u ? 'bg-kh-accent' : 'bg-transparent')} />\n                          <div className=\"min-w-0 flex-1\">\n                            <a\n                              href={safeHref(it.href)}\n                              ref={(el) => {\n                                links.current[key(gi, i)] = el;\n                              }}\n                              onClick={() => {\n                                if (only && u) {\n                                  // the row is about to leave the list: say where focus goes\n                                  const order = props.groups.flatMap((gg, g2) => gg.items.map((_, i2) => ({ g2, i2 }))).filter(({ g2, i2 }) => !isRead(g2, i2));\n                                  const at = order.findIndex((o) => o.g2 === gi && o.i2 === i);\n                                  const nx = order[at + 1] ?? order[at - 1];\n                                  after.current = nx ? key(nx.g2, nx.i2) : 'filter';\n                                }\n                                setRead((r) => (r.includes(key(gi, i)) ? r : [...r, key(gi, i)]));\n                              }}\n                              className={cn('inline-flex min-h-11 items-center gap-2 rounded-kh-sm text-kh-body hover:underline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--kh-focus)', u ? 'font-semibold text-kh-ink' : 'text-kh-ink-2')}\n                            >\n                              <Text path={['groups', gi, 'items', i, 'title']} value={it.title} as=\"span\" />\n                              {u && (\n                                <span className=\"rounded-kh-pill bg-kh-accent-soft px-2 text-kh-caption font-medium text-kh-accent-ink\">\n                                  <Text path={['unreadWord']} value={props.unreadWord} as=\"span\" />\n                                </span>\n                              )}\n                            </a>\n                            <Text path={['groups', gi, 'items', i, 'body']} value={it.body} as=\"p\" className=\"text-kh-small text-kh-muted\" />\n                          </div>\n                          <Text path={['groups', gi, 'items', i, 'time']} value={it.time} as=\"span\" className=\"max-w-[40%] shrink-0 pt-3 text-kh-caption tabular-nums text-kh-muted\" />\n                        </li>\n                      );\n                    })}\n                  </ul>\n                </section>\n              );\n            })}\n          </StateSlot>\n          <StateSlot show={props.state === 'empty'}>\n            <EmptyState icon={<BellOff />} title={<Text path={['messages', 'emptyTitle']} value={props.messages.emptyTitle} as=\"span\" />} level={3}>\n              <Text path={['messages', 'emptyBody']} value={props.messages.emptyBody} as=\"span\" />\n            </EmptyState>\n          </StateSlot>\n        </AppPanel>\n      </Container>\n    </Section>\n  );\n}\n\nexport default withHeadingLevel(AppNotificationsInbox);\n"
    }
  ]
}
