{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "branching-logic-explorer",
  "title": "Branching Logic Explorer",
  "description": "A rule map you can walk: pick any step to light the path to it, see what share of cases goes each way, and read the outcome. On phones it asks one question at a time.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/branching-logic-explorer/branching-logic-explorer.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type BranchTone = \"positive\" | \"warning\" | \"critical\" | \"neutral\";\n\nexport interface BranchNode {\n  id: string;\n  label: string;\n  /** The answer that leads here, e.g. \"Yes\". */\n  answer?: string;\n  /** Share of all cases that reach this node, from 0 to 100. */\n  share?: number;\n  /** Colours the dot on an outcome. */\n  tone?: BranchTone;\n  /** One line about an outcome. */\n  note?: string;\n  /** Leave empty on an outcome. */\n  children?: BranchNode[];\n}\n\nexport interface BranchingLogicExplorerProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  tree?: BranchNode;\n  /** The id of the node that starts selected. */\n  defaultSelected?: string;\n  /** Fires when an outcome is reached. */\n  onOutcome?: (node: BranchNode) => void;\n}\n\nconst FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\nconst NODE_H = 52;\nconst PITCH = 62;\nconst ACCENT = \"#5E6AD2\";\nconst WIRE = 1.5;\n\nconst PALETTES = {\n  dark: {\n    card: \"#0F1011\",\n    node: \"#161718\",\n    border: \"rgba(255,255,255,0.08)\",\n    wire: \"rgba(255,255,255,0.1)\",\n    wireHover: \"rgba(255,255,255,0.28)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    chip: \"rgba(255,255,255,0.05)\",\n    picked: \"rgba(94,106,210,0.14)\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    node: \"#F7F8F8\",\n    border: \"rgba(8,9,10,0.1)\",\n    wire: \"rgba(8,9,10,0.12)\",\n    wireHover: \"rgba(8,9,10,0.32)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    chip: \"rgba(8,9,10,0.05)\",\n    picked: \"rgba(94,106,210,0.1)\",\n  },\n};\n\nconst TONES: Record<BranchTone, string> = {\n  positive: \"#27A644\",\n  warning: \"#FC7840\",\n  critical: \"#EB5757\",\n  neutral: \"#8A8F98\",\n};\n\nconst DEFAULT_TREE: BranchNode = {\n  id: \"age\",\n  label: \"Order older than 30 days?\",\n  share: 100,\n  children: [\n    {\n      id: \"shipped\",\n      answer: \"No\",\n      label: \"Has it shipped?\",\n      share: 72,\n      children: [\n        {\n          id: \"cancel\",\n          answer: \"No\",\n          label: \"Cancel and refund\",\n          share: 28,\n          tone: \"positive\",\n          note: \"Stopped at the warehouse and refunded in a minute.\",\n        },\n        {\n          id: \"value\",\n          answer: \"Yes\",\n          label: \"Order under $100?\",\n          share: 44,\n          children: [\n            {\n              id: \"instant\",\n              answer: \"Yes\",\n              label: \"Instant refund\",\n              share: 31,\n              tone: \"positive\",\n              note: \"Refunded straight away, no return needed.\",\n            },\n            {\n              id: \"agent\",\n              answer: \"No\",\n              label: \"Send to an agent\",\n              share: 13,\n              tone: \"warning\",\n              note: \"Goes to the support queue with the order attached.\",\n            },\n          ],\n        },\n      ],\n    },\n    {\n      id: \"plan\",\n      answer: \"Yes\",\n      label: \"On a paid plan?\",\n      share: 28,\n      children: [\n        {\n          id: \"history\",\n          answer: \"Yes\",\n          label: \"Over 2 refunds this year?\",\n          share: 11,\n          children: [\n            {\n              id: \"credit\",\n              answer: \"No\",\n              label: \"Offer store credit\",\n              share: 8,\n              tone: \"neutral\",\n              note: \"Credit lands on the account for the next order.\",\n            },\n            {\n              id: \"fraud\",\n              answer: \"Yes\",\n              label: \"Flag for review\",\n              share: 3,\n              tone: \"critical\",\n              note: \"Held until the trust team has looked at it.\",\n            },\n          ],\n        },\n        {\n          id: \"decline\",\n          answer: \"No\",\n          label: \"Decline with a reason\",\n          share: 17,\n          tone: \"neutral\",\n          note: \"Sends the policy and a link to contact support.\",\n        },\n      ],\n    },\n  ],\n};\n\n/** Drawn as SVG, because a tiny border-radius circle turns lumpy on 1x screens and fractional positions. */\nfunction Dot({ size, color }: { size: number; color: string }) {\n  const r = size / 2;\n  return (\n    <svg\n      aria-hidden=\"true\"\n      width={size}\n      height={size}\n      viewBox={`0 0 ${size} ${size}`}\n      className=\"block shrink-0\"\n    >\n      <circle\n        cx={r}\n        cy={r}\n        r={r}\n        className=\"motion-safe:transition-[fill] motion-safe:duration-200\"\n        style={{ fill: color }}\n      />\n    </svg>\n  );\n}\n\ninterface Placed {\n  node: BranchNode;\n  parent: string | null;\n  depth: number;\n  col: number;\n  row: number;\n  path: string[];\n}\n\n/** Outcomes share the last column, and every question sits level with the middle of its answers. */\nfunction placeTree(root: BranchNode) {\n  const placed: Placed[] = [];\n  const depthOf = (n: BranchNode): number =>\n    n.children?.length ? 1 + Math.max(...n.children.map(depthOf)) : 0;\n  const last = depthOf(root);\n  let nextRow = 0;\n  const walk = (\n    node: BranchNode,\n    depth: number,\n    parent: string | null,\n    path: string[],\n  ): number => {\n    const own = [...path, node.id];\n    const entry: Placed = { node, parent, depth, col: depth, row: 0, path: own };\n    placed.push(entry);\n    if (!node.children?.length) {\n      entry.col = last;\n      entry.row = nextRow;\n      nextRow += 1;\n      return entry.row;\n    }\n    const rows = node.children.map((c) => walk(c, depth + 1, node.id, own));\n    entry.row = (rows[0] + rows[rows.length - 1]) / 2;\n    return entry.row;\n  };\n  walk(root, 0, null, []);\n  return { placed, columns: last + 1, rows: nextRow };\n}\n\n/** Keeps the map at its real pixel width, so the wires never stretch with the card. */\nfunction useWidth<T extends HTMLElement>(fallback: number) {\n  const ref = React.useRef<T>(null);\n  const [width, setWidth] = React.useState(fallback);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) => {\n      setWidth(Math.round(entry.contentRect.width));\n    });\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n  return [ref, width] as const;\n}\n\nexport function BranchingLogicExplorer({\n  theme = \"dark\",\n  title = \"Refund routing\",\n  subtitle = \"Follow a request through the rules to see where it ends up.\",\n  tree = DEFAULT_TREE,\n  defaultSelected = \"instant\",\n  onOutcome,\n  className,\n  style,\n  ...rest\n}: BranchingLogicExplorerProps) {\n  const c = PALETTES[theme];\n  const { placed, columns, rows } = React.useMemo(() => placeTree(tree), [tree]);\n  const byId = React.useMemo(\n    () => new Map(placed.map((p) => [p.node.id, p])),\n    [placed],\n  );\n  const [selected, setSelected] = React.useState(\n    byId.has(defaultSelected) ? defaultSelected : tree.id,\n  );\n  const [hovered, setHovered] = React.useState<string | null>(null);\n  const [mapRef, width] = useWidth<HTMLDivElement>(860);\n  const buttons = React.useRef(new Map<string, HTMLButtonElement>());\n\n  const current = byId.get(selected) ?? placed[0];\n  const path = current.path;\n  const preview = hovered ? (byId.get(hovered)?.path ?? []) : [];\n  const isOutcome = !current.node.children?.length;\n  const tone = TONES[current.node.tone ?? \"neutral\"];\n\n  const nodeW = Math.min(196, Math.floor((width - (columns - 1) * 44) / columns));\n  const step = columns > 1 ? (width - nodeW) / (columns - 1) : 0;\n  const xOf = (p: Placed) => Math.round(p.col * step);\n  const yOf = (p: Placed) => Math.round(p.row * PITCH);\n  const mapH = (rows - 1) * PITCH + NODE_H;\n\n  const pick = (id: string) => {\n    setSelected(id);\n    const next = byId.get(id);\n    if (next && !next.node.children?.length) onOutcome?.(next.node);\n  };\n\n  // Arrow keys walk the map the way it looks: left to the question, right to its first answer.\n  const onKeyDown = (e: React.KeyboardEvent, p: Placed) => {\n    const siblings = p.parent ? (byId.get(p.parent)?.node.children ?? []) : [];\n    const at = siblings.findIndex((s) => s.id === p.node.id);\n    const target =\n      e.key === \"ArrowLeft\"\n        ? p.parent\n        : e.key === \"ArrowRight\"\n          ? p.node.children?.[0]?.id\n          : e.key === \"ArrowDown\"\n            ? siblings[at + 1]?.id\n            : e.key === \"ArrowUp\"\n              ? siblings[at - 1]?.id\n              : undefined;\n    if (!target) return;\n    e.preventDefault();\n    buttons.current.get(target)?.focus();\n  };\n\n  return (\n    <section\n      aria-label={title}\n      className={`@container w-full rounded-2xl border ${className ?? \"\"}`}\n      style={{\n        background: c.card,\n        borderColor: c.border,\n        color: c.text,\n        fontFamily: FONT,\n        ...style,\n      }}\n      {...rest}\n    >\n      <header className=\"flex items-start justify-between gap-4 p-5 pb-4 @2xl:p-6 @2xl:pb-5\">\n        <div className=\"min-w-0\">\n          <h3 className=\"m-0 text-[20px] leading-[26px] font-[590] tracking-[-0.018em]\">\n            {title}\n          </h3>\n          <p\n            className=\"m-0 mt-1 text-[13px] leading-[20px] tracking-[-0.011em]\"\n            style={{ color: c.sub }}\n          >\n            {subtitle}\n          </p>\n        </div>\n        <button\n          type=\"button\"\n          onClick={() => setSelected(tree.id)}\n          disabled={selected === tree.id}\n          className=\"h-8 shrink-0 cursor-pointer rounded-full border-0 px-3.5 text-[13px] font-[510] tracking-[-0.011em] outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2] disabled:cursor-default\"\n          style={{\n            background: c.chip,\n            color: selected === tree.id ? c.faint : c.soft,\n          }}\n        >\n          Start over\n        </button>\n      </header>\n\n      {/* Wide cards get the whole map. */}\n      <div className=\"hidden px-6 pb-5 @2xl:block\">\n        <div ref={mapRef} className=\"relative w-full\" style={{ height: mapH }}>\n          <svg\n            aria-hidden=\"true\"\n            width={width}\n            height={mapH}\n            className=\"absolute inset-0 block\"\n            fill=\"none\"\n          >\n            {placed.map((p) => {\n              if (!p.parent) return null;\n              const from = byId.get(p.parent);\n              if (!from) return null;\n              const x1 = xOf(from) + nodeW;\n              const y1 = yOf(from) + NODE_H / 2;\n              const x2 = xOf(p);\n              const y2 = yOf(p) + NODE_H / 2;\n              const mid = x1 + Math.min(44, (x2 - x1) / 2);\n              const d = `M${x1} ${y1} C${mid} ${y1} ${mid} ${y2} ${Math.min(x1 + 88, x2)} ${y2} L${x2} ${y2}`;\n              const lit = path.includes(p.node.id);\n              return (\n                <g key={p.node.id}>\n                  <path\n                    d={d}\n                    strokeWidth={WIRE}\n                    stroke={preview.includes(p.node.id) ? c.wireHover : c.wire}\n                    className=\"motion-safe:transition-[stroke] motion-safe:duration-150\"\n                  />\n                  <path\n                    d={d}\n                    pathLength={1}\n                    stroke={ACCENT}\n                    strokeWidth={WIRE}\n                    strokeDasharray=\"1 1\"\n                    strokeDashoffset={lit ? 0 : 1}\n                    className=\"motion-safe:transition-[stroke-dashoffset]\"\n                    style={{\n                      transitionDuration: lit ? \"260ms\" : \"140ms\",\n                      transitionDelay: lit ? `${(p.depth - 1) * 170}ms` : \"0ms\",\n                    }}\n                  />\n                </g>\n              );\n            })}\n          </svg>\n          {placed.map((p) => {\n            const on = path.includes(p.node.id);\n            const end = selected === p.node.id;\n            const outcome = !p.node.children?.length;\n            return (\n              <button\n                key={p.node.id}\n                ref={(el) => {\n                  if (el) buttons.current.set(p.node.id, el);\n                  else buttons.current.delete(p.node.id);\n                }}\n                type=\"button\"\n                data-node={p.node.id}\n                aria-pressed={on}\n                onClick={() => pick(p.node.id)}\n                onKeyDown={(e) => onKeyDown(e, p)}\n                onPointerEnter={(e) => {\n                  if (e.pointerType === \"mouse\") setHovered(p.node.id);\n                }}\n                onPointerLeave={() => setHovered(null)}\n                className=\"absolute flex cursor-pointer flex-col justify-center rounded-lg border px-3 text-left outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2] motion-safe:transition-colors motion-safe:duration-200\"\n                style={{\n                  left: xOf(p),\n                  top: yOf(p),\n                  width: nodeW,\n                  height: NODE_H,\n                  background: end ? c.picked : c.node,\n                  borderColor: on ? ACCENT : c.border,\n                }}\n              >\n                <span\n                  className=\"flex items-center justify-between gap-2 text-[11px] leading-[14px] tracking-[-0.005em] tabular-nums\"\n                  style={{ color: c.faint }}\n                >\n                  <span>{p.node.answer ?? \"Start\"}</span>\n                  <span>{p.node.share ?? 0}%</span>\n                </span>\n                <span\n                  className=\"mt-0.5 flex min-w-0 items-center gap-1.5 text-[12.5px] leading-[18px] font-[510] tracking-[-0.011em]\"\n                  style={{ color: on ? c.text : c.soft }}\n                >\n                  {outcome && (\n                    <Dot size={6} color={TONES[p.node.tone ?? \"neutral\"]} />\n                  )}\n                  <span className=\"truncate\">{p.node.label}</span>\n                </span>\n              </button>\n            );\n          })}\n        </div>\n      </div>\n\n      {/* Narrow cards ask one question at a time. Every step shares one cell, so the card keeps its height. */}\n      <div className=\"grid px-5 pb-4 @2xl:hidden\">\n        {placed.map((p) => {\n          const active = selected === p.node.id;\n          const trail = p.path\n            .slice(1)\n            .map((id) => byId.get(id)?.node.answer)\n            .join(\" / \");\n          return (\n            <div\n              key={p.node.id}\n              className={`[grid-area:1/1] ${active ? \"\" : \"invisible\"}`}\n              aria-hidden={!active}\n            >\n              <p\n                className=\"m-0 text-[12px] leading-[16px] tracking-[-0.005em] tabular-nums\"\n                style={{ color: c.faint }}\n              >\n                {trail || \"Start\"}\n              </p>\n              <p className=\"m-0 mt-1.5 flex items-center gap-2 text-[17px] leading-[24px] font-[590] tracking-[-0.016em]\">\n                {!p.node.children?.length && (\n                  <Dot size={8} color={TONES[p.node.tone ?? \"neutral\"]} />\n                )}\n                {p.node.label}\n              </p>\n              <div className=\"mt-3 flex flex-col gap-2\">\n                {p.node.children?.map((child) => (\n                  <button\n                    key={child.id}\n                    type=\"button\"\n                    tabIndex={active ? 0 : -1}\n                    onClick={() => pick(child.id)}\n                    className=\"flex h-11 cursor-pointer items-center justify-between rounded-lg border px-3.5 text-[13px] font-[510] tracking-[-0.011em] outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2]\"\n                    style={{\n                      background: c.node,\n                      borderColor: c.border,\n                      color: c.text,\n                    }}\n                  >\n                    <span>{child.answer}</span>\n                    <span className=\"tabular-nums\" style={{ color: c.faint }}>\n                      {child.share ?? 0}%\n                    </span>\n                  </button>\n                ))}\n                {!p.node.children?.length && (\n                  <p\n                    className=\"m-0 text-[13px] leading-[20px] tracking-[-0.011em]\"\n                    style={{ color: c.sub }}\n                  >\n                    {p.node.note}\n                  </p>\n                )}\n              </div>\n            </div>\n          );\n        })}\n      </div>\n\n      <footer\n        aria-live=\"polite\"\n        className=\"flex h-[52px] items-center justify-between gap-4 border-t px-5 @2xl:px-6\"\n        style={{ borderColor: c.border }}\n      >\n        <p className=\"m-0 flex min-w-0 items-center gap-2 text-[13px] leading-[20px] font-[510] tracking-[-0.011em]\">\n          <Dot size={8} color={isOutcome ? tone : c.faint} />\n          <span className=\"truncate @2xl:hidden\">\n            {isOutcome ? \"Outcome reached\" : \"Pick an answer to continue\"}\n          </span>\n          <span className=\"hidden truncate @2xl:inline\">\n            {isOutcome ? current.node.label : \"Pick an answer to continue\"}\n          </span>\n          {isOutcome && current.node.note && (\n            <span\n              className=\"hidden truncate font-normal @2xl:inline\"\n              style={{ color: c.sub }}\n            >\n              {current.node.note}\n            </span>\n          )}\n        </p>\n        <p\n          className=\"m-0 shrink-0 text-[13px] leading-[20px] tracking-[-0.011em] tabular-nums\"\n          style={{ color: c.sub }}\n        >\n          {current.node.share ?? 0}% of cases\n        </p>\n      </footer>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}