{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bar-visualizer",
  "title": "Bar Visualizer",
  "description": "A row of bars driven by frequency bands, with idle, loading and mirrored modes. Bars paint without re-rendering React.",
  "registryDependencies": [
    "@audiocn/core",
    "@audiocn/use-audio-config",
    "@audiocn/use-frame-source",
    "@audiocn/use-reduced-motion",
    "@audiocn/use-visibility"
  ],
  "files": [
    {
      "path": "components/ui/bar-visualizer.tsx",
      "content": "\"use client\";\n\nimport { useCallback, useEffect, useImperativeHandle, useRef } from \"react\";\nimport type { ComponentProps, CSSProperties, Ref, RefObject } from \"react\";\n\nimport { useAudioConfig } from \"@/hooks/use-audio-config\";\nimport { useFrameSource } from \"@/hooks/use-frame-source\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { useVisibility } from \"@/hooks/use-visibility\";\nimport { createBarLevels } from \"@/lib/audio/bar-levels\";\nimport type { BarIdle, BarLevelsOptions } from \"@/lib/audio/bar-levels\";\nimport { createFrameTask, createPainterClock } from \"@/lib/audio/frame-loop\";\nimport type { FrameSource, Orientation, VisualFrame } from \"@/lib/audio/types\";\nimport { cn } from \"@/lib/utils\";\n\nconst DEFAULT_BAR_COUNT = 24;\nconst DEFAULT_MIN_LEVEL = 0.08;\nconst REDUCED_MOTION_INTERVAL_MS = 250;\n\nexport interface BarVisualizerActions {\n  /** Paint levels (0..1) directly. They are resampled to the bar count. */\n  paint: (levels: ArrayLike<number>) => void;\n}\n\nexport interface BarVisualizerProps extends ComponentProps<\"div\"> {\n  /** A visual source; the bars follow its frequency bands. */\n  source?: FrameSource<VisualFrame> | null;\n  /** Levels, 0..1, for declarative use. */\n  levels?: ArrayLike<number>;\n  /** Number of bars. Bands are resampled to fit. Default 24. */\n  barCount?: number;\n  /** Where bars grow from. Default `center`. */\n  align?: \"center\" | \"start\" | \"end\";\n  /** Symmetric around the middle bar. Default false. */\n  mirrored?: boolean;\n  /** Resting bar size, 0..1. Default 0.08. */\n  minLevel?: number;\n  /** What the bars do with no signal. Default `static`. */\n  idle?: BarIdle;\n  /** Runs a sweep animation, for connecting or thinking states. Default false. */\n  loading?: boolean;\n  orientation?: Orientation;\n  actionsRef?: Ref<BarVisualizerActions>;\n}\n\nconst ALIGN_CLASS = {\n  center: \"items-center\",\n  end: \"items-end\",\n  start: \"items-start\",\n} as const;\n\ninterface BarPainterOptions extends BarLevelsOptions {\n  bars: (HTMLSpanElement | null)[];\n  input: RefObject<ArrayLike<number> | null>;\n  root: RefObject<HTMLElement | null>;\n  visible: RefObject<boolean>;\n}\n\nconst noop = () => {\n  // Nothing to wake before the painter starts.\n};\n\n/**\n * Paints bars outside React, with release smoothing and idle animations.\n * Returns true while it needs another frame: static bars that have settled,\n * or bars off screen, cost no frames until new levels wake them.\n */\nconst createBarPainter = (options: BarPainterOptions) => {\n  const { bars, reducedMotion } = options;\n  const barLevels = createBarLevels(options);\n  const shown = new Float32Array(options.barCount).fill(-1);\n  const clock = createPainterClock();\n  let lastPaintMs = Number.NEGATIVE_INFINITY;\n  let activeShown: boolean | null = null;\n\n  return (frameMs: number): boolean => {\n    // Hidden: sleep. Coming back into view wakes the painter.\n    if (!options.visible.current) {\n      return false;\n    }\n    const nowMs = clock(frameMs);\n    if (reducedMotion && nowMs - lastPaintMs < REDUCED_MOTION_INTERVAL_MS) {\n      return true;\n    }\n    lastPaintMs = nowMs;\n    const active = barLevels.step(nowMs, options.input.current);\n    if (active !== activeShown) {\n      activeShown = active;\n      options.root.current?.toggleAttribute(\"data-active\", active);\n    }\n\n    for (const [index, value] of barLevels.levels.entries()) {\n      if (Math.abs(value - (shown[index] ?? -1)) > 0.002) {\n        shown[index] = value;\n        bars[index]?.style.setProperty(\"--bar-level\", value.toFixed(4));\n      }\n    }\n    return !barLevels.settled;\n  };\n};\n\nexport const BarVisualizer = ({\n  source,\n  levels,\n  barCount = DEFAULT_BAR_COUNT,\n  align = \"center\",\n  mirrored = false,\n  minLevel = DEFAULT_MIN_LEVEL,\n  idle = \"static\",\n  loading = false,\n  orientation: orientationProp,\n  actionsRef,\n  className,\n  ref,\n  ...props\n}: BarVisualizerProps) => {\n  const config = useAudioConfig();\n  const orientation = orientationProp ?? config.orientation ?? \"horizontal\";\n  const reducedMotion = useReducedMotion();\n  const barsRef = useRef<(HTMLSpanElement | null)[]>([]);\n  const inputRef = useRef<ArrayLike<number> | null>(null);\n  const rootRef = useRef<HTMLDivElement | null>(null);\n  // Wakes the painter when new levels arrive. Settled bars sleep.\n  const wakeRef = useRef<() => void>(noop);\n  const visibleRef = useVisibility(rootRef, (visible) => {\n    if (visible) {\n      wakeRef.current();\n    }\n  });\n\n  const paint = useCallback((next: ArrayLike<number>) => {\n    inputRef.current = next;\n    wakeRef.current();\n  }, []);\n\n  useFrameSource(source, (frame) => {\n    inputRef.current = frame.bands;\n    wakeRef.current();\n  });\n\n  // Clear only when levels go from set to unset, so the bars fall instead of\n  // freezing, without wiping levels painted through the handle on re-runs.\n  const hadLevelsRef = useRef(false);\n  useEffect(() => {\n    if (levels) {\n      inputRef.current = levels;\n      hadLevelsRef.current = true;\n    } else if (hadLevelsRef.current) {\n      inputRef.current = null;\n      hadLevelsRef.current = false;\n    }\n    wakeRef.current();\n  }, [levels]);\n\n  useImperativeHandle(actionsRef, () => ({ paint }), [paint]);\n\n  useEffect(() => {\n    const task = createFrameTask(\n      createBarPainter({\n        barCount,\n        bars: barsRef.current,\n        idle,\n        input: inputRef,\n        loading,\n        minLevel,\n        mirrored,\n        reducedMotion,\n        root: rootRef,\n        visible: visibleRef,\n      })\n    );\n    wakeRef.current = task.wake;\n    const root = rootRef.current;\n    return () => {\n      wakeRef.current = noop;\n      task.stop();\n      if (root) {\n        delete root.dataset.active;\n      }\n    };\n  }, [barCount, idle, loading, minLevel, mirrored, reducedMotion, visibleRef]);\n\n  const setRootRef = useCallback(\n    (node: HTMLDivElement | null) => {\n      rootRef.current = node;\n      if (typeof ref === \"function\") {\n        ref(node);\n      } else if (ref) {\n        ref.current = node;\n      }\n    },\n    [ref]\n  );\n\n  const horizontal = orientation === \"horizontal\";\n\n  return (\n    <div\n      aria-label=\"Audio visualizer\"\n      className={cn(\n        \"flex justify-center gap-(--bar-gap) [--bar-gap:0.1875rem] [--bar-radius:9999px] [--bar-width:0.375rem]\",\n        horizontal ? \"h-16 w-full flex-row\" : \"h-full w-16 flex-col\",\n        ALIGN_CLASS[align],\n        className\n      )}\n      data-loading={loading ? \"\" : undefined}\n      data-orientation={orientation}\n      data-slot=\"bar-visualizer\"\n      role=\"img\"\n      {...props}\n      ref={setRootRef}\n    >\n      {Array.from({ length: barCount }, (_, index) => (\n        <span\n          className={cn(\n            \"rounded-(--bar-radius) bg-current\",\n            horizontal\n              ? \"h-[calc(var(--bar-level)*100%)] max-w-(--bar-width) min-w-0 flex-1\"\n              : \"max-h-(--bar-width) min-h-0 w-[calc(var(--bar-level)*100%)] flex-1\"\n          )}\n          data-index={index}\n          data-slot=\"bar-visualizer-bar\"\n          key={`bar-${index}`}\n          ref={(node) => {\n            barsRef.current[index] = node;\n          }}\n          style={{ \"--bar-level\": minLevel } as CSSProperties}\n        />\n      ))}\n    </div>\n  );\n};\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}