Explore the index
Browse components
InputstableReactPointer EventsCSS

Scrub Number

Drag for a feeling. Type for precision.

Size increment

Drag the arrow handles, type a number, or focus a handle and use arrow keys.

A real browser viewport. Scroll within the preview when the sample needs more space. Configuration stays in sync between sizes.

01What it does

A compact controlled number field with a horizontal drag handle, direct numeric input and keyboard adjustments. Useful for creative tools, layout inspectors and parameter panels.

In this previewThe controls change the actual size, corner radius and rotation of a local specimen. No values are saved or sent.

Design decisions

Keep dragging on an explicit handle so text entry and scrolling remain predictable.

Use native numeric input alongside the drag gesture so precision never depends on pointer dexterity.

02Install

Registry install

registry:component

Run this in a React project with components.json. The component and its stylesheet land in components/lab/scrub-number/.

npx shadcn@latest add https://lab.tanishk.me/r/scrub-number.json

Files included

2
  • components/lab/scrub-number/scrub-number.tsxcomponent
  • components/lab/scrub-number/scrub-number.cssstyles

Manual install: copy each file into the same folder and preserve its relative imports. Open the source

03Usage

scrub-number.usage.tsx
"use client";
import { useState } from "react";
import { ScrubNumber } from "@/components/lab/scrub-number/scrub-number";

export function SizeField() {
const [value, setValue] = useState(48);
return <ScrubNumber label="Size" value={value} onChange={setValue} min={12} max={120} />;
}

04Props

Props accepted by Scrub Number
PropTypeDefaultNotes
valuenumber—Controlled numeric value.
onChange(value: number) => void—Receives the finite, rounded and clamped value.
labelstring—Visible and accessible field label.
minnumber0Lower bound.
maxnumber100Upper bound.
stepnumber1Positive increment; invalid increments fall back to one.

05Accessibility

  • A native labeled number input provides direct entry and native numeric semantics.
  • The drag handle is a focusable button: arrow keys adjust, Shift multiplies keyboard steps, and Home/End reach the bounds.
  • Pointer capture ends on release or cancellation; horizontal touch dragging is limited to the handle.
  • No animation is required for operation.

06Source

2 files, 146 lines. The combined copy is a reference; save each file separately for use.

components/lab/scrub-number/scrub-number.tsx— press enter to show or hide the source
tsx
"use client";
import { useRef } from "react";
import "./scrub-number.css";
export type ScrubNumberProps = {
value: number;
onChange: (value: number) => void;
label: string;
min?: number;
max?: number;
step?: number;
};
/** A controlled numeric field with pointer scrubbing and keyboard equivalents. */
export function ScrubNumber({
value,
onChange,
label,
min = 0,
max = 100,
step = 1,
}: ScrubNumberProps) {
const drag = useRef<{ x: number; value: number } | null>(null);
const increment = Number.isFinite(step) && step > 0 ? step : 1;
const commit = (n: number) => {
if (Number.isFinite(n))
onChange(
Math.max(
min,
Math.min(
max,
Number((Math.round(n / increment) * increment).toFixed(8)),
),
),
);
};
return (
<div className="lab-scrub-number">
<span className="lab-scrub-label">{label}</span>
<div className="lab-scrub-field">
<button
type="button"
className="lab-scrub-handle"
aria-label={`Drag to adjust ${label}; use arrow keys for small steps, Shift for ten steps, Home or End for limits`}
onPointerDown={(e) => {
if (e.button !== 0) return;
drag.current = { x: e.clientX, value };
e.currentTarget.setPointerCapture(e.pointerId);
}}
onPointerMove={(e) => {
if (drag.current)
commit(
drag.current.value +
((e.clientX - drag.current.x) / 6) *
increment *
(e.shiftKey ? 0.1 : 1),
);
}}
onPointerUp={() => (drag.current = null)}
onPointerCancel={() => (drag.current = null)}
onLostPointerCapture={() => (drag.current = null)}
onKeyDown={(e) => {
const direction =
e.key === "ArrowRight" || e.key === "ArrowUp"
? 1
: e.key === "ArrowLeft" || e.key === "ArrowDown"
? -1
: 0;
if (direction) {
e.preventDefault();
commit(value + direction * increment * (e.shiftKey ? 10 : 1));
}
if (e.key === "Home" || e.key === "End") {
e.preventDefault();
commit(e.key === "Home" ? min : max);
}
}}
>
<span aria-hidden="true">↔</span>
</button>
<input
type="number"
aria-label={label}
value={value}
min={min}
max={max}
step={increment}
onChange={(e) => commit(e.currentTarget.valueAsNumber)}
onBlur={(e) => {
e.currentTarget.value = String(value);
}}
/>
</div>
</div>
);
}
components/lab/scrub-number/scrub-number.css— press enter to show or hide the source
css
.lab-scrub-number {
display: grid;
gap: 10px;
font: inherit;
color: inherit;
min-width: 0;
}
.lab-scrub-label {
font-size: 14px;
}
.lab-scrub-field {
display: flex;
align-items: center;
border: 1px solid #25262055;
border-radius: 4px;
background: #fff;
overflow: hidden;
}
.lab-scrub-handle {
display: grid;
place-items: center;
flex: none;
width: 48px;
height: 48px;
border: 0;
border-right: 1px solid #25262025;
background: #eeeae0;
color: #252620;
cursor: ew-resize;
touch-action: none;
user-select: none;
font: inherit;
font-size: 22px;
}
.lab-scrub-field input {
width: 100%;
min-width: 0;
padding: 12px;
border: 0;
background: transparent;
color: #252620;
font: inherit;
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.lab-scrub-handle:focus-visible,
.lab-scrub-field input:focus-visible {
outline: 2px solid #5834d7;
outline-offset: -3px;
}