{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ui-odometer",
  "title": "Odometer stepper",
  "description": "Number stepper where every digit is its own rolling wheel; supports press-and-hold, sideways drag, the scroll wheel and the keyboard.",
  "registryDependencies": [
    "https://elements.lumesec.ai/r/lumesec-theme.json",
    "https://elements.lumesec.ai/r/lumesec-math.json",
    "https://elements.lumesec.ai/r/lumesec-motion.json",
    "https://elements.lumesec.ai/r/lumesec-use-controllable-state.json",
    "utils"
  ],
  "files": [
    {
      "path": "registry/components/lumesec/ui-odometer.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { clamp } from \"@/lib/lumesec/math\";\nimport { prefersReducedMotion } from \"@/lib/lumesec/motion\";\nimport { useControllableState } from \"@/lib/lumesec/use-controllable-state\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface UiOdometerProps extends Omit<React.ComponentProps<\"div\">, \"onChange\" | \"defaultValue\"> {\n  /** Controlled value, rounded to the nearest multiple of `step` and clamped to `min`/`max`. */\n  value?: number;\n  /** Initial value when uncontrolled. */\n  defaultValue?: number;\n  /** Lowest value. */\n  min?: number;\n  /** Highest value. Also sets the number of digit wheels. A `max` below `min` is treated as `min`. */\n  max?: number;\n  /** Increment per step, greater than 0 (anything else falls back to 1). */\n  step?: number;\n  /** Caption above the number and the spinbutton's accessible name. */\n  label?: string;\n  /** Optional monospace caption under the number. */\n  unit?: string;\n  /** Called on every value change from the buttons, drag, wheel or keys. */\n  onValueChange?: (value: number) => void;\n  /**\n   * Called with each change from the buttons, wheel or keys, and once when a drag on the number ends,\n   * even if the value did not change.\n   */\n  onValueCommit?: (value: number) => void;\n}\n\n/** Steps per key press. */\nconst KEY_STEPS = new Map<string, number>([\n  [\"ArrowUp\", 1],\n  [\"ArrowRight\", 1],\n  [\"ArrowDown\", -1],\n  [\"ArrowLeft\", -1],\n  [\"PageUp\", 10],\n  [\"PageDown\", -10],\n]);\n\nconst STEP_BUTTON_CLASS =\n  \"grid size-[34px] cursor-pointer place-items-center rounded-[9px] border border-border bg-card p-0 font-[inherit] text-foreground outline-none select-none [transition:transform_.15s_cubic-bezier(.2,.8,.2,1),opacity_.2s] hover:bg-muted active:[transform:scale(.88)] aria-disabled:opacity-35 focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-lumesec\";\n\nconst WIDTH_TRANSITION = \"[transition:width_.35s_cubic-bezier(.2,.8,.2,1),opacity_.25s] motion-reduce:transition-none\";\n\n/**\n * Number stepper where every digit is its own rolling wheel; supports press-and-hold, sideways drag, the\n * scroll wheel and the keyboard. Built for non-negative whole numbers, grouped with commas.\n *\n * @example\n * <UiOdometer defaultValue={8192} min={1024} max={65536} step={1024} label=\"Max output tokens\" />\n */\nexport function UiOdometer({\n  value: valueProp,\n  defaultValue = 8192,\n  min = 1024,\n  max: maxProp = 65536,\n  step: stepProp = 1024,\n  label = \"Max output tokens\",\n  unit,\n  onValueChange,\n  onValueCommit,\n  className,\n  ...props\n}: UiOdometerProps) {\n  // an inverted range or a step of 0 would break the rounding below\n  const max = Math.max(min, maxProp);\n  const step = stepProp > 0 ? stepProp : 1;\n  const [rawValue, setValue] = useControllableState({ value: valueProp, defaultValue, onChange: onValueChange });\n  const value = normalize(rawValue, min, max, step);\n\n  const numberRef = React.useRef<HTMLDivElement>(null);\n  const current = React.useRef(value);\n  const drag = React.useRef<{ x: number; value: number } | null>(null);\n  const repeatTimer = React.useRef(0);\n  const latest = React.useRef({ min, max, step, onValueCommit });\n  React.useEffect(() => {\n    current.current = value;\n    latest.current = { min, max, step, onValueCommit };\n  });\n\n  /** Apply a new value from user input. */\n  const update = React.useCallback(\n    (raw: number) => {\n      const { min: low, max: high, step: size, onValueCommit: commit } = latest.current;\n      const next = normalize(raw, low, high, size);\n      const changed = next !== current.current;\n      current.current = next;\n      setValue(next);\n      if (changed && !drag.current) commit?.(next);\n    },\n    [setValue],\n  );\n\n  /** Move by `steps`; pushing past a limit nudges the number and lands on the limit. */\n  const bump = React.useCallback(\n    (steps: number) => {\n      const { min: low, max: high, step: size } = latest.current;\n      const next = current.current + steps * size;\n      if (next > high + 1e-9 || next < low - 1e-9) {\n        if (!prefersReducedMotion()) {\n          const sign = steps > 0 ? 1 : -1;\n          numberRef.current?.animate(\n            [\n              { transform: \"translateX(0)\" },\n              { transform: `translateX(${sign * 6}px)` },\n              { transform: `translateX(${-sign * 2}px)` },\n              { transform: \"translateX(0)\" },\n            ],\n            { duration: 300, easing: \"ease-out\" },\n          );\n        }\n        update(steps > 0 ? high : low);\n        return;\n      }\n      update(next);\n    },\n    [update],\n  );\n\n  // the wheel must not scroll the page, which needs a non-passive listener\n  React.useEffect(() => {\n    const element = numberRef.current;\n    if (!element) return;\n    const onWheel = (event: WheelEvent) => {\n      // a sideways scroll (deltaY 0) is not a step and leaves the page alone\n      if (event.deltaY === 0) return;\n      event.preventDefault();\n      bump(event.deltaY < 0 ? 1 : -1);\n    };\n    element.addEventListener(\"wheel\", onWheel, { passive: false });\n    return () => element.removeEventListener(\"wheel\", onWheel);\n  }, [bump]);\n\n  React.useEffect(() => () => window.clearTimeout(repeatTimer.current), []);\n\n  const stopRepeat = () => window.clearTimeout(repeatTimer.current);\n\n  /** Press and hold: one step now, repeats after 380 ms, speeding up; after 14 repeats, four steps at a time. */\n  const startRepeat = (event: React.PointerEvent<HTMLButtonElement>, direction: 1 | -1) => {\n    if (event.button !== 0) return;\n    event.preventDefault();\n    bump(direction);\n    let repeats = 0;\n    const repeat = () => {\n      repeats++;\n      bump(direction * (repeats > 14 ? 4 : 1));\n      repeatTimer.current = window.setTimeout(repeat, Math.max(45, 110 - repeats * 6));\n    };\n    stopRepeat();\n    repeatTimer.current = window.setTimeout(repeat, 380);\n  };\n\n  // keyboard activation (Enter / Space) arrives as a click with no pointer\n  const stepByKey = (event: React.MouseEvent<HTMLButtonElement>, direction: 1 | -1) => {\n    if (event.detail === 0) bump(direction);\n  };\n\n  const endDrag = () => {\n    if (!drag.current) return;\n    drag.current = null;\n    latest.current.onValueCommit?.(current.current);\n  };\n\n  const text = String(Math.round(value));\n  const columns = String(Math.round(max)).length;\n  const wheels: React.ReactNode[] = [];\n  for (let place = columns - 1; place >= 0; place--) {\n    const on = place < text.length;\n    const digit = on ? Number(text[text.length - 1 - place]) : 0;\n    wheels.push(\n      <span\n        key={`digit-${place}`}\n        aria-hidden=\"true\"\n        className={cn(\"h-[1.3em] w-[.62em] overflow-hidden\", WIDTH_TRANSITION, !on && \"w-0 opacity-0\")}\n      >\n        <span\n          className=\"flex flex-col [transition:transform_.6s_cubic-bezier(.3,1.25,.45,1)] motion-reduce:transition-none\"\n          // higher places start a little later, so a carry ripples leftward\n          style={{ transform: `translateY(${-digit * 1.3}em)`, transitionDelay: `${place * 35}ms` }}\n        >\n          {DIGITS.map((d) => (\n            <span key={d} className=\"h-[1.3em] text-center leading-[1.3em]\">\n              {d}\n            </span>\n          ))}\n        </span>\n      </span>,\n    );\n    if (place > 0 && place % 3 === 0) {\n      wheels.push(\n        <span\n          key={`sep-${place}`}\n          aria-hidden=\"true\"\n          className={cn(\"w-[.3em] overflow-hidden text-center\", WIDTH_TRANSITION, !on && \"w-0 opacity-0\")}\n        >\n          ,\n        </span>,\n      );\n    }\n  }\n\n  return (\n    <div\n      data-slot=\"ui-odometer\"\n      className={cn(\"inline-grid justify-items-center gap-2.5 leading-normal\", className)}\n      {...props}\n    >\n      <span className=\"text-[12.5px] text-muted-foreground\">{label}</span>\n      <div className=\"flex items-center gap-3\">\n        <button\n          type=\"button\"\n          aria-label=\"Decrease\"\n          aria-disabled={value <= min}\n          className={STEP_BUTTON_CLASS}\n          onPointerDown={(event) => startRepeat(event, -1)}\n          onPointerUp={stopRepeat}\n          onPointerLeave={stopRepeat}\n          onPointerCancel={stopRepeat}\n          onClick={(event) => stepByKey(event, -1)}\n        >\n          <svg\n            viewBox=\"0 0 14 14\"\n            aria-hidden=\"true\"\n            className=\"size-3.5 fill-none stroke-current [stroke-width:2] [stroke-linecap:round]\"\n          >\n            <path d=\"M3 7h8\" />\n          </svg>\n        </button>\n        <div\n          ref={numberRef}\n          role=\"spinbutton\"\n          tabIndex={0}\n          aria-valuenow={value}\n          // the wheels are drawn digit strips, hidden from assistive technology; this is the number they show\n          aria-valuetext={Math.round(value).toLocaleString(\"en-US\")}\n          aria-valuemin={min}\n          aria-valuemax={max}\n          aria-label={label}\n          className=\"flex h-[1.3em] cursor-ew-resize touch-none items-center rounded-lg [mask-image:linear-gradient(transparent,#000_14%,#000_86%,transparent)] px-1.5 text-[32px] font-semibold tracking-[-.01em] text-foreground tabular-nums outline-none select-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-lumesec focus-visible:outline-solid\"\n          onPointerDown={(event) => {\n            if (event.button !== 0) return;\n            event.currentTarget.setPointerCapture(event.pointerId);\n            drag.current = { x: event.clientX, value: current.current };\n          }}\n          onPointerMove={(event) => {\n            const start = drag.current;\n            if (!start) return;\n            // one step per 12 px sideways\n            const steps = Math.round((event.clientX - start.x) / 12);\n            update(start.value + steps * latest.current.step);\n          }}\n          onPointerUp={endDrag}\n          onPointerCancel={endDrag}\n          onKeyDown={(event) => {\n            const steps = KEY_STEPS.get(event.key);\n            if (steps !== undefined) {\n              event.preventDefault();\n              bump(steps);\n            } else if (event.key === \"Home\" || event.key === \"End\") {\n              event.preventDefault();\n              update(event.key === \"Home\" ? min : max);\n            }\n          }}\n        >\n          {wheels}\n        </div>\n        <button\n          type=\"button\"\n          aria-label=\"Increase\"\n          aria-disabled={value >= max}\n          className={STEP_BUTTON_CLASS}\n          onPointerDown={(event) => startRepeat(event, 1)}\n          onPointerUp={stopRepeat}\n          onPointerLeave={stopRepeat}\n          onPointerCancel={stopRepeat}\n          onClick={(event) => stepByKey(event, 1)}\n        >\n          <svg\n            viewBox=\"0 0 14 14\"\n            aria-hidden=\"true\"\n            className=\"size-3.5 fill-none stroke-current [stroke-width:2] [stroke-linecap:round]\"\n          >\n            <path d=\"M3 7h8M7 3v8\" />\n          </svg>\n        </button>\n      </div>\n      <span hidden={!unit} className=\"font-mono text-[12px] text-muted-foreground\">\n        {unit}\n      </span>\n    </div>\n  );\n}\n\nconst DIGITS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] as const;\n\n/** Round `raw` to the nearest multiple of `step` and clamp it to the range. */\nfunction normalize(raw: number, min: number, max: number, step: number) {\n  return clamp(Math.round(raw / step) * step, min, max);\n}\n",
      "type": "registry:component",
      "target": "@components/lumesec/ui-odometer.tsx"
    }
  ],
  "meta": {
    "tag": "ui-odometer",
    "collection": "interaction",
    "react": "UiOdometer",
    "kind": "react"
  },
  "docs": "Odometer stepper docs: https://elements.lumesec.ai/components/interaction/odometer",
  "categories": [
    "interaction",
    "data"
  ],
  "type": "registry:component"
}