{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tick-ruler",
  "title": "Tick Ruler",
  "description": "A ruler-style number picker you drag or flick, with a click and a haptic tick on every step.",
  "dependencies": [
    "@rexa-developer/tiks",
    "motion",
    "web-haptics"
  ],
  "files": [
    {
      "path": "src/registry/components/tick-ruler/tick-ruler.tsx",
      "content": "\"use client\";\n\nimport { useTiks } from \"@rexa-developer/tiks/react\";\nimport {\n\tAnimatePresence,\n\ttype AnimationPlaybackControls,\n\tanimate,\n\tmotion,\n\tuseMotionValue,\n\tuseReducedMotion,\n} from \"motion/react\";\nimport {\n\ttype KeyboardEvent,\n\tmemo,\n\ttype PointerEvent,\n\tuseCallback,\n\tuseEffect,\n\tuseLayoutEffect,\n\tuseRef,\n\tuseState,\n} from \"react\";\nimport { useWebHaptics } from \"web-haptics/react\";\nimport { cn } from \"@/lib/utils\";\n\nconst landingSpring = { type: \"spring\", stiffness: 420, damping: 38 } as const;\nconst digitTransition = { duration: 0.18, ease: [0.23, 1, 0.32, 1] } as const;\n\n/** Seconds of release velocity that a flick coasts before it snaps. */\nconst COAST_SECONDS = 0.25;\n/** Fastest release, in px/s, so one jumpy pointer event can't fling the range. */\nconst MAX_VELOCITY = 4000;\n/** Furthest the strip travels past a bound, in px. */\nconst MAX_OVERSHOOT = 40;\n/** Minimum ms between step sounds and haptics. */\nconst TICK_THROTTLE_MS = 35;\n/** Pointer travel, in px, below which a press counts as a tap. */\nconst TAP_SLOP = 5;\n/** Ranges with more ticks than this render only the ticks near the needle. */\nconst WINDOW_THRESHOLD = 400;\n\nconst sizes = {\n\tsm: { spacing: 8, track: \"h-14\", readout: \"text-3xl\", unit: \"text-sm\" },\n\tmd: { spacing: 10, track: \"h-16\", readout: \"text-5xl\", unit: \"text-base\" },\n\tlg: { spacing: 12, track: \"h-20\", readout: \"text-6xl\", unit: \"text-lg\" },\n} as const;\n\ntype Orientation = \"horizontal\" | \"vertical\";\ntype RulerState = \"idle\" | \"dragging\" | \"coasting\";\n\nexport interface TickRulerProps {\n\t/** Controlled value. */\n\tvalue?: number;\n\t/** Initial value when uncontrolled; double-click resets to it. */\n\tdefaultValue?: number;\n\t/** Called every time the needle crosses onto a new step. */\n\tonValueChange?: (value: number) => void;\n\t/** Called once the ruler settles after a drag, flick, key, wheel, or preset. */\n\tonValueCommit?: (value: number) => void;\n\tmin?: number;\n\tmax?: number;\n\tstep?: number;\n\t/** Steps between medium ticks. */\n\tmajorEvery?: number;\n\t/** Steps between labelled ticks. */\n\tlabelEvery?: number;\n\t/** Pixels between ticks. Defaults by `size`. */\n\tspacing?: number;\n\t/** Unit shown after the readout, e.g. `min`. */\n\tunit?: string;\n\t/** Formats the readout and `aria-valuetext`. */\n\tformat?: (value: number) => string;\n\t/** Values shown as chips that glide the ruler to them. */\n\tpresets?: number[];\n\t/** Play a click per step and a pop at the bounds. */\n\tsound?: boolean;\n\t/** Trigger haptics on supported devices. */\n\thaptics?: boolean;\n\tdisabled?: boolean;\n\t/** Accessible name for the slider. */\n\tlabel?: string;\n\tclassName?: string;\n\tsize?: keyof typeof sizes;\n\torientation?: Orientation;\n\t/** Show the large value readout above the ruler. */\n\tshowValue?: boolean;\n}\n\nfunction decimalsOf(value: number) {\n\tconst text = String(value);\n\tif (text.includes(\"e-\")) return Number(text.split(\"e-\")[1]);\n\treturn text.split(\".\")[1]?.length ?? 0;\n}\n\nfunction clamp(value: number, min: number, max: number) {\n\treturn Math.min(Math.max(value, min), max);\n}\n\n/** Pulls travel past a bound back so it never exceeds `MAX_OVERSHOOT`. */\nfunction rubberBand(position: number, maxPosition: number) {\n\tif (position < 0) {\n\t\tconst over = -position;\n\t\treturn -(MAX_OVERSHOOT * over) / (over + MAX_OVERSHOOT);\n\t}\n\tif (position > maxPosition) {\n\t\tconst over = position - maxPosition;\n\t\treturn maxPosition + (MAX_OVERSHOOT * over) / (over + MAX_OVERSHOOT);\n\t}\n\treturn position;\n}\n\ntype DigitSlot = { char: string; id: number };\n\nlet nextDigitId = 0;\n\n/** Aligns `text` to `previous` from the right, keeping ids of unchanged chars. */\nfunction toSlots(text: string, previous: DigitSlot[]): DigitSlot[] {\n\tconst chars = text.split(\"\");\n\treturn chars.map((char, index) => {\n\t\tconst match = previous[previous.length - chars.length + index];\n\t\tif (match?.char === char) return match;\n\t\tnextDigitId += 1;\n\t\treturn { char, id: nextDigitId };\n\t});\n}\n\nfunction RollingValue({\n\ttext,\n\tdirection,\n\tanimated,\n}: {\n\ttext: string;\n\tdirection: number;\n\tanimated: boolean;\n}) {\n\t// Each slot gets a fresh key whenever its character changes. Reusing a key\n\t// while its previous copy is still exiting can leave the digit hidden.\n\tconst [slots, setSlots] = useState(() => toSlots(text, []));\n\tconst [previousText, setPreviousText] = useState(text);\n\tif (text !== previousText) {\n\t\tsetPreviousText(text);\n\t\tsetSlots((previous) => toSlots(text, previous));\n\t}\n\n\tif (!animated) return <span>{text}</span>;\n\n\treturn (\n\t\t<span className=\"inline-flex\">\n\t\t\t{slots.map(({ char, id }, index) => (\n\t\t\t\t// Keyed from the right so units digits stay put as the width changes.\n\t\t\t\t<span key={slots.length - index} className=\"relative inline-block\">\n\t\t\t\t\t<AnimatePresence mode=\"popLayout\" initial={false} custom={direction}>\n\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\tkey={id}\n\t\t\t\t\t\t\tcustom={direction}\n\t\t\t\t\t\t\tclassName=\"inline-block\"\n\t\t\t\t\t\t\tvariants={{\n\t\t\t\t\t\t\t\tenter: (d: number) => ({ y: d * 12, opacity: 0 }),\n\t\t\t\t\t\t\t\tcenter: { y: 0, opacity: 1 },\n\t\t\t\t\t\t\t\texit: (d: number) => ({ y: d * -12, opacity: 0 }),\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\tinitial=\"enter\"\n\t\t\t\t\t\t\tanimate=\"center\"\n\t\t\t\t\t\t\texit=\"exit\"\n\t\t\t\t\t\t\ttransition={digitTransition}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{char}\n\t\t\t\t\t\t</motion.span>\n\t\t\t\t\t</AnimatePresence>\n\t\t\t\t</span>\n\t\t\t))}\n\t\t</span>\n\t);\n}\n\nconst TickStrip = memo(function TickStrip({\n\tstart,\n\tend,\n\tspacing,\n\tsign,\n\torientation,\n\tmajorEvery,\n\tlabelEvery,\n\tlabelAt,\n}: {\n\tstart: number;\n\tend: number;\n\tspacing: number;\n\tsign: number;\n\torientation: Orientation;\n\tmajorEvery: number;\n\tlabelEvery: number;\n\tlabelAt: (index: number) => string;\n}) {\n\tconst horizontal = orientation === \"horizontal\";\n\tconst ticks = [];\n\tfor (let index = start; index <= end; index++) {\n\t\tconst kind =\n\t\t\tindex % labelEvery === 0\n\t\t\t\t? \"label\"\n\t\t\t\t: index % majorEvery === 0\n\t\t\t\t\t? \"major\"\n\t\t\t\t\t: \"minor\";\n\t\tconst offset = sign * index * spacing;\n\t\tticks.push(\n\t\t\t<span\n\t\t\t\tkey={index}\n\t\t\t\tdata-kind={kind}\n\t\t\t\tclassName={cn(\n\t\t\t\t\t\"absolute flex items-center gap-1.5\",\n\t\t\t\t\thorizontal\n\t\t\t\t\t\t? \"top-0 -translate-x-1/2 flex-col\"\n\t\t\t\t\t\t: \"left-0 -translate-y-1/2 flex-row\",\n\t\t\t\t)}\n\t\t\t\tstyle={horizontal ? { left: offset } : { top: offset }}\n\t\t\t>\n\t\t\t\t<span\n\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\"rounded-full\",\n\t\t\t\t\t\thorizontal ? \"w-px\" : \"h-px\",\n\t\t\t\t\t\tkind === \"minor\" && \"bg-muted-foreground/35\",\n\t\t\t\t\t\tkind === \"minor\" && (horizontal ? \"h-3\" : \"w-3\"),\n\t\t\t\t\t\tkind === \"major\" && \"bg-muted-foreground/60\",\n\t\t\t\t\t\tkind === \"major\" && (horizontal ? \"h-5\" : \"w-5\"),\n\t\t\t\t\t\tkind === \"label\" && \"bg-foreground/80\",\n\t\t\t\t\t\tkind === \"label\" &&\n\t\t\t\t\t\t\t(horizontal ? \"h-7 w-[1.5px]\" : \"h-[1.5px] w-7\"),\n\t\t\t\t\t)}\n\t\t\t\t/>\n\t\t\t\t{kind === \"label\" ? (\n\t\t\t\t\t<span className=\"font-medium text-[11px] text-muted-foreground tabular-nums leading-none\">\n\t\t\t\t\t\t{labelAt(index)}\n\t\t\t\t\t</span>\n\t\t\t\t) : null}\n\t\t\t</span>,\n\t\t);\n\t}\n\treturn <>{ticks}</>;\n});\n\nexport function TickRuler({\n\tvalue: valueProp,\n\tdefaultValue,\n\tonValueChange,\n\tonValueCommit,\n\tmin = 0,\n\tmax = 100,\n\tstep = 1,\n\tmajorEvery = 5,\n\tlabelEvery = 10,\n\tspacing: spacingProp,\n\tunit,\n\tformat,\n\tpresets,\n\tsound = true,\n\thaptics = true,\n\tdisabled = false,\n\tlabel = \"Value\",\n\tclassName,\n\tsize = \"md\",\n\torientation = \"horizontal\",\n\tshowValue = true,\n}: TickRulerProps) {\n\tconst sizeStyle = sizes[size];\n\tconst spacing = spacingProp ?? sizeStyle.spacing;\n\tconst horizontal = orientation === \"horizontal\";\n\tconst decimals = Math.max(decimalsOf(step), decimalsOf(min));\n\tconst maxIndex = Math.max(0, Math.round((max - min) / step));\n\tconst maxPosition = maxIndex * spacing;\n\n\tconst indexToValue = useCallback(\n\t\t(index: number) => Number((min + index * step).toFixed(decimals)),\n\t\t[min, step, decimals],\n\t);\n\tconst valueToIndex = useCallback(\n\t\t(value: number) => clamp(Math.round((value - min) / step), 0, maxIndex),\n\t\t[min, step, maxIndex],\n\t);\n\tconst formatValue = useCallback(\n\t\t(value: number) => (format ? format(value) : value.toFixed(decimals)),\n\t\t[format, decimals],\n\t);\n\n\tconst initialIndex = valueToIndex(valueProp ?? defaultValue ?? min);\n\tconst [currentIndex, setCurrentIndex] = useState(initialIndex);\n\tconst [direction, setDirection] = useState(1);\n\tconst [sign, setSign] = useState(horizontal ? 1 : -1);\n\tconst [range, setRange] = useState(() =>\n\t\tmaxIndex + 1 <= WINDOW_THRESHOLD\n\t\t\t? { start: 0, end: maxIndex }\n\t\t\t: {\n\t\t\t\t\tstart: Math.max(0, initialIndex - 60),\n\t\t\t\t\tend: Math.min(maxIndex, initialIndex + 60),\n\t\t\t\t},\n\t);\n\n\tconst shouldReduceMotion = useReducedMotion() ?? false;\n\tconst { trigger: haptic } = useWebHaptics();\n\tconst { click: playClick, pop: playPop } = useTiks({\n\t\t// Sound stays on under reduced motion; only movement is reduced.\n\t\trespectReducedMotion: false,\n\t});\n\n\tconst rootRef = useRef<HTMLDivElement>(null);\n\tconst trackRef = useRef<HTMLDivElement>(null);\n\tconst position = useMotionValue(initialIndex * spacing);\n\tconst stripOffset = useMotionValue(0);\n\tconst centerRef = useRef(0);\n\tconst indexRef = useRef(initialIndex);\n\tconst animationRef = useRef<AnimationPlaybackControls | null>(null);\n\tconst silentRef = useRef(false);\n\tconst interactingRef = useRef(false);\n\tconst lastTickAtRef = useRef(Number.NEGATIVE_INFINITY);\n\tconst chunkRef = useRef(-1);\n\tconst wheelRef = useRef({ accumulated: 0, target: initialIndex });\n\tconst dragRef = useRef<{\n\t\tpointerId: number;\n\t\tstart: number;\n\t\tstartPosition: number;\n\t\tmoved: boolean;\n\t\tscale: number;\n\t} | null>(null);\n\n\t// Latest values for the imperative listeners below, so they never re-bind.\n\tconst latest = useRef({\n\t\tsound,\n\t\thaptics,\n\t\tonValueChange,\n\t\tonValueCommit,\n\t\tindexToValue,\n\t\tmaxIndex,\n\t\tmaxPosition,\n\t\tspacing,\n\t\tsign,\n\t\tlabelEvery,\n\t});\n\tuseLayoutEffect(() => {\n\t\tlatest.current = {\n\t\t\tsound,\n\t\t\thaptics,\n\t\t\tonValueChange,\n\t\t\tonValueCommit,\n\t\t\tindexToValue,\n\t\t\tmaxIndex,\n\t\t\tmaxPosition,\n\t\t\tspacing,\n\t\t\tsign,\n\t\t\tlabelEvery,\n\t\t};\n\t});\n\n\tconst setRulerState = useCallback((state: RulerState) => {\n\t\tif (rootRef.current) rootRef.current.dataset.state = state;\n\t}, []);\n\n\tconst updateRange = useCallback((pos: number) => {\n\t\tconst { maxIndex, spacing } = latest.current;\n\t\tif (maxIndex + 1 <= WINDOW_THRESHOLD) {\n\t\t\tif (chunkRef.current !== 0) {\n\t\t\t\tchunkRef.current = 0;\n\t\t\t\tsetRange({ start: 0, end: maxIndex });\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tconst visible = Math.ceil((centerRef.current * 2) / spacing) || 60;\n\t\tconst chunkSize = Math.max(40, visible);\n\t\tconst chunk = Math.floor(pos / spacing / chunkSize);\n\t\tif (chunk === chunkRef.current) return;\n\t\tchunkRef.current = chunk;\n\t\tsetRange({\n\t\t\tstart: Math.max(0, (chunk - 1) * chunkSize),\n\t\t\tend: Math.min(maxIndex, (chunk + 2) * chunkSize),\n\t\t});\n\t}, []);\n\n\t// The one place that reacts to movement: transforms, ticks, and feedback.\n\tuseEffect(() => {\n\t\tconst sync = (pos: number) => {\n\t\t\tconst {\n\t\t\t\tsign,\n\t\t\t\tspacing,\n\t\t\t\tmaxIndex,\n\t\t\t\tindexToValue,\n\t\t\t\tlabelEvery,\n\t\t\t\tsound,\n\t\t\t\thaptics,\n\t\t\t\tonValueChange,\n\t\t\t} = latest.current;\n\t\t\tstripOffset.set(centerRef.current - sign * pos);\n\t\t\tupdateRange(pos);\n\n\t\t\tconst index = clamp(Math.round(pos / spacing), 0, maxIndex);\n\t\t\tconst previous = indexRef.current;\n\t\t\tif (index === previous) return;\n\t\t\tindexRef.current = index;\n\t\t\tsetCurrentIndex(index);\n\t\t\tsetDirection(index > previous ? 1 : -1);\n\t\t\tif (silentRef.current) return;\n\n\t\t\tonValueChange?.(indexToValue(index));\n\t\t\tconst now = performance.now();\n\t\t\tconst atBound = index === 0 || index === maxIndex;\n\t\t\t// Bounds always sound so a fast flick still ends on the pop.\n\t\t\tif (!atBound && now - lastTickAtRef.current < TICK_THROTTLE_MS) return;\n\t\t\tlastTickAtRef.current = now;\n\t\t\tif (atBound) {\n\t\t\t\tif (sound) playPop();\n\t\t\t\tif (haptics) void haptic(\"rigid\");\n\t\t\t} else {\n\t\t\t\tif (sound) playClick();\n\t\t\t\tif (haptics)\n\t\t\t\t\tvoid haptic(index % labelEvery === 0 ? \"light\" : \"selection\");\n\t\t\t}\n\t\t};\n\t\tsync(position.get());\n\t\treturn position.on(\"change\", sync);\n\t}, [haptic, playClick, playPop, position, stripOffset, updateRange]);\n\n\t// Track size and direction.\n\tuseLayoutEffect(() => {\n\t\tconst track = trackRef.current;\n\t\tif (!track) return;\n\t\tconst measure = () => {\n\t\t\tconst rtl = getComputedStyle(track).direction === \"rtl\";\n\t\t\tconst nextSign = horizontal ? (rtl ? -1 : 1) : -1;\n\t\t\tlatest.current.sign = nextSign;\n\t\t\tsetSign(nextSign);\n\t\t\tcenterRef.current =\n\t\t\t\t(horizontal ? track.offsetWidth : track.offsetHeight) / 2;\n\t\t\tchunkRef.current = -1;\n\t\t\tstripOffset.set(centerRef.current - nextSign * position.get());\n\t\t\tupdateRange(position.get());\n\t\t};\n\t\tmeasure();\n\t\tconst observer = new ResizeObserver(measure);\n\t\tobserver.observe(track);\n\t\treturn () => observer.disconnect();\n\t}, [horizontal, position, stripOffset, updateRange]);\n\n\t// Spacing or range changes keep the current step under the needle.\n\t// biome-ignore lint/correctness/useExhaustiveDependencies: re-run when the range changes too\n\tuseEffect(() => {\n\t\tchunkRef.current = -1;\n\t\tposition.jump(indexRef.current * spacing);\n\t}, [position, spacing, maxIndex]);\n\n\tconst stop = useCallback(() => {\n\t\tanimationRef.current?.stop();\n\t\tanimationRef.current = null;\n\t}, []);\n\n\tconst settle = useCallback(\n\t\t(\n\t\t\ttargetIndex: number,\n\t\t\t{\n\t\t\t\tvelocity = 0,\n\t\t\t\tsilent = false,\n\t\t\t\tonLand,\n\t\t\t}: { velocity?: number; silent?: boolean; onLand?: () => void } = {},\n\t\t) => {\n\t\t\tstop();\n\t\t\tconst target = clamp(targetIndex, 0, latest.current.maxIndex);\n\t\t\tconst targetPosition = target * latest.current.spacing;\n\t\t\tsilentRef.current = silent;\n\t\t\tconst finish = () => {\n\t\t\t\tanimationRef.current = null;\n\t\t\t\tsilentRef.current = false;\n\t\t\t\tsetRulerState(\"idle\");\n\t\t\t\tif (!silent) {\n\t\t\t\t\tlatest.current.onValueCommit?.(latest.current.indexToValue(target));\n\t\t\t\t\tonLand?.();\n\t\t\t\t}\n\t\t\t};\n\t\t\tif (shouldReduceMotion || position.get() === targetPosition) {\n\t\t\t\tposition.jump(targetPosition);\n\t\t\t\tfinish();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tsetRulerState(\"coasting\");\n\t\t\tanimationRef.current = animate(position, targetPosition, {\n\t\t\t\t...landingSpring,\n\t\t\t\tvelocity,\n\t\t\t\trestDelta: 0.01,\n\t\t\t\tonComplete: finish,\n\t\t\t});\n\t\t},\n\t\t[position, setRulerState, shouldReduceMotion, stop],\n\t);\n\n\t// Controlled value: glide there silently unless the user is interacting.\n\tuseEffect(() => {\n\t\tif (valueProp === undefined || interactingRef.current) return;\n\t\tconst target = valueToIndex(valueProp);\n\t\tif (target === indexRef.current && !animationRef.current) return;\n\t\tif (animationRef.current && !silentRef.current) return;\n\t\tsettle(target, { silent: true });\n\t}, [valueProp, valueToIndex, settle]);\n\n\tuseEffect(() => stop, [stop]);\n\n\tconst pointerAxis = (event: PointerEvent) =>\n\t\thorizontal ? event.clientX : event.clientY;\n\n\tconst handlePointerDown = (event: PointerEvent<HTMLDivElement>) => {\n\t\tif (disabled || event.button !== 0) return;\n\t\tconst track = event.currentTarget;\n\t\ttrack.setPointerCapture(event.pointerId);\n\t\ttrack.focus({ preventScroll: true });\n\t\tstop();\n\t\tsilentRef.current = false;\n\t\tinteractingRef.current = true;\n\t\tconst rect = track.getBoundingClientRect();\n\t\t// CSS zoom and transforms scale client coordinates; work in local px.\n\t\tconst scale = horizontal\n\t\t\t? rect.width / (track.offsetWidth || 1)\n\t\t\t: rect.height / (track.offsetHeight || 1);\n\t\tdragRef.current = {\n\t\t\tpointerId: event.pointerId,\n\t\t\tstart: pointerAxis(event),\n\t\t\tstartPosition: position.get(),\n\t\t\tmoved: false,\n\t\t\tscale: scale || 1,\n\t\t};\n\t};\n\n\tconst handlePointerMove = (event: PointerEvent<HTMLDivElement>) => {\n\t\tconst drag = dragRef.current;\n\t\tif (!drag || drag.pointerId !== event.pointerId) return;\n\t\tconst delta = (pointerAxis(event) - drag.start) / drag.scale;\n\t\tif (!drag.moved) {\n\t\t\tif (Math.abs(delta) < TAP_SLOP) return;\n\t\t\tdrag.moved = true;\n\t\t\tsetRulerState(\"dragging\");\n\t\t}\n\t\tconst raw = drag.startPosition - sign * delta;\n\t\tposition.set(rubberBand(raw, maxPosition));\n\t};\n\n\tconst handlePointerUp = (event: PointerEvent<HTMLDivElement>) => {\n\t\tconst drag = dragRef.current;\n\t\tif (!drag || drag.pointerId !== event.pointerId) return;\n\t\tdragRef.current = null;\n\t\tinteractingRef.current = false;\n\t\tconst track = event.currentTarget;\n\t\tif (track.hasPointerCapture(event.pointerId)) {\n\t\t\ttrack.releasePointerCapture(event.pointerId);\n\t\t}\n\n\t\tif (!drag.moved) {\n\t\t\t// Tap: glide to the tick under the pointer.\n\t\t\tconst rect = track.getBoundingClientRect();\n\t\t\tconst local =\n\t\t\t\t(pointerAxis(event) - (horizontal ? rect.left : rect.top)) / drag.scale;\n\t\t\tconst offset = sign * (local - centerRef.current);\n\t\t\tsettle(Math.round((position.get() + offset) / spacing));\n\t\t\treturn;\n\t\t}\n\n\t\tconst velocity = clamp(position.getVelocity(), -MAX_VELOCITY, MAX_VELOCITY);\n\t\tconst current = position.get();\n\t\tconst coast = shouldReduceMotion ? 0 : velocity * COAST_SECONDS;\n\t\tconst target = clamp(current + coast, 0, maxPosition);\n\t\tsettle(Math.round(target / spacing), { velocity });\n\t};\n\n\tconst handlePointerCancel = (event: PointerEvent<HTMLDivElement>) => {\n\t\tif (dragRef.current?.pointerId !== event.pointerId) return;\n\t\tdragRef.current = null;\n\t\tinteractingRef.current = false;\n\t\tsettle(Math.round(clamp(position.get(), 0, maxPosition) / spacing));\n\t};\n\n\tconst handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {\n\t\tif (disabled) return;\n\t\tconst stepBy = event.shiftKey ? 10 : 1;\n\t\tconst flip = horizontal && sign < 0 ? -1 : 1;\n\t\t// Repeated keys build on the pending target, not the moving needle.\n\t\tconst from = animationRef.current\n\t\t\t? wheelRef.current.target\n\t\t\t: indexRef.current;\n\t\tlet target: number | null = null;\n\t\tswitch (event.key) {\n\t\t\tcase \"ArrowRight\":\n\t\t\t\ttarget = from + stepBy * flip;\n\t\t\t\tbreak;\n\t\t\tcase \"ArrowLeft\":\n\t\t\t\ttarget = from - stepBy * flip;\n\t\t\t\tbreak;\n\t\t\tcase \"ArrowUp\":\n\t\t\t\ttarget = from + stepBy;\n\t\t\t\tbreak;\n\t\t\tcase \"ArrowDown\":\n\t\t\t\ttarget = from - stepBy;\n\t\t\t\tbreak;\n\t\t\tcase \"PageUp\":\n\t\t\t\ttarget = from + 10;\n\t\t\t\tbreak;\n\t\t\tcase \"PageDown\":\n\t\t\t\ttarget = from - 10;\n\t\t\t\tbreak;\n\t\t\tcase \"Home\":\n\t\t\t\ttarget = 0;\n\t\t\t\tbreak;\n\t\t\tcase \"End\":\n\t\t\t\ttarget = maxIndex;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\treturn;\n\t\t}\n\t\tevent.preventDefault();\n\t\ttarget = clamp(target, 0, maxIndex);\n\t\twheelRef.current.target = target;\n\t\tsettle(target);\n\t};\n\n\t// Wheel needs a non-passive listener to keep the page from scrolling.\n\tuseEffect(() => {\n\t\tconst track = trackRef.current;\n\t\tif (!track || disabled) return;\n\t\tconst onWheel = (event: WheelEvent) => {\n\t\t\tconst { sign, maxIndex, spacing } = latest.current;\n\t\t\tconst along = horizontal ? event.deltaX : event.deltaY;\n\t\t\tconst across = horizontal ? event.deltaY : event.deltaX;\n\t\t\tlet delta = 0;\n\t\t\tif (Math.abs(along) >= Math.abs(across)) {\n\t\t\t\tdelta = sign * along;\n\t\t\t} else if (horizontal && document.activeElement === track) {\n\t\t\t\t// A plain mouse wheel only drives a focused horizontal ruler.\n\t\t\t\tdelta = -across;\n\t\t\t} else {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tevent.preventDefault();\n\t\t\tconst wheel = wheelRef.current;\n\t\t\tif (!animationRef.current) wheel.target = indexRef.current;\n\t\t\tlet steps = 0;\n\t\t\tif (event.deltaMode !== 0 || Math.abs(delta) >= 50) {\n\t\t\t\t// One notch, one step.\n\t\t\t\tsteps = Math.sign(delta);\n\t\t\t\twheel.accumulated = 0;\n\t\t\t} else {\n\t\t\t\twheel.accumulated += delta;\n\t\t\t\tsteps = Math.trunc(wheel.accumulated / spacing);\n\t\t\t\twheel.accumulated -= steps * spacing;\n\t\t\t}\n\t\t\tif (steps === 0) return;\n\t\t\tconst target = clamp(wheel.target + steps, 0, maxIndex);\n\t\t\tif (target === wheel.target) return;\n\t\t\twheel.target = target;\n\t\t\tsettle(target);\n\t\t};\n\t\ttrack.addEventListener(\"wheel\", onWheel, { passive: false });\n\t\treturn () => track.removeEventListener(\"wheel\", onWheel);\n\t}, [disabled, horizontal, settle]);\n\n\t// iOS can still scroll the page under a captured pointer; block it mid-drag.\n\tuseEffect(() => {\n\t\tconst track = trackRef.current;\n\t\tif (!track) return;\n\t\tconst onTouchMove = (event: TouchEvent) => {\n\t\t\tif (dragRef.current?.moved) event.preventDefault();\n\t\t};\n\t\ttrack.addEventListener(\"touchmove\", onTouchMove, { passive: false });\n\t\treturn () => track.removeEventListener(\"touchmove\", onTouchMove);\n\t}, []);\n\n\tconst currentValue = indexToValue(currentIndex);\n\tconst valueText = format\n\t\t? format(currentValue)\n\t\t: `${formatValue(currentValue)}${unit ? ` ${unit}` : \"\"}`;\n\tconst labelAt = useCallback(\n\t\t(index: number) => indexToValue(index).toFixed(decimals),\n\t\t[indexToValue, decimals],\n\t);\n\tconst defaultIndex = valueToIndex(defaultValue ?? min);\n\n\treturn (\n\t\t<div\n\t\t\tref={rootRef}\n\t\t\tdata-slot=\"tick-ruler\"\n\t\t\tdata-state=\"idle\"\n\t\t\tdata-orientation={orientation}\n\t\t\tdata-disabled={disabled || undefined}\n\t\t\tdata-bound={\n\t\t\t\tcurrentIndex === 0\n\t\t\t\t\t? \"min\"\n\t\t\t\t\t: currentIndex === maxIndex\n\t\t\t\t\t\t? \"max\"\n\t\t\t\t\t\t: undefined\n\t\t\t}\n\t\t\tclassName={cn(\n\t\t\t\t\"group/ruler flex select-none gap-4 [--tick-ruler-accent:var(--color-orange-500)] data-disabled:opacity-50\",\n\t\t\t\thorizontal ? \"w-full flex-col items-center\" : \"flex-row items-center\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t>\n\t\t\t{showValue ? (\n\t\t\t\t<div\n\t\t\t\t\taria-hidden\n\t\t\t\t\tdata-slot=\"tick-ruler-value\"\n\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\"flex items-baseline gap-1.5 font-semibold text-foreground tabular-nums tracking-tight\",\n\t\t\t\t\t\tsizeStyle.readout,\n\t\t\t\t\t)}\n\t\t\t\t>\n\t\t\t\t\t<RollingValue\n\t\t\t\t\t\ttext={formatValue(currentValue)}\n\t\t\t\t\t\tdirection={direction}\n\t\t\t\t\t\tanimated={!shouldReduceMotion}\n\t\t\t\t\t/>\n\t\t\t\t\t{unit && !format ? (\n\t\t\t\t\t\t<span\n\t\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\t\"font-medium text-muted-foreground tracking-normal\",\n\t\t\t\t\t\t\t\tsizeStyle.unit,\n\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{unit}\n\t\t\t\t\t\t</span>\n\t\t\t\t\t) : null}\n\t\t\t\t</div>\n\t\t\t) : null}\n\n\t\t\t<div\n\t\t\t\tref={trackRef}\n\t\t\t\trole=\"slider\"\n\t\t\t\ttabIndex={disabled ? -1 : 0}\n\t\t\t\taria-label={label}\n\t\t\t\taria-orientation={orientation}\n\t\t\t\taria-valuemin={min}\n\t\t\t\taria-valuemax={max}\n\t\t\t\taria-valuenow={currentValue}\n\t\t\t\taria-valuetext={valueText}\n\t\t\t\taria-disabled={disabled || undefined}\n\t\t\t\tdata-slot=\"tick-ruler-track\"\n\t\t\t\tonPointerDown={handlePointerDown}\n\t\t\t\tonPointerMove={handlePointerMove}\n\t\t\t\tonPointerUp={handlePointerUp}\n\t\t\t\tonPointerCancel={handlePointerCancel}\n\t\t\t\tonKeyDown={handleKeyDown}\n\t\t\t\tonDoubleClick={() => {\n\t\t\t\t\tif (!disabled) settle(defaultIndex);\n\t\t\t\t}}\n\t\t\t\tclassName={cn(\n\t\t\t\t\t\"relative cursor-grab overflow-hidden rounded-lg outline-none focus-visible:ring-3 focus-visible:ring-ring/50 group-data-disabled/ruler:pointer-events-none group-data-[state=dragging]/ruler:cursor-grabbing\",\n\t\t\t\t\thorizontal\n\t\t\t\t\t\t? cn(\n\t\t\t\t\t\t\t\t\"w-full touch-pan-y [mask-image:linear-gradient(to_right,transparent,black_18%,black_82%,transparent)]\",\n\t\t\t\t\t\t\t\tsizeStyle.track,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t: \"h-64 w-20 touch-none [mask-image:linear-gradient(to_bottom,transparent,black_18%,black_82%,transparent)]\",\n\t\t\t\t)}\n\t\t\t>\n\t\t\t\t<motion.div\n\t\t\t\t\taria-hidden\n\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\"absolute will-change-transform\",\n\t\t\t\t\t\thorizontal ? \"top-2 left-0\" : \"top-0 left-3\",\n\t\t\t\t\t)}\n\t\t\t\t\tstyle={horizontal ? { x: stripOffset } : { y: stripOffset }}\n\t\t\t\t>\n\t\t\t\t\t<TickStrip\n\t\t\t\t\t\tstart={range.start}\n\t\t\t\t\t\tend={range.end}\n\t\t\t\t\t\tspacing={spacing}\n\t\t\t\t\t\tsign={sign}\n\t\t\t\t\t\torientation={orientation}\n\t\t\t\t\t\tmajorEvery={majorEvery}\n\t\t\t\t\t\tlabelEvery={labelEvery}\n\t\t\t\t\t\tlabelAt={labelAt}\n\t\t\t\t\t/>\n\t\t\t\t</motion.div>\n\n\t\t\t\t<span\n\t\t\t\t\taria-hidden\n\t\t\t\t\tdata-slot=\"tick-ruler-needle\"\n\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\"pointer-events-none absolute rounded-full bg-[var(--tick-ruler-accent)] transition-[transform,box-shadow] duration-200 ease-out group-hover/ruler:scale-110 group-data-[state=dragging]/ruler:scale-115 group-data-[state=dragging]/ruler:shadow-[0_0_12px_2px_var(--tick-ruler-accent)]\",\n\t\t\t\t\t\thorizontal\n\t\t\t\t\t\t\t? \"top-1 left-1/2 h-9 w-[3px] origin-top -translate-x-1/2\"\n\t\t\t\t\t\t\t: \"top-1/2 left-1 h-[3px] w-11 origin-left -translate-y-1/2\",\n\t\t\t\t\t)}\n\t\t\t\t/>\n\t\t\t</div>\n\n\t\t\t{presets?.length ? (\n\t\t\t\t<div\n\t\t\t\t\tdata-slot=\"tick-ruler-presets\"\n\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\"flex flex-wrap gap-2\",\n\t\t\t\t\t\thorizontal ? \"justify-center\" : \"flex-col\",\n\t\t\t\t\t)}\n\t\t\t\t>\n\t\t\t\t\t{presets.map((preset) => {\n\t\t\t\t\t\tconst active = valueToIndex(preset) === currentIndex;\n\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t<button\n\t\t\t\t\t\t\t\tkey={preset}\n\t\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t\t\tdisabled={disabled}\n\t\t\t\t\t\t\t\tdata-active={active || undefined}\n\t\t\t\t\t\t\t\tonClick={() =>\n\t\t\t\t\t\t\t\t\tsettle(valueToIndex(preset), {\n\t\t\t\t\t\t\t\t\t\tonLand: () => {\n\t\t\t\t\t\t\t\t\t\t\tif (haptics) void haptic(\"nudge\");\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tclassName=\"h-8 cursor-pointer rounded-full border border-border bg-background px-3.5 font-medium text-muted-foreground text-sm tabular-nums shadow-xs outline-none transition-[color,background-color,border-color] hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none data-active:border-transparent data-active:bg-foreground data-active:text-background dark:border-input dark:bg-input/30 dark:data-active:bg-foreground\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t{format ? format(preset) : formatValue(preset)}\n\t\t\t\t\t\t\t\t{unit && !format ? ` ${unit}` : null}\n\t\t\t\t\t\t\t</button>\n\t\t\t\t\t\t);\n\t\t\t\t\t})}\n\t\t\t\t</div>\n\t\t\t) : null}\n\t\t</div>\n\t);\n}\n\nexport default TickRuler;\n",
      "type": "registry:component",
      "target": "@components/tick-ruler.tsx"
    }
  ],
  "meta": {
    "createdAt": "2026-10-23",
    "image": "https://cdn.tentui.com/images/components/tick-ruler",
    "video": "https://cdn.tentui.com/videos/components/tick-ruler"
  },
  "docs": "https://tentui.com/components/tick-ruler",
  "categories": [
    "inputs"
  ],
  "type": "registry:component"
}