{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "clip-indicator",
  "title": "Clip Indicator",
  "description": "A clip light that holds after the signal clips, with an optional count and click to reset.",
  "dependencies": [
    "@base-ui/react"
  ],
  "registryDependencies": [
    "@audiocn/core",
    "@audiocn/use-clip-hold",
    "@audiocn/use-frame-source"
  ],
  "files": [
    {
      "path": "components/ui/clip-indicator.tsx",
      "content": "\"use client\";\n\nimport { mergeProps } from \"@base-ui/react/merge-props\";\nimport { useRender } from \"@base-ui/react/use-render\";\nimport { useImperativeHandle } from \"react\";\nimport type { Ref } from \"react\";\n\nimport { useClipHold } from \"@/hooks/use-clip-hold\";\nimport { useFrameSource } from \"@/hooks/use-frame-source\";\nimport { SILENCE_DB } from \"@/lib/audio/decibels\";\nimport type { FrameSource, MeterFrame } from \"@/lib/audio/types\";\nimport { CLIP_HOLD_MS, CLIP_THRESHOLD_DB } from \"@/lib/audio/zones\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface ClipIndicatorActions {\n  /** Feed a level reading. */\n  report: (db: number) => void;\n  /** Turn the light off and clear the count. */\n  reset: () => void;\n}\n\nexport interface ClipIndicatorProps extends useRender.ComponentProps<\"button\"> {\n  /** Controlled clip state. When set, the component does no detection. */\n  clipping?: boolean;\n  /** A meter source; the indicator detects clipping itself. */\n  source?: FrameSource<MeterFrame> | null;\n  /** Levels at or above this count as a clip. Default −1 dBFS. */\n  thresholdDb?: number;\n  /** How long the light stays on. `Infinity` latches until reset. Default 1500 ms. */\n  holdMs?: number;\n  onClippingChange?: (clipping: boolean) => void;\n  /** Show the number of clips next to the light. Default false. */\n  showCount?: boolean;\n  actionsRef?: Ref<ClipIndicatorActions>;\n}\n\nconst loudestPeak = (frame: MeterFrame) => {\n  let loudest = SILENCE_DB;\n  for (const level of frame.channels) {\n    loudest = Math.max(loudest, level.peakDb);\n  }\n  return loudest;\n};\n\nexport const ClipIndicator = ({\n  clipping: clippingProp,\n  source,\n  thresholdDb = CLIP_THRESHOLD_DB,\n  holdMs = CLIP_HOLD_MS,\n  onClippingChange,\n  showCount = false,\n  actionsRef,\n  render,\n  className,\n  children,\n  ...props\n}: ClipIndicatorProps) => {\n  const hold = useClipHold({ holdMs, onClippingChange, thresholdDb });\n  const clipping = clippingProp ?? hold.clipping;\n\n  useFrameSource(source, (frame) => {\n    hold.report(loudestPeak(frame));\n  });\n\n  useImperativeHandle(\n    actionsRef,\n    () => ({ report: hold.report, reset: hold.reset }),\n    [hold.report, hold.reset]\n  );\n\n  const content = (\n    <>\n      {children ?? (\n        <span\n          className=\"bg-muted-foreground/30 group-data-clipping/clip-indicator:bg-meter-clip size-2 shrink-0 rounded-full transition-colors\"\n          data-slot=\"clip-indicator-light\"\n        />\n      )}\n      {showCount ? (\n        <span className=\"tabular-nums\" data-slot=\"clip-indicator-count\">\n          {hold.count}\n        </span>\n      ) : null}\n      <span aria-live=\"polite\" className=\"sr-only\">\n        {clipping ? \"Clipping\" : \"\"}\n      </span>\n    </>\n  );\n\n  return useRender({\n    defaultTagName: \"button\",\n    props: mergeProps<\"button\">(\n      {\n        \"aria-label\": clipping\n          ? \"Clipping. Reset clip indicator\"\n          : \"Clip indicator\",\n        children: content,\n        className: cn(\n          \"group/clip-indicator text-muted-foreground hover:bg-muted focus-visible:ring-ring/30 data-clipping:text-meter-clip-foreground relative inline-flex h-5 shrink-0 items-center justify-center gap-1 rounded-full px-1 text-xs font-medium transition-colors outline-none after:absolute after:-inset-1 focus-visible:ring-3 pointer-coarse:after:-inset-2.5\",\n          className\n        ),\n        onClick: () => {\n          hold.reset();\n        },\n        type: \"button\",\n      },\n      props\n    ),\n    render,\n    state: {\n      clipping,\n      slot: \"clip-indicator\",\n    },\n  });\n};\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}