{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "hex-stat",
  "title": "Hex stat",
  "description": "An attribute as a hexagon holding an icon, a value and a name, and a honeycomb to lay several out.",
  "dependencies": [
    "@ui-sdk/react@0.1.0-beta.5",
    "lucide-react@1.73.0"
  ],
  "registryDependencies": [
    "https://www.cyberspec.dev/r/cyber.json"
  ],
  "files": [
    {
      "path": "registry/cyber/ui/hex-stat.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { Button as SdkButton } from \"@ui-sdk/react/button\"\nimport { Plus } from \"lucide-react\"\nimport { burst } from \"@/registry/cyber/lib/glitch\"\nimport { sfx } from \"@/registry/cyber/lib/sound\"\nimport \"@/registry/cyber/ui/cyber/hex-stat.css\"\n\n// One attribute, as a node on a lattice: a flat-topped hexagon holding its\n// name, an icon and its value. It's a button, pressed while `selected`, and\n// a signal bar along its top edge marks the selected one.\n//\n// - `onRaise` adds a small \"+\" key on its lower edge, a separate button\n//   named \"Raise <name>\". Leave it out when there's nothing to spend; it\n//   also goes once `value` reaches `max`.\n// - When `value` changes, the numeral slips its channels for a moment.\n//\n// Everything else goes to the node's button (`onClick`, `disabled`, …).\nfunction HexStat({\n  name,\n  value,\n  icon,\n  selected = false,\n  max,\n  onRaise,\n  ...props\n}: Omit<React.ComponentProps<typeof SdkButton>, \"children\" | \"name\" | \"value\"> & {\n  name: string\n  value: number\n  icon?: React.ReactNode\n  selected?: boolean\n  max?: number\n  /** Return false if the increase is refused. */\n  onRaise?: () => boolean | void\n}) {\n  const numeral = React.useRef<HTMLSpanElement>(null)\n  const last = React.useRef(value)\n  React.useEffect(() => {\n    if (last.current === value) return\n    last.current = value\n    if (numeral.current) burst(numeral.current, 300)\n  }, [value])\n\n  const raisable = Boolean(onRaise) && (max == null || value < max) && !props.disabled\n\n  return (\n    <span data-kit=\"hex-stat\" data-selected={selected ? \"\" : undefined} data-raisable={raisable ? \"\" : undefined}>\n      <SdkButton data-kit=\"hex-stat-node\" aria-pressed={selected} {...props}>\n        <span data-kit=\"hex-stat-face\">\n          <span data-kit=\"hex-stat-name\">{name}</span>{\" \"}\n          <span data-kit=\"hex-stat-reading\">\n            {icon && (\n              <span data-kit=\"hex-stat-icon\" aria-hidden>\n                {icon}\n              </span>\n            )}\n            <span ref={numeral} data-kit=\"hex-stat-value\">\n              {value}\n            </span>\n            {max != null && <span data-sr-only> of {max}</span>}\n          </span>\n        </span>\n      </SdkButton>\n      {raisable && (\n        <SdkButton\n          data-kit=\"hex-stat-raise\"\n          data-frame=\"tl br\"\n          data-press-flash=\"\"\n          data-sfx=\"none\"\n          aria-label={`Raise ${name}`}\n          onClick={() => {\n            if (raisable && onRaise && onRaise() !== false) sfx(\"increment\")\n          }}\n        >\n          <Plus aria-hidden />\n        </SdkButton>\n      )}\n    </span>\n  )\n}\n\n// A flat-topped hexagon 100 wide is this tall.\nconst hexHeight = 86.6\n\ntype Point = { x: number; y: number }\n\n// Where each node sits. Columns step three quarters of a hexagon across\n// and odd columns ride half a step higher, which is what makes it a\n// honeycomb. A short last row spreads across the width instead of bunching\n// at the start, so five nodes in three columns leave the middle cell empty.\nfunction lattice(count: number, columns: number, gap: number) {\n  const dx = 75 + gap * 0.866\n  const dy = hexHeight + gap\n  const rows = Math.max(1, Math.ceil(count / columns))\n  const at = (column: number, row: number): Point => ({ x: column * dx, y: row * dy + (columns > 1 && column % 2 === 0 ? dy / 2 : 0) })\n  const taken = new Set<string>()\n  const nodes: Point[] = []\n  for (let index = 0; index < count; index++) {\n    const row = Math.floor(index / columns)\n    const inRow = row === rows - 1 ? count - row * columns : columns\n    let column = index - row * columns\n    if (inRow < columns) column = inRow === 1 ? Math.floor((columns - 1) / 2) : Math.round((column * (columns - 1)) / (inRow - 1))\n    taken.add(`${column}:${row}`)\n    nodes.push(at(column, row))\n  }\n  // A cell left empty becomes a hub, if nodes border it on most sides.\n  // With fewer it would be a junction hanging off the edge of the lattice.\n  const hubs: Point[] = []\n  for (let column = 0; column < columns; column++) {\n    if (taken.has(`${column}:${rows - 1}`)) continue\n    const hub = at(column, rows - 1)\n    if (nodes.filter((node) => Math.hypot(node.x - hub.x, node.y - hub.y) < dy * 1.05).length >= 4) hubs.push(hub)\n  }\n  const all = [...nodes, ...hubs]\n  // Whichever columns ride high, the lattice starts at its own top edge.\n  const top = all.length ? Math.min(...all.map((point) => point.y)) : 0\n  for (const point of all) point.y -= top\n  return {\n    nodes,\n    hubs,\n    reach: dy * 1.05,\n    width: Math.max(0, ...all.map((point) => point.x)) + 100,\n    height: Math.max(0, ...all.map((point) => point.y)) + hexHeight,\n  }\n}\n\n// How wide the lattice is in hexagon widths, without laying it out.\nconst span = (count: number, columns: number, gap: number) => ((Math.min(count, columns) - 1) * (75 + gap * 0.866) + 100) / 100\n\n// Three parallel traces from one center toward another, starting `from`\n// and stopping `to` units short of each. The outer two give up partway\n// and end in a pad, the way a bus thins out on a board.\nfunction traces(a: Point, b: Point, from: number, to: number) {\n  const length = Math.hypot(b.x - a.x, b.y - a.y)\n  const ux = (b.x - a.x) / length\n  const uy = (b.y - a.y) / length\n  const point = (along: number, across: number) => `${(a.x + ux * along - uy * across).toFixed(1)} ${(a.y + uy * along + ux * across).toFixed(1)}`\n  const end = length - to\n  const short = from + (end - from) * 0.62\n  const pad = (along: number, across: number) => `M${point(along - 1.4, across)}L${point(along + 1.4, across)}`\n  return {\n    lines: `M${point(from, 0)}L${point(end, 0)}M${point(from, -5)}L${point(short, -5)}M${point(from, 5)}L${point(short, 5)}`,\n    pads: `${pad(short, -5)}${pad(short, 5)}`,\n  }\n}\n\n// Several nodes in a honeycomb, joined by thin circuit traces: short buses\n// between neighbors, and a hub in any cell the layout leaves empty. The\n// traces that reach the selected node light in the signal color.\n//\n// It's a labelled group of buttons. It uses `columns` across while a\n// hexagon stays at least `minSize` px wide, and fewer when it wouldn't, so\n// a narrow column gets two tall columns rather than three cramped ones.\n//\n// Children are `HexStat`s; the group reads each one's `selected` to light\n// the traces.\nfunction HexStatGroup({\n  label,\n  columns = 3,\n  gap = 16,\n  minSize = 92,\n  children,\n  ...props\n}: React.ComponentProps<\"div\"> & {\n  label: string\n  columns?: number\n  gap?: number\n  minSize?: number\n}) {\n  const root = React.useRef<HTMLDivElement>(null)\n  const items = React.Children.toArray(children).filter(React.isValidElement)\n  const count = items.length\n  const [fit, setFit] = React.useState(columns)\n\n  React.useLayoutEffect(() => {\n    const node = root.current\n    if (!node) return\n    const measure = () => {\n      let next = Math.max(1, columns)\n      while (next > 1 && node.clientWidth / span(count, next, gap) < minSize) next--\n      setFit(next)\n    }\n    measure()\n    const observer = new ResizeObserver(measure)\n    observer.observe(node)\n    return () => observer.disconnect()\n  }, [columns, count, gap, minSize])\n\n  const { nodes, hubs, reach, width, height } = React.useMemo(() => lattice(count, Math.max(1, fit), gap), [count, fit, gap])\n  const center = (point: Point): Point => ({ x: point.x + 50, y: point.y + hexHeight / 2 })\n  const lit = items.map((item) => Boolean((item.props as { selected?: boolean }).selected))\n  const apothem = hexHeight / 2\n\n  const links: { key: string; lit: boolean; lines: string; pads: string }[] = []\n  nodes.forEach((a, i) => {\n    nodes.forEach((b, j) => {\n      if (j <= i || Math.hypot(b.x - a.x, b.y - a.y) > reach) return\n      links.push({ key: `${i}-${j}`, lit: lit[i] || lit[j], ...traces(center(a), center(b), apothem - 1, apothem - 1) })\n    })\n  })\n  hubs.forEach((hub, h) => {\n    nodes.forEach((node, i) => {\n      if (Math.hypot(node.x - hub.x, node.y - hub.y) > reach) return\n      links.push({ key: `h${h}-${i}`, lit: lit[i], ...traces(center(hub), center(node), 13, apothem - 1) })\n    })\n  })\n\n  return (\n    <div ref={root} data-kit=\"hex-stat-group\" role=\"group\" aria-label={label} {...props}>\n      <div data-kit=\"hex-stat-lattice\" style={{ \"--lattice-span\": width / 100, \"--lattice-ratio\": `${width} / ${height}` } as React.CSSProperties}>\n        <svg data-kit=\"hex-stat-traces\" viewBox={`0 0 ${width.toFixed(1)} ${height.toFixed(1)}`} preserveAspectRatio=\"none\" aria-hidden focusable=\"false\">\n          {links.map((link) => (\n            <g key={link.key} data-lit={link.lit ? \"\" : undefined}>\n              <path d={link.lines} />\n              <path d={link.pads} data-pad=\"\" />\n            </g>\n          ))}\n          {hubs.map((hub, h) => {\n            const c = center(hub)\n            return (\n              <path\n                key={h}\n                data-hub=\"\"\n                data-lit={lit.some(Boolean) ? \"\" : undefined}\n                d={`M${c.x} ${c.y - 10}L${c.x + 10} ${c.y}L${c.x} ${c.y + 10}L${c.x - 10} ${c.y}ZM${c.x} ${c.y - 4}L${c.x + 4} ${c.y}L${c.x} ${c.y + 4}L${c.x - 4} ${c.y}Z`}\n              />\n            )\n          })}\n        </svg>\n        {items.map((item, index) => (\n          <span\n            key={item.key ?? index}\n            data-kit=\"hex-stat-cell\"\n            style={\n              {\n                \"--cell-x\": `${(nodes[index].x / width) * 100}%`,\n                \"--cell-y\": `${(nodes[index].y / height) * 100}%`,\n                \"--cell-w\": `${(100 / width) * 100}%`,\n              } as React.CSSProperties\n            }\n          >\n            {item}\n          </span>\n        ))}\n      </div>\n    </div>\n  )\n}\n\nexport { HexStat, HexStatGroup }\n",
      "type": "registry:ui",
      "target": "@ui/hex-stat.tsx"
    },
    {
      "path": "registry/cyber/ui/cyber/hex-stat.css",
      "content": "@layer ui-sdk, cyber.tokens, cyber.base, cyber.ui, cyber.blocks, cyber.fx, cyber.a11y;\n\n@layer cyber.ui {\n  /* A hexagon is clipped, and a clipped element can't show a border, so the\n     node is two hexagons: the button, filled with the edge color, and a\n     face inset inside it, filled with the panel. The state language moves\n     --hex-edge and --hex-fill the way a frame moves its line and fill.\n\n     The wrapper is a size container, so the type inside scales with the\n     hexagon when a lattice shrinks it. */\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat\"] {\n    --hex-edge: var(--cy-line-ctl);\n    --hex-edge-w: 1px;\n    --hex-fill: var(--cy-panel);\n    --hex-ink: var(--cy-ink);\n    --hex-shape: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);\n\n    position: relative;\n    flex: none;\n    display: inline-block;\n    width: var(--hex-stat-size, 112px);\n    aspect-ratio: 1 / 0.866;\n    container-type: inline-size;\n    vertical-align: middle;\n\n    @media (hover: hover) {\n      &:has([data-kit=\"hex-stat-node\"]:hover:not(:disabled, [data-disabled])) {\n        --hex-edge: var(--cy-line-hot);\n        --hex-fill: var(--cy-fill-hover);\n      }\n    }\n\n    &[data-selected],\n    &:has([data-kit=\"hex-stat-node\"]:active:not(:disabled, [data-disabled])) {\n      --hex-edge: var(--cy-line-hot);\n      --hex-fill: var(--cy-fill-active);\n      --hex-ink: var(--cy-ink-hi);\n    }\n\n    /* The clip would cut an outline off, so focus thickens the edge and\n       turns it the signal color instead. */\n    &:has([data-kit=\"hex-stat-node\"]:focus-visible) {\n      --hex-edge: var(--cy-signal);\n      --hex-edge-w: 2px;\n    }\n\n    &:has([data-kit=\"hex-stat-node\"]:is(:disabled, [data-disabled])) {\n      --hex-edge: var(--cy-line-deco);\n      --hex-fill: var(--cy-panel);\n      --hex-ink: var(--cy-ink-mute);\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-node\"] {\n    position: absolute;\n    inset: 0;\n    display: block;\n    width: 100%;\n    height: 100%;\n    margin: 0;\n    padding: 0;\n    border: 0;\n    background: var(--hex-edge);\n    clip-path: var(--hex-shape);\n    color: var(--hex-ink);\n    cursor: pointer;\n    user-select: none;\n    outline: none;\n    transition: background-color var(--cy-t-fast) linear;\n\n    &:is(:disabled, [data-disabled]) {\n      cursor: not-allowed;\n    }\n\n    /* A press: the face jumps to the flash fill and bleeds back. */\n    &:active:not(:disabled, [data-disabled]) > [data-kit=\"hex-stat-face\"] {\n      animation: var(--cy-fx-flash) 220ms var(--cy-ease-snap);\n    }\n\n    &:is(:disabled, [data-disabled]) > [data-kit=\"hex-stat-face\"] {\n      background-image: repeating-linear-gradient(135deg, transparent 0 5px, var(--cy-line-deco) 5px 6px);\n    }\n  }\n\n  /* The slanted sides are steeper than they are wide, so the face comes in\n     a little further across than down to keep the edge even all round. */\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-face\"] {\n    position: absolute;\n    inset: var(--hex-edge-w) calc(var(--hex-edge-w) * 1.155);\n    display: grid;\n    align-content: center;\n    justify-items: center;\n    gap: 1cqi;\n    padding-block-end: 2cqi;\n    background-color: var(--hex-fill);\n    clip-path: var(--hex-shape);\n    transition: background-color var(--cy-t-fast) linear;\n\n    /* Selected: a signal bar along the top edge, so it isn't the fill\n       alone that says so. */\n    [data-selected] > [data-kit=\"hex-stat-node\"] > &::before {\n      content: \"\";\n      position: absolute;\n      inset-block-start: 0;\n      inset-inline: 25%;\n      height: 3px;\n      background: var(--cy-signal);\n    }\n  }\n\n  /* Room for the \"+\" key, which sits across the lower edge. */\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat\"][data-raisable] [data-kit=\"hex-stat-face\"] {\n    padding-block-end: 9cqi;\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-name\"] {\n    max-width: 68cqi;\n    overflow: hidden;\n    text-overflow: ellipsis;\n    white-space: nowrap;\n    font-size: clamp(var(--cy-text-xs), 11cqi, var(--cy-text-sm));\n    font-weight: 600;\n    line-height: 1.1;\n    letter-spacing: var(--cy-track);\n    text-transform: uppercase;\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-reading\"] {\n    display: inline-flex;\n    align-items: center;\n    gap: 5cqi;\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-icon\"] {\n    display: grid;\n    place-items: center;\n\n    & svg {\n      width: clamp(14px, 16cqi, 20px);\n      height: clamp(14px, 16cqi, 20px);\n    }\n  }\n\n  /* The value is always the signal color, whatever the node's state. */\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-value\"] {\n    font-family: var(--cy-mono);\n    font-size: clamp(var(--cy-text-lg), 22cqi, 28px);\n    font-weight: 600;\n    font-variant-numeric: tabular-nums;\n    line-height: 1;\n    letter-spacing: -0.03em;\n    color: var(--cy-signal);\n\n    &[data-glitching] {\n      animation: var(--cy-fx-rgb) 300ms var(--cy-ease-snap);\n    }\n\n    :is(:disabled, [data-disabled]) & {\n      color: var(--cy-ink-mute);\n    }\n  }\n\n  /* The \"+\" key: a small cut frame in the signal color, half on the node\n     and half off it. A frame, so its focus ring is drawn inside. */\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-raise\"] {\n    --frame-cut: 5px;\n    --frame-line: var(--cy-signal-dim);\n    --frame-fill: var(--cy-panel);\n    --frame-focus: var(--cy-ink-hi);\n\n    position: absolute;\n    inset-block-end: -11px;\n    left: 50%;\n    translate: -50% 0;\n    display: grid;\n    place-items: center;\n    width: 30px;\n    height: 24px;\n    padding: 0;\n    color: var(--cy-signal);\n    cursor: pointer;\n    transition:\n      background-color var(--cy-t-fast) linear,\n      border-color var(--cy-t-fast) linear;\n\n    & svg {\n      width: 14px;\n      height: 14px;\n    }\n\n    @media (pointer: coarse) {\n      inset-block-end: -15px;\n      width: 44px;\n      height: 32px;\n    }\n\n    @media (hover: hover) {\n      &:hover {\n        --frame-line: var(--cy-signal);\n        --frame-fill: var(--cy-signal-fill);\n      }\n    }\n\n    &:active {\n      --frame-line: var(--cy-signal);\n      --frame-fill: color-mix(in srgb, var(--cy-signal) 30%, var(--cy-panel));\n      color: var(--cy-ink-hi);\n    }\n  }\n\n  /* ── The lattice ───────────────────────────────────────────────────── */\n\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-group\"] {\n    width: 100%;\n    min-width: 0;\n    /* The \"+\" keys of the last row hang below the lattice. */\n    padding-block-end: 12px;\n  }\n\n  /* Laid out in units of one hexagon width by the component, which hands\n     over how many it spans and its proportions. Everything inside is\n     placed in percentages, so it scales as one piece. */\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-lattice\"] {\n    position: relative;\n    width: 100%;\n    max-width: calc(var(--lattice-span) * var(--hex-stat-size, 124px));\n    aspect-ratio: var(--lattice-ratio);\n    margin-inline: auto;\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-cell\"] {\n    position: absolute;\n    inset-block-start: var(--cell-y);\n    inset-inline-start: var(--cell-x);\n    width: var(--cell-w);\n\n    & > [data-kit=\"hex-stat\"] {\n      display: block;\n      width: 100%;\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"hex-stat-traces\"] {\n    position: absolute;\n    inset: 0;\n    width: 100%;\n    height: 100%;\n    overflow: visible;\n    pointer-events: none;\n\n    &:dir(rtl) {\n      scale: -1 1;\n    }\n\n    & path {\n      fill: none;\n      stroke: var(--cy-line-deco);\n      stroke-width: 1px;\n      vector-effect: non-scaling-stroke;\n      transition: stroke var(--cy-t) linear;\n    }\n\n    & [data-pad] {\n      stroke-width: 3px;\n    }\n\n    & [data-hub] {\n      stroke: var(--cy-line-ctl);\n    }\n\n    /* What reaches the selected node carries its signal. */\n    & [data-lit] path,\n    & [data-hub][data-lit] {\n      stroke: var(--cy-signal);\n    }\n  }\n\n  @media (forced-colors: active) {\n    [data-skin=\"cyber\"] [data-kit=\"hex-stat-node\"] {\n      background: CanvasText;\n    }\n    [data-skin=\"cyber\"] [data-kit=\"hex-stat\"][data-selected] [data-kit=\"hex-stat-node\"] {\n      background: Highlight;\n    }\n    [data-skin=\"cyber\"] [data-kit=\"hex-stat-face\"] {\n      background-color: Canvas;\n    }\n  }\n}\n",
      "type": "registry:file",
      "target": "@ui/cyber/hex-stat.css"
    }
  ],
  "type": "registry:ui"
}
