{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "kit-app",
  "type": "registry:lib",
  "title": "Kit app",
  "description": "Helpers shared by the app blocks.",
  "dependencies": [],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/block-kit.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/blocks/_shared/app-kit.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/_shared/app-kit.tsx",
      "content": "// Pieces shared by the App UI blocks (settings, data, dashboard, onboarding, billing).\nimport { useCallback, useEffect, useRef, useState, type ReactNode } from 'react';\nimport { cn } from '../../lib/utils';\nimport { useEditing } from './kit';\n\n/** The framed surface every App UI block draws its screen on. */\nexport function AppPanel({ className, children }: { className?: string; children: ReactNode }) {\n  return <div className={cn('rounded-kh-xl border border-kh-line bg-kh-surface shadow-kh-1', className)}>{children}</div>;\n}\n\n/**\n * Copy that belongs to one state of a block (a saved note, an error, an empty message). It stays in the DOM, hidden,\n * while another state shows, so every string is reachable; in the canvas editor all states show so each can be edited.\n */\nexport function StateSlot({ show, className, children }: { show: boolean; className?: string; children: ReactNode }) {\n  const editing = useEditing();\n  return (\n    <div hidden={!show && !editing} className={className}>\n      {children}\n    </div>\n  );\n}\n\n/** Inline copy for one of two or more variants of a label (Show / Hide): the other stays in the DOM, hidden, so every string is reachable and editable in the canvas. */\nexport function Swap({ show, children }: { show: boolean; children: ReactNode }) {\n  const editing = useEditing();\n  return <span hidden={!show && !editing}>{children}</span>;\n}\n\n// ---------- State that follows props ----------\n\n/**\n * Local state that starts from `initial` and starts over when `initial` changes (compared by value). A block keeps its own\n * UI state (an open group, a ticked step) but must still obey its props after mount: the props panel, a host re-rendering\n * with new data, and the \"States\" previews all change props while the block stays mounted.\n */\nexport function useSynced<T>(initial: T, resetOn?: unknown): [T, (v: T | ((prev: T) => T)) => void] {\n  const key = JSON.stringify([initial, resetOn ?? null]); // `resetOn`: other props that should also start the state over\n  const [box, setBox] = useState({ key, value: initial });\n  if (box.key !== key) setBox({ key, value: initial }); // adjust during render: no stale frame, no effect\n  const value = box.key === key ? box.value : initial;\n  const set = useCallback((v: T | ((prev: T) => T)) => setBox((b) => ({ key: b.key, value: typeof v === 'function' ? (v as (p: T) => T)(b.value) : v })), []);\n  return [value, set];\n}\n\n// ---------- Typed values from display text ----------\n\n/**\n * A number from the text people write: \"12 000 kr\", \"1,840\", \"1 840\", \"1.840,50\", \"-5\", \"4.5k\" is not one (null).\n * A lone separator followed by exactly three digits is a thousands separator; otherwise it is the decimal point.\n * Text with no digits (Unlimited, none) is null.\n */\nexport function parseAmount(text: string): number | null {\n  const t = text.replace(/[\\u2212\\u2012\\u2013]/g, '-');\n  const m = t.replace(/[\\u00a0\\u202f\\s']/g, '').match(/\\d[\\d.,]*/);\n  if (!m) return null;\n  if (/\\d[.,]?\\d*\\s?[kKmM](?![a-zA-Z])/.test(t)) return null; // \"4.5k\": a shorthand we will not guess at\n  const neg = /^[^\\d]*-\\s?\\d/.test(t);\n  let n = m[0].replace(/[.,]$/, '');\n  const dots = (n.match(/\\./g) ?? []).length;\n  const commas = (n.match(/,/g) ?? []).length;\n  if (dots && commas) {\n    const dec = n.lastIndexOf('.') > n.lastIndexOf(',') ? '.' : ',';\n    n = n.replace(dec === '.' ? /,/g : /\\./g, '').replace(dec, '.');\n  } else if (dots > 1 || commas > 1) n = n.replace(/[.,]/g, '');\n  else if (dots + commas === 1) {\n    const sep = dots ? '.' : ',';\n    const [head = '', tail = ''] = n.split(sep);\n    n = tail.length === 3 && head.length <= 3 && head !== '0' ? head + tail : `${head}.${tail}`;\n  }\n  const v = Number(n);\n  if (!Number.isFinite(v)) return null;\n  return neg ? -v : v;\n}\n\nconst MONTHS: Record<string, number> = {};\n['jan', 'feb', 'mar', 'apr', 'may|maj', 'jun', 'jul', 'aug', 'sep', 'oct|okt', 'nov', 'dec'].forEach((m, i) => m.split('|').forEach((x) => (MONTHS[x] = i + 1)));\n\n/** A sortable number from a date written as \"2026-06-12\", \"12 Jun\", \"30 May 2026\" or \"12 juni\" (no year sorts by month then day); null if it is not a date. */\nexport function parseDate(text: string): number | null {\n  const iso = text.match(/(\\d{4})-(\\d{2})-(\\d{2})/);\n  if (iso) return Number(iso[1]) * 10000 + Number(iso[2]) * 100 + Number(iso[3]);\n  const m = text.match(/(\\d{1,2})\\.?\\s+([A-Za-zåäöÅÄÖ]{3,})\\.?(?:,?\\s+(\\d{4}))?/);\n  if (!m) return null;\n  const month = MONTHS[m[2]!.slice(0, 3).toLowerCase()];\n  if (!month) return null;\n  return (m[3] ? Number(m[3]) : 2000) * 10000 + month * 100 + Number(m[1]);\n}\n\nexport type SortKind = 'text' | 'number' | 'date';\n\n/** Orders two cells of one column by what they mean. `key` is the machine value when the row has one; empty or unparsable cells go last. */\nexport function compareCells(kind: SortKind, a: { text: string; key?: string }, b: { text: string; key?: string }, locale = 'sv'): number {\n  const pick = (c: { text: string; key?: string }) => (kind === 'number' ? parseAmount(c.key || c.text) : kind === 'date' ? parseDate(c.key || c.text) : null);\n  if (kind !== 'text') {\n    const x = pick(a);\n    const y = pick(b);\n    if (x !== null && y !== null) return x - y;\n    if (x !== null) return -1;\n    if (y !== null) return 1;\n  }\n  return new Intl.Collator(locale, { numeric: true, sensitivity: 'base' }).compare(a.key || a.text, b.key || b.text);\n}\n\n/** 47100 -> \"47 100\". Deterministic (no locale), so a server and a browser print the same. */\nexport const groupThousands = (n: number) => String(Math.round(n)).replace(/\\B(?=(\\d{3})+(?!\\d))/g, ' ');\n\n// ---------- A scroll region that shows there is more ----------\n\n/**\n * A sideways-scrolling region (focusable, named) with a fade at each edge that has more to scroll to, and a caption that\n * appears while anything is cut off. The page itself never scrolls sideways.\n */\nexport function ScrollRegion({ label, hint, hintText, children, className, innerClassName }: { label: string; hint: ReactNode; /** The hint's text, when the block can leave it empty: an empty hint draws nothing, not a ruled empty line. */ hintText?: string; children: ReactNode; className?: string; /** Extra classes on the scrolling element, e.g. a max height so a sticky header works inside it. */ innerClassName?: string }) {\n  const editing = useEditing();\n  const ref = useRef<HTMLDivElement>(null);\n  const [edge, setEdge] = useState({ start: false, end: false });\n  const measure = useCallback(() => {\n    const el = ref.current;\n    if (!el) return;\n    const start = el.scrollLeft > 1;\n    const end = el.scrollLeft + el.clientWidth < el.scrollWidth - 1;\n    setEdge((e) => (e.start === start && e.end === end ? e : { start, end }));\n  }, []);\n  useEffect(() => {\n    measure();\n    const el = ref.current;\n    if (!el || typeof ResizeObserver === 'undefined') return;\n    const ro = new ResizeObserver(measure);\n    ro.observe(el);\n    if (el.firstElementChild) ro.observe(el.firstElementChild);\n    return () => ro.disconnect();\n  }, [measure]);\n  return (\n    <div className={cn('relative', className)}>\n      <div ref={ref} onScroll={measure} role=\"region\" aria-label={label} tabIndex={0} className={cn('relative overflow-x-auto focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-(--kh-focus)', innerClassName)}>\n        {children}\n      </div>\n      <span aria-hidden className={cn('pointer-events-none absolute inset-y-0 left-0 w-8 bg-linear-to-r from-kh-surface to-transparent', !edge.start && 'hidden')} />\n      <span aria-hidden className={cn('pointer-events-none absolute inset-y-0 right-0 w-8 bg-linear-to-l from-kh-surface to-transparent', !edge.end && 'hidden')} />\n      {(hintText === undefined || hintText !== '' || editing) && (\n        <p hidden={!edge.start && !edge.end && !editing} className=\"border-t border-kh-line px-4 py-2 text-kh-caption text-kh-muted\">\n          {hint}\n        </p>\n      )}\n    </div>\n  );\n}\n"
    }
  ]
}
