/Inputs
Inputs • React
Rolling Number Stepper
A minus and plus stepper where only the digits that change roll, up on the way up and down on the way down, and a digit that appears or disappears opens or closes its column; holding a button keeps stepping.
Tickets
Installation
Terminal
npx shadcn@latest add @microkit/rolling-number-stepperCode
1"use client";23import { useEffect, useRef, useState, type MouseEvent, type PointerEvent } from "react";45const MIN = 0;6const MAX = 99;7const START = 8;8const EASE = "cubic-bezier(.22, 1, .36, 1)";910// Ref callbacks rather than effects: each one runs once, the moment its digit or column is mounted.11const roll = (node: HTMLElement | null) => {12if (!node) return;13// Strict Mode attaches refs twice in development; starting over keeps the second pass from stacking on the first.14node.getAnimations().forEach((animation) => animation.cancel());15const direction = Number(node.dataset.direction);16const leaving = node.dataset.roll === "out";17const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;18// A step up pushes the old digit out of the top and brings the new one in from below; a step down reverses it.19const away = `translateY(${direction * -100}%)`;20const toward = `translateY(${direction * 100}%)`;21node.animate(22leaving23? [{ transform: "none", opacity: 1 }, { transform: away, opacity: 0 }]24: [{ transform: toward, opacity: 0 }, { transform: "none", opacity: 1 }],25{ duration: reduce ? 0 : 450, easing: EASE, fill: leaving ? "forwards" : "none" },26);27};2829// A digit that appears or disappears (9 to 10, 10 to 9) opens or closes its column, so the number re-centres smoothly.30const resize = (node: HTMLElement | null) => {31if (!node) return;32// Measured with any earlier pass cancelled, or a column caught mid-collapse would open to nothing.33node.getAnimations().forEach((animation) => animation.cancel());34const width = `${node.offsetWidth}px`;35const leaving = node.dataset.column === "out";36const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;37node.animate(leaving ? [{ width }, { width: "0px" }] : [{ width: "0px" }, { width }], {38duration: reduce ? 0 : 450,39easing: EASE,40fill: leaving ? "forwards" : "none",41});42};4344type Change = { value: number; previous: number; direction: number; tick: number };4546function RollingDigits({ value, previous, direction, tick }: Change) {47const now = String(value);48const was = String(previous);49const length = Math.max(now.length, was.length);5051return (52<span className="pointer-events-none inline-flex select-none justify-self-center [grid-area:1/1]" aria-hidden="true">53{Array.from({ length }, (_, index) => {54// Columns are matched from the right, so the ones digit always rolls over the ones digit.55const place = length - 1 - index;56const digit = now[now.length - 1 - place];57const old = was[was.length - 1 - place];58if (digit === old) {59return (60<span key={place} className="inline-grid overflow-hidden">61<span className="[grid-area:1/1]">{digit}</span>62</span>63);64}65const column = digit === undefined ? "out" : old === undefined ? "in" : undefined;66return (67// Keyed by the step, so every change mounts fresh digits and their animation plays again.68<span69key={`${place}-${tick}`}70ref={column ? resize : undefined}71className="inline-grid overflow-hidden"72data-column={column}73>74{old !== undefined && (75<span ref={roll} className="[grid-area:1/1]" data-roll="out" data-direction={direction}>76{old}77</span>78)}79{digit !== undefined && (80<span ref={roll} className="[grid-area:1/1]" data-roll="in" data-direction={direction}>81{digit}82</span>83)}84</span>85);86})}87</span>88);89}9091export function RollingNumberStepper() {92const [change, setChange] = useState<Change>({ value: START, previous: START, direction: 1, tick: 0 });93const valueRef = useRef(START);94const hold = useRef({ delay: 0, repeat: 0 });9596useEffect(() => {97const timers = hold.current;98return () => {99clearTimeout(timers.delay);100clearInterval(timers.repeat);101};102}, []);103104const release = () => {105clearTimeout(hold.current.delay);106clearInterval(hold.current.repeat);107};108109const nudge = (direction: number) => {110const previous = valueRef.current;111const value = Math.min(MAX, Math.max(MIN, previous + direction));112// A disabled button never hears the pointer come up, so reaching the limit ends the hold itself.113if (value === previous) return release();114valueRef.current = value;115setChange((current) => ({ value, previous, direction, tick: current.tick + 1 }));116};117118// Pressing steps once; holding on keeps stepping until the pointer lets go.119const press = (event: PointerEvent<HTMLButtonElement>, direction: number) => {120if (event.button !== 0) return;121release();122nudge(direction);123hold.current.delay = window.setTimeout(() => {124hold.current.repeat = window.setInterval(() => nudge(direction), 120);125}, 400);126};127128// Pointer presses already stepped on the way down, so only a keyboard click (detail 0) steps here.129const click = (event: MouseEvent<HTMLButtonElement>, direction: number) => {130if (event.detail === 0) nudge(direction);131};132133return (134<div135className="inline-flex items-center gap-[18px] text-[color:light-dark(#14181e,#f2f4f6)]"136role="group" aria-label="Tickets">137<button138type="button"139className="m-0 grid size-[44px] shrink-0 cursor-pointer touch-manipulation select-none place-items-center rounded-full border-0 bg-[light-dark(#f0f2f5,#1b1e23)] p-0 text-inherit transition-[background-color,transform,opacity] duration-200 ease-[cubic-bezier(.32,.72,0,1)] [-webkit-touch-callout:none] enabled:hover:bg-[light-dark(#e6e9ee,#24282e)] enabled:active:scale-[.92] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#86b6ff] disabled:cursor-default disabled:opacity-[.35] motion-reduce:transition-[background-color,opacity]"140aria-label="Remove a ticket"141onPointerDown={(event) => press(event, -1)}142onPointerUp={release}143onPointerLeave={release}144onPointerCancel={release}145onClick={(event) => click(event, -1)}146disabled={change.value <= MIN}147>148<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">149<path d="M5 12h14" />150</svg>151</button>152<div className="grid min-w-0 justify-items-center">153<span className="inline-grid whitespace-nowrap text-[32px] font-semibold leading-none tracking-[-.02em] tabular-nums">154{/* The widest value sizes the box, so the buttons never move while digits come and go. */}155<span className="invisible justify-self-center [grid-area:1/1]" aria-hidden="true">156{MAX}157</span>158{/* The settled value is what screen readers and copy-paste get, but it is never painted. */}159<output className="justify-self-center text-transparent [grid-area:1/1]" aria-live="polite">160{change.value}161</output>162<RollingDigits {...change} />163</span>164<span className="mt-[6px] text-[11px] font-medium leading-none text-[color:light-dark(#69727f,#868d97)]">Tickets</span>165</div>166<button167type="button"168className="m-0 grid size-[44px] shrink-0 cursor-pointer touch-manipulation select-none place-items-center rounded-full border-0 bg-[light-dark(#f0f2f5,#1b1e23)] p-0 text-inherit transition-[background-color,transform,opacity] duration-200 ease-[cubic-bezier(.32,.72,0,1)] [-webkit-touch-callout:none] enabled:hover:bg-[light-dark(#e6e9ee,#24282e)] enabled:active:scale-[.92] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#86b6ff] disabled:cursor-default disabled:opacity-[.35] motion-reduce:transition-[background-color,opacity]"169aria-label="Add a ticket"170onPointerDown={(event) => press(event, 1)}171onPointerUp={release}172onPointerLeave={release}173onPointerCancel={release}174onClick={(event) => click(event, 1)}175disabled={change.value >= MAX}176>177<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">178<path d="M5 12h14M12 5v14" />179</svg>180</button>181</div>182);183}