{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tempo-control",
  "title": "Tempo Control",
  "description": "Editable BPM control with tap tempo.",
  "dependencies": [
    "solid-js@^1.9.15",
    "class-variance-authority"
  ],
  "registryDependencies": [
    "@audiocn-solid/core",
    "@audiocn-solid/use-audio-config",
    "@audiocn-solid/use-tap-tempo"
  ],
  "files": [
    {
      "path": "components/ui/tempo-control.tsx",
      "content": "import { cva } from \"class-variance-authority\";\nimport { createContext, createSignal, useContext } from \"solid-js\";\nimport type { Component } from \"solid-js\";\n\nimport { useAudioConfig } from \"@/hooks/use-audio-config\";\nimport type { AudioSize } from \"@/hooks/use-audio-config\";\nimport { useTapTempo } from \"@/hooks/use-tap-tempo\";\nimport type { TempoChangeDetails } from \"@/hooks/use-tap-tempo\";\nimport { clamp } from \"@/lib/audio/decibels\";\nimport { provideContext } from \"@/lib/solid/context\";\nimport { createCompatEffect } from \"@/lib/solid/effect\";\nimport type {\n  DivDOMProps,\n  InputDOMProps,\n  ButtonDOMProps,\n} from \"@/lib/solid/jsx-types\";\nimport { omitProps } from \"@/lib/solid/props\";\nimport { cn } from \"@/lib/utils\";\n\ninterface TempoContextValue {\n  disabled: () => boolean;\n  readOnly: () => boolean;\n  resetTaps: () => void;\n  tap: (event?: Event) => void;\n  setValue: (next: number, details?: TempoChangeDetails) => void;\n  min: () => number;\n  max: () => number;\n  value: () => number;\n  name: () => string | undefined;\n  largeStep: () => number;\n  tapRevision: () => number;\n  commit: (next: number) => void;\n}\n\nconst TempoContext = createContext<TempoContextValue>();\n\nconst useTempo = () => {\n  const context = useContext(TempoContext);\n\n  if (!context)\n    throw new Error(\"TempoControl part must be used inside TempoControl.\");\n\n  return context;\n};\n\nexport const tempoControlVariants = cva(\n  \"group/tempo-control inline-flex items-center gap-2 data-disabled:opacity-50\",\n  {\n    variants: {\n      size: {\n        default: \"[--tempo-height:--spacing(8)]\",\n        lg: \"[--tempo-height:--spacing(9)]\",\n        sm: \"[--tempo-height:--spacing(7)]\",\n      },\n    },\n    defaultVariants: { size: \"default\" },\n  }\n);\n\nexport interface TempoControlProps extends Omit<DivDOMProps, \"onChange\"> {\n  value?: number;\n  defaultValue?: number;\n  onValueChange?: (value: number, details: TempoChangeDetails) => void;\n  onValueCommitted?: (value: number) => void;\n  min?: number;\n  max?: number;\n  largeStep?: number;\n  tapTimeout?: number;\n  size?: AudioSize;\n  disabled?: boolean;\n  readOnly?: boolean;\n  name?: string;\n}\n\nexport const TempoControlInput = (\n  props: InputDOMProps & { className?: string }\n) => {\n  const context = useTempo();\n\n  const rest = omitProps(props, [\n    \"class\",\n    \"className\",\n    \"onInput\",\n    \"onChange\",\n    \"onBlur\",\n    \"onKeyDown\",\n    \"value\",\n    \"name\",\n  ]);\n\n  const [draft, setDraft] = createSignal<{\n    text: string;\n    value: number;\n    previousValue: number;\n    revision: number;\n  } | null>(null);\n\n  createCompatEffect(\n    () => [context.value(), context.tapRevision()] as const,\n    ([value, revision]) => {\n      const current = draft();\n\n      if (\n        current &&\n        ((current.value !== value && current.previousValue !== value) ||\n          current.revision !== revision)\n      ) {\n        setDraft(null);\n      }\n    }\n  );\n\n  const displayValue = () => {\n    const current = draft();\n\n    return current &&\n      (current.value === context.value() ||\n        current.previousValue === context.value()) &&\n      current.revision === context.tapRevision()\n      ? current.text\n      : String(context.value());\n  };\n\n  const edit = (event: Event & { currentTarget: HTMLInputElement }) => {\n    if (event.defaultPrevented || context.disabled() || context.readOnly())\n      return;\n    const text = event.currentTarget.value;\n    const next = text.trim() === \"\" ? Number.NaN : Number(text);\n    const previousValue = context.value();\n\n    if (Number.isFinite(next)) {\n      context.setValue(next, { reason: \"input\", event });\n    } else {\n      context.resetTaps();\n    }\n\n    setDraft({\n      text,\n      // Controlled writes settle after the event in Solid 2. Record the\n      // requested value and allow the previous value if the parent rejects it.\n      value: Number.isFinite(next)\n        ? clamp(Math.round(next), context.min(), context.max())\n        : context.value(),\n      previousValue,\n      revision: context.tapRevision(),\n    });\n  };\n\n  const finish = (event: Event & { currentTarget: HTMLInputElement }) => {\n    if (context.disabled() || context.readOnly()) return;\n    const current = draft();\n\n    const valid =\n      current !== null &&\n      current.text.trim() !== \"\" &&\n      Number.isFinite(Number(current.text));\n\n    setDraft(null);\n    event.currentTarget.value = String(context.value());\n\n    if (valid) context.commit(context.value());\n  };\n\n  return (\n    <div\n      class=\"bg-input/50 focus-within:ring-ring/30 flex h-(--tempo-height) items-center rounded-lg focus-within:ring-3\"\n      data-slot=\"tempo-control-input-group\"\n    >\n      <input\n        aria-label=\"Tempo in beats per minute\"\n        class={cn(\n          \"h-full w-16 bg-transparent px-2 text-end font-mono text-sm tabular-nums outline-hidden\",\n          props.class,\n          props.className\n        )}\n        data-slot=\"tempo-control-input\"\n        disabled={context.disabled()}\n        inputmode=\"decimal\"\n        aria-valuemin={context.min()}\n        aria-valuemax={context.max()}\n        aria-valuenow={context.value()}\n        max={context.max()}\n        min={context.min()}\n        name={props.name ?? context.name()}\n        value={displayValue()}\n        onInput={(event) => {\n          const handler = props.onInput;\n\n          if (handler && \"call\" in handler) handler(event);\n          else if (handler) handler[0](handler[1], event);\n\n          edit(event);\n        }}\n        onChange={(event) => {\n          const handler = props.onChange;\n\n          if (handler && \"call\" in handler) handler(event);\n          else if (handler) handler[0](handler[1], event);\n\n          if (draft()?.text !== event.currentTarget.value) edit(event);\n        }}\n        onBlur={(event) => {\n          const handler = props.onBlur;\n\n          if (handler && \"call\" in handler) handler(event);\n          else if (handler) handler[0](handler[1], event);\n\n          if (!event.defaultPrevented) finish(event);\n        }}\n        onKeyDown={(event) => {\n          const handler = props.onKeyDown;\n\n          if (handler && \"call\" in handler) handler(event);\n          else if (handler) handler[0](handler[1], event);\n\n          if (\n            event.defaultPrevented ||\n            context.disabled() ||\n            context.readOnly()\n          )\n            return;\n\n          if (event.key === \"Enter\") {\n            finish(event);\n\n            return;\n          }\n\n          let next: number;\n          const step = event.shiftKey ? context.largeStep() : 1;\n\n          switch (event.key) {\n            case \"ArrowUp\":\n              next = context.value() + step;\n              break;\n            case \"ArrowDown\":\n              next = context.value() - step;\n              break;\n            case \"Home\":\n              next = context.min();\n              break;\n            case \"End\":\n              next = context.max();\n              break;\n            default:\n              return;\n          }\n\n          event.preventDefault();\n          context.setValue(next, { event, reason: \"keyboard\" });\n          setDraft(null);\n          event.currentTarget.value = String(context.value());\n          context.commit(context.value());\n        }}\n        readonly={context.readOnly()}\n        step={1}\n        type=\"text\"\n        role=\"spinbutton\"\n        {...rest}\n      />\n      <span\n        aria-hidden=\"true\"\n        class=\"text-muted-foreground pr-2 text-xs\"\n        data-slot=\"tempo-control-unit\"\n      >\n        BPM\n      </span>\n    </div>\n  );\n};\n\ntype TempoTapDOMProps = Omit<ButtonDOMProps, \"onClick\" | \"onKeyDown\"> & {\n  onClick?: (event: MouseEvent) => void;\n  onKeyDown?: (event: KeyboardEvent) => void;\n};\n\nexport interface TempoControlTapProps extends TempoTapDOMProps {\n  className?: string;\n  as?: Component<TempoTapDOMProps>;\n}\n\nexport const TempoControlTap = (props: TempoControlTapProps) => {\n  const context = useTempo();\n\n  const rest = omitProps(props, [\n    \"as\",\n    \"class\",\n    \"className\",\n    \"children\",\n    \"disabled\",\n    \"onClick\",\n    \"onKeyDown\",\n  ]);\n\n  const TapButton =\n    props.as ??\n    ((buttonProps: TempoTapDOMProps) => <button {...buttonProps} />);\n\n  return (\n    <TapButton\n      aria-label={props[\"aria-label\"] ?? \"Tap tempo\"}\n      class={cn(\n        \"border-border bg-background hover:bg-muted inline-flex h-(--tempo-height) shrink-0 touch-manipulation items-center justify-center rounded-lg border px-3 text-sm font-medium outline-none focus-visible:ring-3 disabled:pointer-events-none disabled:opacity-50\",\n        props.class,\n        props.className\n      )}\n      data-slot=\"tempo-control-tap\"\n      disabled={context.disabled() || context.readOnly() || props.disabled}\n      onClick={(event: MouseEvent) => {\n        props.onClick?.(event);\n\n        if (!event.defaultPrevented) context.tap(event);\n      }}\n      onKeyDown={(event: KeyboardEvent) => {\n        props.onKeyDown?.(event);\n\n        if (event.repeat && (event.key === \"Enter\" || event.key === \" \"))\n          event.preventDefault();\n      }}\n      type=\"button\"\n      {...rest}\n    >\n      {props.children ?? \"Tap\"}\n    </TapButton>\n  );\n};\n\nexport const TempoControl = (\n  props: TempoControlProps & { className?: string }\n) => {\n  const config = useAudioConfig();\n  const [tapRevision, setTapRevision] = createSignal(0);\n  const disabled = () => props.disabled ?? config.disabled ?? false;\n  const size = () => props.size ?? config.size ?? \"default\";\n  const min = () => Math.ceil(props.min ?? 30);\n  const max = () => Math.floor(props.max ?? 600);\n\n  const tempo = useTapTempo({\n    get value() {\n      return props.value;\n    },\n    get defaultValue() {\n      return props.defaultValue;\n    },\n    get min() {\n      return min();\n    },\n    get max() {\n      return max();\n    },\n    get tapTimeout() {\n      return props.tapTimeout;\n    },\n    get disabled() {\n      return disabled();\n    },\n    get readOnly() {\n      return props.readOnly;\n    },\n    get onValueChange() {\n      return props.onValueChange;\n    },\n    get onValueCommitted() {\n      return (next: number) => {\n        setTapRevision((revision) => revision + 1);\n        props.onValueCommitted?.(next);\n      };\n    },\n  });\n\n  const context: TempoContextValue = {\n    disabled,\n    readOnly: () => props.readOnly ?? false,\n    resetTaps: tempo.resetTaps,\n    tap: tempo.tap,\n    tapRevision,\n    setValue: tempo.setValue,\n    min,\n    max,\n    value: tempo.value,\n    name: () => props.name,\n    largeStep: () => Math.max(1, Math.round(props.largeStep ?? 10)),\n    commit: (next) => props.onValueCommitted?.(next),\n  };\n\n  const rest = omitProps(props, [\n    \"value\",\n    \"defaultValue\",\n    \"onValueChange\",\n    \"onValueCommitted\",\n    \"min\",\n    \"max\",\n    \"largeStep\",\n    \"tapTimeout\",\n    \"size\",\n    \"disabled\",\n    \"readOnly\",\n    \"name\",\n    \"class\",\n    \"className\",\n    \"children\",\n  ]);\n\n  return provideContext(TempoContext, context, () => (\n    <div\n      aria-label=\"Tempo\"\n      class={cn(\n        tempoControlVariants({ size: size() }),\n        props.class,\n        props.className\n      )}\n      data-disabled={disabled() ? \"\" : undefined}\n      data-readonly={props.readOnly ? \"\" : undefined}\n      data-size={size()}\n      data-slot=\"tempo-control\"\n      role=\"group\"\n      {...rest}\n    >\n      {props.children ?? (\n        <>\n          <TempoControlInput />\n          <TempoControlTap />\n        </>\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"
}
