{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "kit-d-chrome",
  "type": "registry:lib",
  "title": "Kit d-chrome",
  "description": "Helpers shared by the chrome blocks.",
  "dependencies": [
    "lucide-react@^1.49.0"
  ],
  "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/input.json",
    "https://ui.klarhimmel.com/r/kbd.json",
    "https://ui.klarhimmel.com/r/kit-form.json",
    "https://ui.klarhimmel.com/r/theme.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/blocks/_shared/d-chrome-kit.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/_shared/d-chrome-kit.tsx",
      "content": "// Helpers for the chrome and conversion blocks (navbars, footers, banners, forms). React lives here, never in spec files.\nimport { useCallback, useEffect, useId, useRef, useState, useSyncExternalStore, type ReactNode, type RefObject } from 'react';\nimport { Menu, Moon, Search, Sun, X } from 'lucide-react';\nimport { Text, type PropPath } from '../../editing';\nimport { Button } from '../../ui/button';\nimport { Input } from '../../ui/input';\nimport { Kbd } from '../../ui/kbd';\nimport { cn } from '../../lib/utils';\nimport { MODE_VARS, readAppearance, setAppearance } from '../../theme/appearance';\nimport { FormExtras, useFormProps, type FormSettings } from './form-kit';\nimport { useEditing, useNamed } from './kit';\n\nexport const FOCUS = 'focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--kh-focus)';\n\n/** Classes for a text link in a navbar or footer row. */\nexport const navLinkClass = cn(\n  'inline-flex min-h-11 items-center rounded-kh px-3 text-kh-small font-medium text-kh-ink-2 transition-colors hover:bg-kh-surface-2 hover:text-kh-ink',\n  // The current page is marked by more than colour: bolder, ink, and an underline in the accent.\n  'aria-[current=page]:font-semibold aria-[current=page]:text-kh-ink aria-[current=page]:underline aria-[current=page]:decoration-kh-accent aria-[current=page]:decoration-2 aria-[current=page]:underline-offset-8',\n  FOCUS,\n);\n\n/** A footer link to the current page, added to that link only (so a footer with no current draws as before): marked by more than colour, as the navbar marks it, but quieter. */\nexport const footCurrentClass = 'font-medium text-kh-ink underline decoration-kh-accent decoration-2 underline-offset-4';\n\nconst noSubscribe = () => () => {};\n/**\n * False in server-rendered HTML and while hydrating, true once React runs in the browser (and always inside an editor).\n * Controls that do nothing without JavaScript (search trigger, theme toggle) render only when this is true, so a page\n * served without scripts has no dead buttons.\n */\nexport function useInteractive(): boolean {\n  const editing = useEditing();\n  return useSyncExternalStore(noSubscribe, () => true, () => false) || editing;\n}\n\n/**\n * Disclosure state for a collapsing menu. Escape closes it and returns focus to the button, a click or focus\n * leaving the header closes it, and the editor always sees it open so every label stays reachable.\n */\nexport function useMenu() {\n  const [open, setOpen] = useState(false);\n  const editing = useEditing();\n  const menuId = useId();\n  const buttonRef = useRef<HTMLButtonElement>(null);\n  const rootRef = useRef<HTMLElement>(null);\n  const summaryRef = useRef<HTMLElement>(null);\n  const detailsRef = useRef<HTMLDetailsElement>(null);\n\n  // A <details> menu may have been opened natively before this hydrated: adopt its state.\n  useEffect(() => {\n    if (detailsRef.current?.open) setOpen(true);\n  }, []);\n\n  // Escape and outside clicks are listened for always, and look at the real <details> as well as React's `open`: a menu opened\n  // natively (before hydration, or while the state and the element disagree) still closes, and focus returns to its summary.\n  useEffect(() => {\n    const isOpen = () => open || !!detailsRef.current?.open;\n    const close = () => {\n      if (detailsRef.current) detailsRef.current.open = false;\n      setOpen(false);\n    };\n    const away = (e: PointerEvent) => {\n      if (isOpen() && rootRef.current && !rootRef.current.contains(e.target as Node)) close();\n    };\n    // One Escape closes one menu, the innermost first: a disclosure open inside this one (a dropdown in the mobile sheet) goes\n    // before this one, and a press another menu has already used (defaultPrevented) is left alone.\n    const esc = (e: globalThis.KeyboardEvent) => {\n      if (e.key !== 'Escape' || !isOpen() || e.defaultPrevented || detailsRef.current?.querySelector('details[open]')) return;\n      e.preventDefault();\n      close();\n      if (!document.activeElement || document.activeElement === document.body || rootRef.current?.contains(document.activeElement)) (buttonRef.current ?? summaryRef.current)?.focus();\n    };\n    document.addEventListener('pointerdown', away);\n    document.addEventListener('keydown', esc);\n    return () => {\n      document.removeEventListener('pointerdown', away);\n      document.removeEventListener('keydown', esc);\n    };\n  }, [open]);\n\n  const onBlur = useCallback((e: React.FocusEvent) => {\n    const next = e.relatedTarget as Node | null;\n    if (open && next && rootRef.current && !rootRef.current.contains(next)) setOpen(false);\n  }, [open]);\n\n  return { shown: open || editing, open, setOpen, menuId, buttonRef, summaryRef, detailsRef, rootRef, rootProps: { onBlur } };\n}\n\n/** The disclosure button of a collapsing menu. Hide it from the width where the menu is inline with `className`. */\nexport function MenuButton({ shown, menuId, onToggle, buttonRef, className }: {\n  shown: boolean; menuId: string; onToggle: () => void; buttonRef: RefObject<HTMLButtonElement | null>; className?: string;\n}) {\n  return (\n    <button\n      ref={buttonRef}\n      type=\"button\"\n      aria-expanded={shown}\n      aria-controls={menuId}\n      aria-label={shown ? 'Close menu' : 'Open menu'}\n      onClick={onToggle}\n      className={cn('inline-flex size-11 shrink-0 items-center justify-center rounded-kh text-kh-ink transition-colors hover:bg-kh-surface-2', FOCUS, className)}\n    >\n      {shown ? <X aria-hidden className=\"size-5\" /> : <Menu aria-hidden className=\"size-5\" />}\n    </button>\n  );\n}\n\n/**\n * The menu as a native <details>: the summary opens and closes it with no JavaScript, and with JavaScript the open state\n * is mirrored into React (Escape, outside click, focus leaving and link clicks close it). `className` goes on the summary\n * (hide it from the width where the menu is inline); `detailsClass` on the <details> (wide layout: keep the content visible).\n * Children are the menu panel. The summary swaps its icon with CSS so it needs no state either.\n */\nexport function MenuDetails({ shown, onToggle, summaryRef, detailsRef, className, detailsClass, children }: {\n  shown: boolean; onToggle: (open: boolean) => void; summaryRef: RefObject<HTMLElement | null>; detailsRef: RefObject<HTMLDetailsElement | null>;\n  className?: string; detailsClass?: string; children: ReactNode;\n}) {\n  // Without script nothing keeps aria-expanded true while the native <details> is open, so the static render leaves it out rather\n  // than say \"collapsed\" over an open menu (a second agent's walk of the library rows, KB-364); hydrated, it follows the state.\n  const interactive = useInteractive();\n  return (\n    <details\n      ref={detailsRef}\n      open={shown}\n      onToggle={(e) => onToggle(e.currentTarget.open)}\n      // A tap on the open panel's empty space (not a link, button or field) closes it, as a tap outside does: on a phone the panel\n      // fills the screen, so there is no \"outside\" left to tap.\n      // The path is read as dispatched: a control that swaps its own content on click (ThemeToggle's icon) has detached e.target by now.\n      onClick={(e) => {\n        const hit = e.nativeEvent.composedPath().some((n) => n instanceof Element && n.matches('summary, a[href], button, input, select, textarea, label, [role], [tabindex]'));\n        if (!detailsRef.current?.open || hit) return;\n        detailsRef.current.open = false;\n        onToggle(false);\n      }}\n      className={cn('group/menu', detailsClass)}\n    >\n      <summary\n        ref={summaryRef}\n        // WebKit exposes a bare <summary> as an unnamed group, so VoiceOver on iPhone never hears \"Menu\": say it is a button and whether it is open.\n        role=\"button\"\n        aria-expanded={interactive ? shown : undefined}\n        aria-label=\"Menu\"\n        className={cn('inline-flex size-11 shrink-0 cursor-pointer list-none items-center justify-center rounded-kh text-kh-ink transition-colors hover:bg-kh-surface-2 [&::-webkit-details-marker]:hidden', FOCUS, className)}\n      >\n        <Menu aria-hidden className=\"size-5 group-open/menu:hidden\" />\n        <X aria-hidden className=\"hidden size-5 group-open/menu:block\" />\n      </summary>\n      {children}\n    </details>\n  );\n}\n\n/**\n * Search trigger (\"Search ⌘K\"). It opens nothing itself: it dispatches a bubbling `kh:search` event the host listens for.\n * `compact` keeps the label for screen readers and shows only the icon and hint, until the container is wide enough.\n */\nexport function SearchTrigger({ path, label, hint, className, compactLabel }: {\n  path: PropPath; label: string; hint: string; className?: string; compactLabel?: string;\n}) {\n  const editing = useEditing();\n  const interactive = useInteractive();\n  if (!interactive) return null;\n  return (\n    <button\n      type=\"button\"\n      aria-haspopup=\"dialog\"\n      aria-keyshortcuts=\"Meta+K Control+K\"\n      onClick={(e) => e.currentTarget.dispatchEvent(new CustomEvent('kh:search', { bubbles: true }))}\n      className={cn(\n        'inline-flex min-h-11 items-center gap-2.5 rounded-kh border border-kh-line-strong bg-kh-surface px-3 text-kh-small text-kh-muted transition-colors hover:border-kh-muted hover:text-kh-ink',\n        FOCUS,\n        className,\n      )}\n    >\n      <Search aria-hidden strokeWidth={1.75} className=\"size-4 shrink-0\" />\n      <Text path={[...path, 'label']} value={label} as=\"span\" className={cn(!editing && compactLabel)} />\n      {(hint || editing) && <Kbd className=\"ml-auto\"><Text path={[...path, 'hint']} value={hint} as=\"span\" placeholder=\"Hint\" /></Kbd>}\n    </button>\n  );\n}\n\n\n/**\n * Light/dark toggle. Hydrated apps: click dispatches `kh:theme-toggle` and <Theme> owns `data-kh-mode`. Static sites wire\n * `[data-kh-theme-toggle]` in `/js/theme.js` (persists `kh-theme` in localStorage, follows the OS when unset).\n */\nexport function ThemeToggle({ className }: { className?: string }) {\n  const interactive = useInteractive();\n  const ref = useRef<HTMLButtonElement>(null);\n  const [dark, setDark] = useState(false);\n  useEffect(() => {\n    const root = ref.current?.closest<HTMLElement>('[data-kh-mode]');\n    if (!root) return;\n    const read = () => setDark(root.dataset.khMode === 'dark');\n    // A static page (no hydrated Theme) opens in the system mode: bring back the visitor's stored pick. A hydrated Theme already\n    // resolved it, so this changes nothing there.\n    const stored = readAppearance();\n    const theme = root.closest<HTMLElement>('[data-kh-theme]') ?? root;\n    if (stored !== 'system' && theme.dataset.khMode !== stored && !theme.hasAttribute('data-kh-mode-pinned')) {\n      theme.dataset.khMode = stored;\n      for (const k of Array.from(theme.style)) if (MODE_VARS.test(k)) theme.style.removeProperty(k);\n    }\n    read();\n    const mo = new MutationObserver(read);\n    mo.observe(root, { attributes: true, attributeFilter: ['data-kh-mode'] });\n    return () => mo.disconnect();\n  }, [interactive]); // hydrated, the first render is the static button (no ref): observe once the live one is there\n  const toggle = () => {\n    const btn = ref.current;\n    if (!btn) return;\n    const mode = dark ? 'light' : 'dark';\n    const proceed = btn.dispatchEvent(new CustomEvent('kh:theme-toggle', { bubbles: true, cancelable: true, detail: { mode } }));\n    const root = btn.closest<HTMLElement>('[data-kh-theme]');\n    if (proceed && root) {\n      // No hydrated Theme took the event (a static page with an island header): switch here and keep the pick for the next page.\n      setAppearance(mode);\n      root.dataset.khMode = mode;\n      for (const k of Array.from(root.style)) if (MODE_VARS.test(k)) root.style.removeProperty(k);\n    }\n  };\n  if (!interactive) {\n    return (\n      <button\n        type=\"button\"\n        data-kh-theme-toggle\n        aria-pressed=\"false\"\n        aria-label=\"Dark mode\"\n        className={cn('inline-flex size-11 shrink-0 items-center justify-center rounded-kh text-kh-ink-2 transition-colors hover:bg-kh-surface-2 hover:text-kh-ink', FOCUS, className)}\n      >\n        <Moon aria-hidden strokeWidth={1.75} className=\"kh-theme-icon-light size-5\" />\n        <Sun aria-hidden strokeWidth={1.75} className=\"kh-theme-icon-dark size-5\" />\n      </button>\n    );\n  }\n  return (\n    <button\n      ref={ref}\n      type=\"button\"\n      data-kh-theme-toggle\n      aria-pressed={dark}\n      aria-label=\"Dark mode\"\n      onClick={toggle}\n      className={cn('inline-flex size-11 shrink-0 items-center justify-center rounded-kh text-kh-ink-2 transition-colors hover:bg-kh-surface-2 hover:text-kh-ink', FOCUS, className)}\n    >\n      {dark ? <Sun aria-hidden strokeWidth={1.75} className=\"size-5\" /> : <Moon aria-hidden strokeWidth={1.75} className=\"size-5\" />}\n    </button>\n  );\n}\n\n/**\n * One-line email capture: a labelled input and a submit button. Never submits anywhere; the host wires `onSubmit` by\n * listening for the form event. `action` (the block's optional prop) becomes the form's action attribute for the host to read.\n */\nexport function EmailForm({ path, placeholder, submit, form = {}, className, size = 'md' }: {\n  path: PropPath; placeholder: string; submit: string; form?: FormSettings; className?: string; size?: 'md' | 'lg';\n}) {\n  const editing = useEditing();\n  const formProps = useFormProps(form);\n  const id = useId();\n  const named = useNamed(placeholder); // the placeholder is the field's name: without it the field is not drawn\n  if (!named) return null;\n  return (\n    <form {...formProps} className={cn('@container', className)}>\n      <label htmlFor={id} className={editing ? 'mb-1.5 block text-kh-caption text-kh-muted' : 'sr-only'}>\n        <Text path={[...path, 'placeholder']} value={placeholder} as=\"span\" />\n      </label>\n      <div className=\"flex flex-col gap-2 @sm:flex-row\">\n        <Input id={id} name=\"email\" type=\"email\" autoComplete=\"email\" required placeholder={placeholder} className=\"min-w-0 @sm:flex-1\" />\n        <Button type=\"submit\" size={size} className=\"@sm:shrink-0\">\n          <Text path={[...path, 'submit']} value={submit} as=\"span\" />\n        </Button>\n      </div>\n      <FormExtras {...form} />\n    </form>\n  );\n}\n"
    }
  ]
}
