{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "db-scale",
  "title": "dB Scale",
  "description": "Tick marks and labels for a decibel range.",
  "dependencies": [
    "solid-js@^1.9.15"
  ],
  "registryDependencies": [
    "@audiocn-solid/core",
    "@audiocn-solid/use-audio-config"
  ],
  "files": [
    {
      "path": "components/ui/db-scale.tsx",
      "content": "import {\n  For,\n  createContext,\n  createMemo,\n  createSignal,\n  useContext,\n} from \"solid-js\";\n\nimport { useAudioConfig } from \"@/hooks/use-audio-config\";\nimport { DEFAULT_MAX_DB, DEFAULT_MIN_DB, formatDb } from \"@/lib/audio/decibels\";\nimport { resolveTaper } from \"@/lib/audio/taper\";\nimport type { TaperInput } from \"@/lib/audio/taper\";\nimport type { Orientation, Taper } from \"@/lib/audio/types\";\nimport { provideContext } from \"@/lib/solid/context\";\nimport { createCompatEffect } from \"@/lib/solid/effect\";\nimport type { DivDOMProps } from \"@/lib/solid/jsx-types\";\nimport { omitProps } from \"@/lib/solid/props\";\nimport { setRefValue } from \"@/lib/solid/ref\";\nimport type { RefTarget } from \"@/lib/solid/ref\";\nimport { mergeStyleVars } from \"@/lib/solid/style\";\nimport type { StyleValue } from \"@/lib/solid/style\";\nimport { cn } from \"@/lib/utils\";\n\nconst DEFAULT_TICKS = [12, 6, 0, -6, -12, -18, -24, -36, -48, -60, -72, -90];\n\nconst EDGE = 0.02;\n\nconst LABEL_GAP = 3;\n\nconst MAJOR_STEP = 12;\n\nexport const thinDbScaleLabels = (scale: HTMLElement) => {\n  const horizontal = scale.dataset.orientation !== \"vertical\";\n  const entries: { label: HTMLElement; rank: number; value: number }[] = [];\n\n  for (const tick of scale.querySelectorAll<HTMLElement>(\n    \"[data-slot=db-scale-tick]\"\n  )) {\n    const label = tick.querySelector<HTMLElement>(\"[data-slot=db-scale-label]\");\n\n    if (label) {\n      delete label.dataset.hidden;\n      entries.push({ label, rank: 3, value: Number(tick.dataset.value) });\n    }\n  }\n\n  const values = entries.map((entry) => entry.value);\n  const top = Math.max(...values);\n  const bottom = Math.min(...values);\n\n  for (const entry of entries) {\n    if (entry.value === 0) {\n      entry.rank = 0;\n    } else if (entry.value === top || entry.value === bottom) {\n      entry.rank = 1;\n    } else if (entry.value % MAJOR_STEP === 0) {\n      entry.rank = 2;\n    }\n  }\n\n  entries.sort((a, b) => a.rank - b.rank || b.value - a.value);\n\n  const kept: [number, number][] = [];\n\n  for (const { label } of entries) {\n    const rect = label.getBoundingClientRect();\n\n    if (rect.width === 0 && rect.height === 0) {\n      continue;\n    }\n\n    const start = horizontal ? rect.left : rect.top;\n    const end = horizontal ? rect.right : rect.bottom;\n\n    const collides = kept.some(\n      ([keptStart, keptEnd]) =>\n        start < keptEnd + LABEL_GAP && end > keptStart - LABEL_GAP\n    );\n\n    if (collides) {\n      label.dataset.hidden = \"\";\n    } else {\n      kept.push([start, end]);\n    }\n  }\n};\n\nconst defaultFormat = (db: number) =>\n  formatDb(db, { decimals: 0, unit: false });\n\ninterface DbScaleContextValue {\n  orientation: () => Orientation;\n  side: () => \"start\" | \"end\";\n  labels: () => boolean;\n  taper: () => Taper;\n  format: () => (db: number) => string;\n}\n\nconst DbScaleContext = createContext<DbScaleContextValue | null>(null);\n\nconst useDbScale = () => {\n  const context = useContext(DbScaleContext);\n\n  if (!context) {\n    throw new Error(\"DbScaleTick must be used inside DbScale.\");\n  }\n\n  return context;\n};\n\nconst alignClass = (orientation: Orientation, position: number) => {\n  if (orientation === \"vertical\") {\n    if (position < EDGE) {\n      return \"translate-y-0\";\n    }\n\n    return position > 1 - EDGE ? \"translate-y-full\" : \"translate-y-1/2\";\n  }\n\n  if (position < EDGE) {\n    return \"translate-x-0\";\n  }\n\n  return position > 1 - EDGE ? \"-translate-x-full\" : \"-translate-x-1/2\";\n};\n\nconst markClass = (horizontal: boolean, major: boolean) => {\n  if (horizontal) {\n    return major ? \"h-1.5 w-px\" : \"h-1 w-px\";\n  }\n\n  return major ? \"h-px w-1.5\" : \"h-px w-1\";\n};\n\ntype DivProps = Omit<\n  DivDOMProps,\n  \"children\" | \"class\" | \"className\" | \"ref\" | \"style\"\n> & {\n  class?: string;\n  className?: string;\n  children?: DivDOMProps[\"children\"];\n  style?: StyleValue;\n  ref?: RefTarget<HTMLDivElement>;\n};\n\nexport interface DbScaleTickProps extends DivProps {\n  value: number;\n  /** Major ticks are longer. Default true. */\n  major?: boolean;\n}\n\nconst TICK_OWN_PROPS = [\n  \"value\",\n  \"major\",\n  \"class\",\n  \"className\",\n  \"children\",\n  \"style\",\n  \"ref\",\n] as const;\n\nexport const DbScaleTick = (props: DbScaleTickProps) => {\n  const context = useDbScale();\n  const rest = omitProps(props, TICK_OWN_PROPS);\n  const position = createMemo(() => context.taper().toPosition(props.value));\n  const horizontal = () => context.orientation() === \"horizontal\";\n  const major = () => props.major ?? true;\n  const reversed = () => context.side() === \"start\";\n\n  const style = createMemo(() =>\n    mergeStyleVars(props.style, {\n      \"--tick-position\": `${position() * 100}%`,\n    })\n  );\n\n  return (\n    <div\n      class={cn(\n        \"absolute flex items-center gap-0.5\",\n        horizontal()\n          ? \"top-0 left-(--tick-position) h-full flex-col\"\n          : \"bottom-(--tick-position) left-0 w-full flex-row\",\n        reversed() && (horizontal() ? \"flex-col-reverse\" : \"flex-row-reverse\"),\n        alignClass(context.orientation(), position()),\n        props.class,\n        props.className\n      )}\n      data-major={major() ? \"\" : undefined}\n      data-slot=\"db-scale-tick\"\n      data-value={props.value}\n      ref={(node) => setRefValue(props.ref, node)}\n      style={style()}\n      {...rest}\n    >\n      <span\n        class={cn(\"bg-border shrink-0\", markClass(horizontal(), major()))}\n        data-slot=\"db-scale-mark\"\n      />\n      {context.labels() ? (\n        <span\n          class={cn(\n            \"data-hidden:invisible\",\n            !horizontal() && \"flex-1 text-end\"\n          )}\n          data-slot=\"db-scale-label\"\n        >\n          {props.children ?? context.format()(props.value)}\n        </span>\n      ) : null}\n    </div>\n  );\n};\n\nexport interface DbScaleProps extends DivProps {\n  minDb?: number;\n  maxDb?: number;\n  ticks?: number[];\n  taper?: TaperInput;\n  orientation?: Orientation;\n  side?: \"start\" | \"end\";\n  labels?: boolean;\n  format?: (db: number) => string;\n}\n\nconst SCALE_OWN_PROPS = [\n  \"minDb\",\n  \"maxDb\",\n  \"ticks\",\n  \"taper\",\n  \"orientation\",\n  \"side\",\n  \"labels\",\n  \"format\",\n  \"class\",\n  \"className\",\n  \"children\",\n  \"ref\",\n] as const;\n\nexport const DbScale = (props: DbScaleProps) => {\n  const config = useAudioConfig();\n  const rest = omitProps(props, SCALE_OWN_PROPS);\n  const minDb = () => props.minDb ?? config.minDb ?? DEFAULT_MIN_DB;\n  const maxDb = () => props.maxDb ?? config.maxDb ?? DEFAULT_MAX_DB;\n\n  const orientation = () =>\n    props.orientation ?? config.orientation ?? \"horizontal\";\n\n  const side = () => props.side ?? \"end\";\n  const labels = () => props.labels ?? true;\n  const format = () => props.format ?? defaultFormat;\n\n  const taper = createMemo(() =>\n    resolveTaper(props.taper ?? \"linear\", minDb(), maxDb())\n  );\n\n  const values = createMemo(\n    () =>\n      props.ticks ??\n      DEFAULT_TICKS.filter((tick) => tick >= minDb() && tick <= maxDb())\n  );\n\n  const [scale, setScale] = createSignal<HTMLDivElement | null>(null);\n\n  const context: DbScaleContextValue = {\n    format,\n    labels,\n    orientation,\n    side,\n    taper,\n  };\n\n  createCompatEffect(\n    () => ({ element: scale(), labels: labels() }),\n    ({ element, labels: showLabels }) => {\n      if (!(element && showLabels)) {\n        return;\n      }\n\n      let cancelled = false;\n      const thin = () => thinDbScaleLabels(element);\n      thin();\n\n      const resize =\n        typeof ResizeObserver === \"undefined\" ? null : new ResizeObserver(thin);\n\n      resize?.observe(element);\n\n      const ticksChanged =\n        typeof MutationObserver === \"undefined\"\n          ? null\n          : new MutationObserver(thin);\n\n      ticksChanged?.observe(element, {\n        attributeFilter: [\"style\"],\n        characterData: true,\n        childList: true,\n        subtree: true,\n      });\n\n      if (typeof document !== \"undefined\") {\n        void document.fonts?.ready.then(() => {\n          if (!cancelled) {\n            thin();\n          }\n        });\n      }\n\n      return () => {\n        cancelled = true;\n        resize?.disconnect();\n        ticksChanged?.disconnect();\n      };\n    }\n  );\n\n  const setScaleRef = (node: HTMLDivElement) => {\n    setScale(node);\n    setRefValue(props.ref, node);\n  };\n\n  return provideContext(DbScaleContext, context, () => (\n    <div\n      aria-hidden={\"true\"}\n      class={cn(\n        \"text-muted-foreground relative shrink-0 text-[0.625rem] leading-none tabular-nums select-none\",\n        orientation() === \"horizontal\" ? \"h-4 w-full\" : \"h-full w-7\",\n        props.class,\n        props.className\n      )}\n      data-orientation={orientation()}\n      data-side={side()}\n      data-slot=\"db-scale\"\n      ref={setScaleRef}\n      {...rest}\n    >\n      {props.children ?? (\n        <For each={values()}>{(tick) => <DbScaleTick value={tick} />}</For>\n      )}\n    </div>\n  ));\n};\n",
      "type": "registry:ui"
    },
    {
      "path": "lib/audiocn-license.txt",
      "content": "Audio component source adapted from https://github.com/audiocn/ui.\nMIT License — Copyright (c) 2026 OrcDev\n\nRegular SVG icon geometry adapted from @phosphor-icons/react 2.1.10.\nMIT License — Copyright (c) 2020 Phosphor Icons\n\nThe following permission and warranty terms apply to both notices.\nRetain this file with copies or substantial portions of the source.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n",
      "type": "registry:file",
      "target": "@lib/audiocn-license.txt"
    }
  ],
  "type": "registry:ui"
}
