{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-cc-unmapped-notice",
  "type": "registry:ui",
  "title": "CC Unmapped Notice",
  "description": "The host's one toast for a hardware controller that moved but is not mapped: the source and channel, a live value, a chip you drag straight onto a control, Pick control (click or arrow keys and Enter) and Ignore. It never takes focus, fades after the control goes quiet, and guides any source-first pick. Every edit goes through the shared CC drag controller, so drops into sandboxed face frames work too.",
  "dependencies": [
    "maruaudio@>=0.0.3 <0.1.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "components/ui/cc-unmapped-notice.tsx",
      "content": "/**\n * Source: https://maru.audio/ui-kit/cc-unmapped-notice\n * Copyright © [Maru](https://maru.audio)\n *\n * Maru UI Kit — https://maru.audio/ui-kit\n *\n * Installed from the Maru UI registry. This file is yours now: edit it\n * freely, it will not be overwritten unless you re-run the add command.\n *\n * Keep this item’s companion files and CSS imports together. Dependencies\n * declared by the registry are installed with the source.\n */\n/**\n * CcUnmappedNotice — the host's one toast for a controller nobody mapped yet.\n *\n * When a hardware knob that is not bound to anything moves deliberately, the\n * host's unmapped-CC detector raises a candidate. This notice shows it: the\n * source (\"CC 21 · KeyStep · ch 1\"), its live value, a chip you can drag\n * straight onto a control, **Pick control** (click a highlighted control, or\n * arrows and Enter) and **Ignore**.\n *\n * It is a registry item with no app imports so every host shows the same\n * thing: the website mounts it once through the playback bar, and a native\n * webview can mount it beside `HostedModuleFace`.\n *\n * Everything that changes a map goes through the host's one `CcDndController`\n * — the chip is the controller's pointer-capture drag source (not HTML5\n * drag-and-drop) and Pick control is its `startTargetSelection` — so a drop\n * lands on a same-document knob or a knob inside an opaque face frame through\n * the same target registry every other chip uses.\n *\n * Presentation rules:\n *\n *   - It never takes focus. The message is a polite live region; the buttons\n *     are ordinary tab stops.\n *   - One notice at a time. A newer candidate simply replaces the current one.\n *   - It fades out after a quiet period (`quietMs` since the control last\n *     moved) unless it is hovered, focused or a drag/pick is in flight.\n *   - While any source-first target selection runs (from here, or from a\n *     mappings list retargeting a row) it becomes the guide for that pick:\n *     \"Choose a control for CC 21\", with Cancel.\n *\n * `form=\"guide\"` keeps only that guide (plus the input mute, when offered):\n * a host whose bar already shows the candidate as a chip — every playback\n * bar surface — does not repeat it as a sentence (design 2026-10-27 §8.2).\n */\n\nimport { useEffect, useRef, useState } from \"react\"\nimport type { CSSProperties, ReactNode } from \"react\"\nimport { token } from \"maruaudio/ui/shared/theme\"\nimport {\n  CcDragGhost,\n  ccDescription,\n  ccRoleName,\n  ccRoleShortLabel,\n  useCcDndState,\n  useCcDragSource,\n  type CcAssignment,\n  type CcDndController,\n  type CcDndSnapshot,\n  type CcRoleName,\n} from \"maruaudio/ui/shared/cc-dnd\"\n\n/**\n * The detector's candidate, structurally.\n *\n * Declared here rather than imported so the registry item stands alone; the\n * SDK's `UnmappedCcCandidate` satisfies it as is.\n */\nexport interface CcUnmappedCandidate {\n  /** The detector's stable identity: source kind, source id, channel, CC. */\n  readonly key: string\n  readonly cc: number\n  /** 1–16, the channel the gesture arrived on. */\n  readonly channel: number\n  /** The value that established intent. */\n  readonly value?: number\n  readonly source?: { readonly id?: string; readonly label?: string }\n  /**\n   * The controller role the host's global role layer gives this source\n   * (\"modWheel\", \"macro3\"), when it gives one. A drop then creates a role\n   * binding, which follows the player's mod wheel everywhere rather than\n   * this one device and channel.\n   */\n  readonly role?: CcRoleName\n}\n\n/** How long after the control last moved the notice fades out. */\nexport const CC_UNMAPPED_NOTICE_QUIET_MS = 8_000\n/** The fade itself, at the end of the quiet period. */\nconst FADE_MS = 400\n\n/**\n * What a drop or a pick assigns: the controller number AND the input and\n * channel it arrived on, the same identity a learn records. Dropping the\n * chip therefore binds exactly the knob that moved, not every CC 21 on every\n * port.\n */\nexport function ccCandidateAssignment(\n  candidate: CcUnmappedCandidate\n): CcAssignment {\n  return {\n    cc: candidate.cc,\n    channel: candidate.channel,\n    ...(candidate.role ? { role: candidate.role } : {}),\n    ...(candidate.source?.id ? { sourceId: candidate.source.id } : {}),\n    ...(candidate.source?.label ? { sourceLabel: candidate.source.label } : {}),\n  }\n}\n\n/** \"CC 21 · KeyStep · ch 1\" (or \"Mod wheel · CC 1 · …\") — the source. */\nexport function ccCandidateTitle(\n  candidate: Pick<CcUnmappedCandidate, \"cc\" | \"channel\" | \"source\" | \"role\">\n): string {\n  return [\n    candidate.role ? ccRoleName(candidate.role) : undefined,\n    `CC ${candidate.cc}`,\n    candidate.source?.label,\n    `ch ${candidate.channel}`,\n  ]\n    .filter(Boolean)\n    .join(\" · \")\n}\n\n/** The chip's visible text: the role's short name, else \"CC 21\". */\nexport function ccCandidateChipText(\n  candidate: Pick<CcUnmappedCandidate, \"cc\" | \"role\">\n): string {\n  return candidate.role\n    ? ccRoleShortLabel(candidate.role)\n    : `CC ${candidate.cc}`\n}\n\n/** The chip's accessible name. */\nexport function ccCandidateChipLabel(\n  candidate: Pick<CcUnmappedCandidate, \"cc\" | \"role\">\n): string {\n  return candidate.role\n    ? `Drag ${ccRoleName(candidate.role)} (CC ${candidate.cc}) onto a control`\n    : `Drag CC ${candidate.cc} onto a control`\n}\n\n/** The drag-source id a candidate's chip drags under, wherever it is drawn. */\nexport function ccCandidateSourceId(\n  candidate: Pick<CcUnmappedCandidate, \"key\">\n): string {\n  return `unmapped:${candidate.key}`\n}\n\nexport type CcUnmappedNoticeView =\n  | { readonly kind: \"hidden\" }\n  | {\n      readonly kind: \"candidate\"\n      readonly title: string\n      readonly hint: string\n      readonly assignment: CcAssignment\n    }\n  | {\n      readonly kind: \"selecting\"\n      readonly title: string\n      readonly hint: string\n    }\n\n/**\n * What the notice shows for a candidate and the controller's state.\n *\n * Pure, so the rules are pinned without a DOM: a running source-first pick\n * wins (whoever started it), otherwise the candidate, otherwise nothing.\n */\nexport function ccUnmappedNoticeView(\n  candidate: CcUnmappedCandidate | null,\n  dnd: Pick<CcDndSnapshot, \"assignment\" | \"mode\" | \"overTargetLabel\">\n): CcUnmappedNoticeView {\n  if (dnd.mode === \"selection\" && dnd.assignment) {\n    return {\n      kind: \"selecting\",\n      title: `Choose a control for ${ccDescription(dnd.assignment)}`,\n      hint: dnd.overTargetLabel\n        ? `${dnd.overTargetLabel} · Enter to map`\n        : \"Click a control, or use arrows + Enter.\",\n    }\n  }\n  if (!candidate) return { kind: \"hidden\" }\n  return {\n    kind: \"candidate\",\n    title: `${ccCandidateTitle(candidate)} is moving`,\n    hint: \"Drag the chip onto a control, or pick one.\",\n    assignment: ccCandidateAssignment(candidate),\n  }\n}\n\nexport interface CcUnmappedNoticeProps {\n  /** The current candidate, or null. A new key replaces the old notice. */\n  candidate: CcUnmappedCandidate | null\n  /** The host's one drag controller. Defaults to the ambient one. */\n  controller?: CcDndController\n  /** Live 0–127 values keyed by CC, from the host's activity feed. */\n  activity?: Readonly<Record<number, number>>\n  /**\n   * The candidate's own live value, from the exact source, channel and CC\n   * that raised it. Wins over `activity`, which is keyed by number alone and\n   * mixes every device sending that CC.\n   */\n  value?: number\n  /**\n   * Ignore, fade-out and \"mapped\" all end here. The host's detector already\n   * holds its cooldown for that source, so dismissing is enough.\n   */\n  onDismiss: () => void\n  /**\n   * \"Don't suggest for this input\": a session-scoped mute of the candidate's\n   * input. Offered only when the candidate names an input and the host\n   * supplies this.\n   */\n  onMuteSource?: (sourceId: string) => void\n  /** A drop or a pick assigned the candidate to this target. */\n  onMapped?: (targetId: string) => void\n  /** Quiet period before the fade. Defaults to `CC_UNMAPPED_NOTICE_QUIET_MS`. */\n  quietMs?: number\n  /**\n   * \"toast\" (default): the full notice. \"guide\": only the \"Choose a control\n   * for …\" guide during a pick, and for a candidate nothing but the input\n   * mute — for hosts whose bar chip already shows the candidate.\n   */\n  form?: \"toast\" | \"guide\"\n  /**\n   * Draw the chip's drag ghost. False where the host already keeps one\n   * ghost mounted for candidate drags (the playback bar's CC slot).\n   */\n  ghost?: boolean\n  className?: string\n  style?: CSSProperties\n}\n\nconst buttonStyle = (primary: boolean): CSSProperties => ({\n  appearance: \"none\",\n  border: `1px solid ${primary ? token.accent : token.border}`,\n  borderRadius: token.radius,\n  background: primary\n    ? `color-mix(in srgb, ${token.accent} 22%, ${token.surface})`\n    : \"transparent\",\n  color: token.text,\n  font: \"inherit\",\n  fontSize: 11,\n  fontWeight: 600,\n  padding: \"4px 9px\",\n  cursor: \"pointer\",\n  whiteSpace: \"nowrap\",\n})\n\n/** The notice. Renders nothing while there is nothing to show. */\nexport function CcUnmappedNotice({\n  candidate,\n  controller,\n  activity,\n  value,\n  onDismiss,\n  onMuteSource,\n  onMapped,\n  quietMs = CC_UNMAPPED_NOTICE_QUIET_MS,\n  ghost = true,\n  form = \"toast\",\n  className,\n  style,\n}: CcUnmappedNoticeProps) {\n  const dnd = useCcDndState(controller)\n  const view = ccUnmappedNoticeView(candidate, dnd)\n  const [hovered, setHovered] = useState(false)\n  const [focused, setFocused] = useState(false)\n  const [fading, setFading] = useState(false)\n  const onDismissRef = useRef(onDismiss)\n  onDismissRef.current = onDismiss\n\n  const liveValue =\n    candidate === null\n      ? undefined\n      : (value ?? activity?.[candidate.cc] ?? candidate.value)\n  const busy = dnd.assignment !== null\n  const held = hovered || focused || busy\n\n  // The quiet period restarts whenever the control moves (a new live value)\n  // or a new candidate arrives, and never runs while the notice is held.\n  useEffect(() => {\n    setFading(false)\n    // The guide form never times a candidate out: the bar's chip, which\n    // shows it, owns the quiet period.\n    if (!candidate || held || form === \"guide\") return\n    const fade = setTimeout(\n      () => setFading(true),\n      Math.max(0, quietMs - FADE_MS)\n    )\n    const dismiss = setTimeout(() => onDismissRef.current(), quietMs)\n    return () => {\n      clearTimeout(fade)\n      clearTimeout(dismiss)\n    }\n  }, [candidate, form, held, liveValue, quietMs])\n\n  if (view.kind === \"hidden\") return null\n  if (form === \"guide\" && view.kind === \"candidate\") {\n    const sourceId = candidate?.source?.id\n    if (!onMuteSource || !sourceId) return null\n    return (\n      <div\n        className={`uikit-control${className ? ` ${className}` : \"\"}`}\n        data-uikit-cc-unmapped-notice=\"mute\"\n        style={{\n          display: \"flex\",\n          color: token.text,\n          fontFamily: token.font,\n          fontSize: 12,\n          ...style,\n          boxShadow: \"none\",\n        }}\n      >\n        <MuteButton onClick={() => onMuteSource(sourceId)} />\n      </div>\n    )\n  }\n\n  const mapped = (_assignment: CcAssignment, targetId: string) => {\n    onMapped?.(targetId)\n    onDismissRef.current()\n  }\n\n  return (\n    <div\n      className={`uikit-control${className ? ` ${className}` : \"\"}`}\n      data-uikit-cc-unmapped-notice={view.kind}\n      role=\"group\"\n      aria-label=\"Unmapped MIDI controller\"\n      onPointerEnter={() => setHovered(true)}\n      onPointerLeave={() => setHovered(false)}\n      onFocus={() => setFocused(true)}\n      onBlur={(event) => {\n        const next = event.relatedTarget as Node | null\n        if (!next || !event.currentTarget.contains(next)) setFocused(false)\n      }}\n      style={{\n        boxSizing: \"border-box\",\n        display: \"flex\",\n        alignItems: \"center\",\n        gap: 10,\n        padding: \"8px 10px\",\n        borderRadius: token.radius,\n        border: `1px solid color-mix(in srgb, ${token.accent} 45%, ${token.border})`,\n        background: token.surface,\n        boxShadow: \"0 10px 30px rgba(0,0,0,0.35)\",\n        color: token.text,\n        fontFamily: token.font,\n        fontSize: 12,\n        opacity: fading && !held ? 0 : 1,\n        transition: `opacity ${FADE_MS}ms ease-out`,\n        ...style,\n      }}\n    >\n      {view.kind === \"candidate\" && candidate ? (\n        <CcCandidateChip\n          candidate={candidate}\n          assignment={view.assignment}\n          controller={controller}\n          activity={liveValue}\n          onDropped={mapped}\n          ghost={ghost}\n        />\n      ) : null}\n      <div style={{ minWidth: 0, flex: \"1 1 auto\" }}>\n        <div role=\"status\" aria-live=\"polite\">\n          <div\n            style={{\n              fontWeight: 600,\n              whiteSpace: \"nowrap\",\n              overflow: \"hidden\",\n              textOverflow: \"ellipsis\",\n            }}\n          >\n            {view.title}\n          </div>\n          <div\n            style={{\n              color: token.textMuted,\n              fontSize: 11,\n              whiteSpace: \"nowrap\",\n              overflow: \"hidden\",\n              textOverflow: \"ellipsis\",\n            }}\n          >\n            {view.hint}\n          </div>\n        </div>\n        {view.kind === \"candidate\" && liveValue !== undefined ? (\n          <LiveValueBar value={liveValue} />\n        ) : null}\n      </div>\n      <div style={{ display: \"flex\", flexWrap: \"wrap\", gap: 6 }}>\n        {view.kind === \"selecting\" ? (\n          <button\n            type=\"button\"\n            style={buttonStyle(false)}\n            onClick={() => controller?.cancel()}\n          >\n            Cancel\n          </button>\n        ) : (\n          <>\n            <button\n              type=\"button\"\n              style={buttonStyle(true)}\n              onClick={() =>\n                controller?.startTargetSelection(view.assignment, {\n                  sourceId: candidate\n                    ? ccCandidateSourceId(candidate)\n                    : undefined,\n                  onDropped: mapped,\n                })\n              }\n            >\n              Pick control\n            </button>\n            <button\n              type=\"button\"\n              style={buttonStyle(false)}\n              aria-label=\"Ignore this MIDI controller\"\n              onClick={() => onDismissRef.current()}\n            >\n              Ignore\n            </button>\n            {onMuteSource && candidate?.source?.id ? (\n              <MuteButton onClick={() => onMuteSource(candidate.source!.id!)} />\n            ) : null}\n          </>\n        )}\n      </div>\n    </div>\n  )\n}\n\nfunction MuteButton({ onClick }: { onClick: () => void }): ReactNode {\n  return (\n    <button\n      type=\"button\"\n      style={{\n        ...buttonStyle(false),\n        border: \"none\",\n        color: token.textMuted,\n        padding: \"4px 2px\",\n      }}\n      onClick={onClick}\n    >\n      Don&apos;t suggest for this input\n    </button>\n  )\n}\n\nexport interface CcCandidateChipProps {\n  candidate: CcUnmappedCandidate\n  assignment?: CcAssignment\n  controller?: CcDndController\n  /** The live value shown beside the number. */\n  activity?: number\n  onDropped: (assignment: CcAssignment, targetId: string) => void\n  /**\n   * Render this chip's own drag ghost. A host that keeps one ghost mounted\n   * where the chip may unmount mid-drag (the playback bar's reserved slot)\n   * passes false and draws `CcDragGhost` itself.\n   */\n  ghost?: boolean\n  /** A click (no drag) starts source-first target selection. */\n  onPick?: () => void\n  /** Draw the live value as a thin bar under the text (compact hosts). */\n  valueBar?: boolean\n  style?: CSSProperties\n}\n\n/**\n * The draggable half: the controller's own drag source and ghost.\n *\n * Exported for hosts that show the candidate in a slot of their own (the\n * playback bar's reserved CC slot) rather than as the full notice.\n */\nexport function CcCandidateChip({\n  candidate,\n  assignment = ccCandidateAssignment(candidate),\n  controller,\n  activity,\n  onDropped,\n  ghost = true,\n  onPick,\n  valueBar = false,\n  style,\n}: CcCandidateChipProps) {\n  const source = useCcDragSource({\n    assignment,\n    sourceId: ccCandidateSourceId(candidate),\n    controller,\n    onDropped,\n    ...(onPick ? { activationDistance: 4 } : {}),\n  })\n  const lit = source.dragging\n  return (\n    <span\n      {...source.handleProps}\n      {...(onPick ? { onClick: () => onPick() } : {})}\n      aria-label={ccCandidateChipLabel(candidate)}\n      aria-description={`${ccCandidateTitle(candidate)}. Press Enter to pick up, arrow keys to choose a control, Enter to drop, Escape to cancel.`}\n      data-uikit-cc-unmapped-chip=\"\"\n      data-uikit-cc-candidate-key={candidate.key}\n      style={{\n        position: \"relative\",\n        display: \"inline-flex\",\n        alignItems: \"center\",\n        gap: 5,\n        flex: \"0 0 auto\",\n        padding: \"5px 8px\",\n        borderRadius: token.radius,\n        background: `color-mix(in srgb, ${token.accent} ${lit ? 30 : 18}%, ${token.surface})`,\n        border: `1px solid ${token.accent}`,\n        color: token.text,\n        fontFamily: token.fontMono,\n        fontSize: 11,\n        fontWeight: 700,\n        cursor: lit ? \"grabbing\" : \"grab\",\n        userSelect: \"none\",\n        touchAction: \"none\",\n        ...style,\n      }}\n    >\n      <svg\n        width=\"10\"\n        height=\"12\"\n        viewBox=\"0 0 10 12\"\n        fill=\"currentColor\"\n        aria-hidden\n      >\n        {[2, 6, 10].map((y) =>\n          [2, 7].map((x) => <circle key={`${x}-${y}`} cx={x} cy={y} r=\"1\" />)\n        )}\n      </svg>\n      <span>{ccCandidateChipText(candidate)}</span>\n      {activity !== undefined ? (\n        <span\n          aria-hidden\n          data-uikit-cc-candidate-value={Math.round(activity)}\n          style={{ color: token.textMuted, fontVariantNumeric: \"tabular-nums\" }}\n        >\n          {Math.round(activity)}\n        </span>\n      ) : null}\n      {valueBar && activity !== undefined ? (\n        <span\n          aria-hidden\n          style={{\n            position: \"absolute\",\n            left: 4,\n            right: 4,\n            bottom: 1,\n            height: 2,\n            borderRadius: 1,\n            background: token.track,\n            overflow: \"hidden\",\n          }}\n        >\n          <span\n            style={{\n              display: \"block\",\n              width: `${Math.min(1, Math.max(0, activity / 127)) * 100}%`,\n              height: \"100%\",\n              background: token.accent,\n              transition: \"width 80ms linear\",\n            }}\n          />\n        </span>\n      ) : null}\n      {ghost ? (\n        <CcDragGhost sourceId={source.sourceId} controller={controller} />\n      ) : null}\n    </span>\n  )\n}\n\nfunction LiveValueBar({ value }: { value: number }) {\n  const fraction = Math.min(1, Math.max(0, value / 127))\n  return (\n    <div\n      aria-hidden\n      data-uikit-cc-unmapped-value={Math.round(value)}\n      style={{\n        marginTop: 5,\n        height: 3,\n        borderRadius: 2,\n        background: token.track,\n        overflow: \"hidden\",\n      }}\n    >\n      <div\n        style={{\n          width: `${fraction * 100}%`,\n          height: \"100%\",\n          background: token.accent,\n          transition: \"width 80ms linear\",\n        }}\n      />\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "@ui/cc-unmapped-notice.tsx"
    }
  ]
}
