{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "key-hint",
  "title": "Key hint",
  "description": "An input hint: a key glyph and what it does, optionally pressable.",
  "dependencies": [
    "@ui-sdk/react@0.1.0-beta.5"
  ],
  "registryDependencies": [
    "https://www.cyberspec.dev/r/cyber.json"
  ],
  "files": [
    {
      "path": "registry/cyber/ui/key-hint.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { Button as SdkButton } from \"@ui-sdk/react/button\"\nimport { sfx } from \"@/registry/cyber/lib/sound\"\nimport \"@/registry/cyber/ui/cyber/key-hint.css\"\n\n// Glyphs a screen reader would skip or misread, and what to say instead.\nconst spoken: Record<string, string> = {\n  \"↑\": \"Up\",\n  \"↓\": \"Down\",\n  \"←\": \"Left\",\n  \"→\": \"Right\",\n  \"↵\": \"Enter\",\n  \"⏎\": \"Enter\",\n  \"⎋\": \"Escape\",\n  \"␣\": \"Space\",\n  \"⇥\": \"Tab\",\n  \"⇧\": \"Shift\",\n  \"⌫\": \"Backspace\",\n  \"⌘\": \"Command\",\n  \"⌥\": \"Option\",\n  \"⌃\": \"Control\",\n}\n\n// What a printed key is called by a keyboard event, where the two differ.\nconst eventKeys: Record<string, string> = {\n  up: \"arrowup\",\n  down: \"arrowdown\",\n  left: \"arrowleft\",\n  right: \"arrowright\",\n  esc: \"escape\",\n  return: \"enter\",\n  space: \" \",\n  del: \"delete\",\n  ctrl: \"control\",\n  cmd: \"meta\",\n  command: \"meta\",\n  opt: \"alt\",\n  option: \"alt\",\n}\n\nfunction eventKeyOf(glyph: string) {\n  const name = (spoken[glyph] ?? glyph).toLowerCase()\n  return eventKeys[name] ?? name\n}\n\n// One pair of listeners for every hint on the page. A key going down while\n// someone is typing, or as part of a shortcut, isn't a hint's business.\ntype Watcher = (key: string | null, down: boolean) => void\nconst watchers = new Set<Watcher>()\nconst typing = 'input, textarea, select, [contenteditable=\"\"], [contenteditable=\"true\"]'\n\nfunction onKey(event: KeyboardEvent) {\n  const down = event.type === \"keydown\"\n  if (down && (event.metaKey || event.ctrlKey || event.altKey || (event.target as Element | null)?.closest?.(typing))) return\n  const key = event.key.toLowerCase()\n  watchers.forEach((watcher) => watcher(key, down))\n}\n\n// A key released while the window is elsewhere never reports it.\nconst onBlur = () => watchers.forEach((watcher) => watcher(null, false))\n\nfunction watchKeys(watcher: Watcher) {\n  if (!watchers.size) {\n    document.addEventListener(\"keydown\", onKey)\n    document.addEventListener(\"keyup\", onKey)\n    window.addEventListener(\"blur\", onBlur)\n  }\n  watchers.add(watcher)\n  return () => {\n    watchers.delete(watcher)\n    if (watchers.size) return\n    document.removeEventListener(\"keydown\", onKey)\n    document.removeEventListener(\"keyup\", onKey)\n    window.removeEventListener(\"blur\", onBlur)\n  }\n}\n\n// Which of these printed keys are being held on the keyboard right now.\nfunction useHeld(glyphs: string[]) {\n  const [held, setHeld] = React.useState<readonly string[]>([])\n  const wanted = glyphs.map(eventKeyOf).join(\"\\n\")\n  React.useEffect(() => {\n    const mine = new Set(wanted.split(\"\\n\"))\n    return watchKeys((key, down) => {\n      if (key === null) return setHeld((now) => (now.length ? [] : now))\n      if (!mine.has(key)) return\n      setHeld((now) => (down ? (now.includes(key) ? now : [...now, key]) : now.filter((other) => other !== key)))\n    })\n  }, [wanted])\n  return held\n}\n\ntype KeyHintSpec = {\n  /** The key, or keys, as printed: \"X\", \"Esc\", [\"↑\", \"↓\"]. */\n  keys: string | string[]\n  /** What it does: \"Answer\". */\n  label: string\n  /** Makes the hint a button that does the same thing, for a pointer or a finger. */\n  onPress?: () => boolean | void\n  /** Directional feedback for a hint that changes a value. */\n  action?: \"increment\" | \"decrement\"\n  disabled?: boolean\n}\n\n// The hint at the foot of a screen: a key glyph, then what the key does.\n//\n// - Each key is a <kbd> in a hexagon. Arrows and other symbols are read by\n//   name (\"Up\", \"Enter\").\n// - With `onPress` it's a real button, so the same action is there for a\n//   pointer or a finger. Without it, it's text.\n// - The glyph lights while its key is held on the keyboard, so the screen\n//   answers the press. The hint only shows the key: binding it to the\n//   action is the screen's job.\nfunction KeyHint({ keys, label, onPress, action, disabled, ...props }: KeyHintSpec & Omit<React.ComponentProps<\"span\">, \"children\">) {\n  const glyphs = Array.isArray(keys) ? keys : [keys]\n  const held = useHeld(glyphs)\n  const content = (\n    <>\n      <span data-kit=\"key-hint-keys\">\n        {glyphs.map((glyph) => (\n          <kbd key={glyph} data-kit=\"key-hint-key\" data-held={held.includes(eventKeyOf(glyph)) ? \"\" : undefined}>\n            {glyph in spoken ? (\n              <>\n                <span aria-hidden>{glyph}</span>\n                <span data-sr-only>{spoken[glyph]}</span>\n              </>\n            ) : (\n              glyph\n            )}\n          </kbd>\n        ))}\n      </span>\n      <span data-kit=\"key-hint-label\">{label}</span>\n    </>\n  )\n\n  if (!onPress)\n    return (\n      <span data-kit=\"key-hint\" {...props}>\n        {content}\n      </span>\n    )\n  return (\n    <SdkButton\n      data-kit=\"key-hint\"\n      data-sfx={action ? \"none\" : undefined}\n      disabled={disabled}\n      onClick={() => {\n        if (!disabled && onPress() !== false && action) sfx(action)\n      }}\n      {...(props as React.ComponentProps<typeof SdkButton>)}\n    >\n      {content}\n    </SdkButton>\n  )\n}\n\n// A row of hints, as a list: what the keys do on this screen. It sits at\n// the trailing end of whatever it's in, the way the foot of a screen has\n// it; `align` moves it.\nfunction KeyHints({\n  label = \"Controls\",\n  hints,\n  align = \"end\",\n  ...props\n}: Omit<React.ComponentProps<\"ul\">, \"children\"> & {\n  label?: string\n  hints: KeyHintSpec[]\n  align?: \"start\" | \"center\" | \"end\"\n}) {\n  return (\n    <ul data-kit=\"key-hints\" data-align={align} aria-label={label} {...props}>\n      {hints.map((hint) => (\n        <li key={hint.label}>\n          <KeyHint {...hint} />\n        </li>\n      ))}\n    </ul>\n  )\n}\n\nexport { KeyHint, KeyHints }\nexport type { KeyHintSpec }\n",
      "type": "registry:ui",
      "target": "@ui/key-hint.tsx"
    },
    {
      "path": "registry/cyber/ui/cyber/key-hint.css",
      "content": "@layer ui-sdk, cyber.tokens, cyber.base, cyber.ui, cyber.blocks, cyber.fx, cyber.a11y;\n\n@layer cyber.ui {\n  /* A key glyph, then what the key does. The glyph is in the signal color,\n     like every other mark that points at input; the word is a label. */\n  [data-skin=\"cyber\"] [data-kit=\"key-hint\"] {\n    display: inline-flex;\n    align-items: center;\n    gap: 7px;\n    min-height: 24px;\n    padding: 0;\n    border: 0;\n    background: none;\n    font-size: var(--cy-text-sm);\n    font-weight: 600;\n    line-height: 1;\n    letter-spacing: var(--cy-track);\n    text-transform: uppercase;\n    white-space: nowrap;\n    color: var(--cy-ink);\n    user-select: none;\n  }\n\n  /* As a button: the word brightens and the glyph takes a wash under the\n     pointer, and goes solid as it's pressed. It isn't a frame, so focus is\n     the kit's plain ring around the whole hint. */\n  [data-skin=\"cyber\"] button[data-kit=\"key-hint\"] {\n    cursor: pointer;\n    transition: color var(--cy-t-fast) linear;\n\n    @media (pointer: coarse) {\n      min-height: 44px;\n    }\n\n    @media (hover: hover) {\n      &:hover:not([data-disabled]) {\n        color: var(--cy-ink-hi);\n\n        & [data-kit=\"key-hint-key\"] {\n          --key-fill: var(--cy-signal-fill);\n        }\n      }\n    }\n\n    &:active:not([data-disabled]) {\n      color: var(--cy-ink-hi);\n\n      & [data-kit=\"key-hint-key\"] {\n        --key-fill: var(--cy-signal);\n        color: var(--cy-on-solid);\n      }\n    }\n\n    /* Unavailable: the glyph is hatched and both lose their color. */\n    &[data-disabled] {\n      color: var(--cy-ink-mute);\n      cursor: not-allowed;\n\n      & [data-kit=\"key-hint-key\"] {\n        --key-line: var(--cy-line-ctl);\n        --key-fill: var(--cy-panel);\n        --key-image: repeating-linear-gradient(135deg, transparent 0 3px, var(--cy-line-deco) 3px 4px);\n        color: var(--cy-ink-mute);\n      }\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"key-hint-keys\"] {\n    display: inline-flex;\n    gap: 3px;\n  }\n\n  /* The key: a hexagon, stretched to fit a longer name. Nothing in the kit\n     is round, so where another interface would draw a circled letter this\n     draws six sides. The outline is the element's own fill, clipped to the\n     shape, with the inside laid over it one pixel in. */\n  [data-skin=\"cyber\"] [data-kit=\"key-hint-key\"] {\n    --key-line: var(--cy-signal);\n    --key-fill: var(--cy-panel);\n    --key-image: none;\n    --key-point: 6px;\n    --key-shape: polygon(\n      var(--key-point) 0,\n      calc(100% - var(--key-point)) 0,\n      100% 50%,\n      calc(100% - var(--key-point)) 100%,\n      var(--key-point) 100%,\n      0 50%\n    );\n\n    position: relative;\n    isolation: isolate;\n    display: inline-grid;\n    place-items: center;\n    min-width: 22px;\n    height: 20px;\n    padding-inline: 7px;\n    font-family: var(--cy-mono);\n    font-size: var(--cy-text-xs);\n    font-weight: 600;\n    line-height: 1;\n    letter-spacing: 0;\n    color: var(--cy-signal);\n    background: var(--key-line);\n    clip-path: var(--key-shape);\n    transition: color var(--cy-t-fast) linear;\n\n    &::before {\n      content: \"\";\n      position: absolute;\n      inset: 1px;\n      z-index: -1;\n      background: var(--key-image), var(--key-fill);\n      clip-path: var(--key-shape);\n      transition: background-color var(--cy-t-fast) linear;\n    }\n\n    /* Held on the keyboard: solid, for as long as the key is down. */\n    &[data-held] {\n      --key-fill: var(--cy-signal);\n      color: var(--cy-on-solid);\n    }\n\n    /* The system's colors replace the fills that draw the hexagon, so the\n       key falls back to a plain box with a real border. */\n    @media (forced-colors: active) {\n      border: 1px solid CanvasText;\n      clip-path: none;\n\n      &::before {\n        display: none;\n      }\n\n      &[data-held] {\n        border-color: Highlight;\n      }\n    }\n  }\n\n  /* A row of hints. */\n  [data-skin=\"cyber\"] [data-kit=\"key-hints\"] {\n    display: flex;\n    flex-wrap: wrap;\n    align-items: center;\n    justify-content: flex-end;\n    gap: 4px 18px;\n    min-width: 0;\n    margin: 0;\n    padding: 0;\n    list-style: none;\n\n    &[data-align=\"start\"] {\n      justify-content: flex-start;\n    }\n    &[data-align=\"center\"] {\n      justify-content: center;\n    }\n\n    & > li {\n      display: flex;\n    }\n  }\n}\n",
      "type": "registry:file",
      "target": "@ui/cyber/key-hint.css"
    }
  ],
  "type": "registry:ui"
}
