React Wheel Picker With Inertia and Snapping – Detent

Description:

Detent is a React wheel picker that keeps spinning after a flick and snaps to the selected row.

It has a styled picker for inputs such as minutes or quantities and a headless hook for custom rows.

Preview

wheel-picker-detent

Features

  • Vertical and horizontal wheels with right-to-left layout support.
  • Notched mouse wheels can advance one row at a time, while trackpads follow the gesture.
  • Keyboard selection and an accessible name for the picker.
  • The center marker can resize to fit the selected label.
  • Optional haptics and tick sounds in browsers that support them.

Add a controlled minute picker

Install Detent in your React project:

npm install @hayderkg/detent

Pass value and onChange when the form owns the selection. Use defaultValue for an uncontrolled picker and label to name the control.

In Next.js App Router, keep this stateful example in a Client Component.

"use client";
import { useState } from "react";
import { Detent } from "@hayderkg/detent";
const minutes = Array.from({ length: 60 }, (_, index) =>
  String(index).padStart(2, "0"),
);
export function MinutePicker() {
  const [minute, setMinute] = useState("15");
  return (
    <Detent
      items={minutes}
      label="Minutes"
      value={minute}
      onChange={setMinute}
    />
  );
}

Detent props and callbacks

PropEffect
items, labelPass the choices and an accessible name. For object choices, use getKey(item, index) for a stable key and getLabel(item, index) for typeahead and announcements.
value, defaultValueSet the controlled or initial uncontrolled selection. A controlled value is matched by key.
onChange(item, index), changeOnCalled when the selection changes. The default "settle" reports the item after motion stops. "tick" also reports rows crossed during wheel gestures.
onSettle(item, index)Use the settled item to commit the choice, especially when you use changeOn="tick" for a live preview.
isDisabled(item, index), disabledMark individual choices unavailable or disable the whole picker.
orientation, dir, loopThe default orientation is "vertical". Set "horizontal" for a row of options, pass dir="rtl" for right-to-left output on the first render, or set loop to wrap at the ends.
itemHeight, gap, visibleCountSet the row size along the scroll axis and the gap between rows in pixels. The styled picker defaults to 40 px per item and seven visible rows. Use an odd visibleCount.
wheelMode"auto" distinguishes notched wheels from trackpads, "step" advances by steps, and "smooth" follows scroll deltas.
renderItem(item, state)Renders a custom option. The state includes index, selected, offset, and progress.
indicatorDefaults to a solid, rounded band. Set { fit: "content", shape: "pill", material: "glass" } to fit the indicator to the selected label.

Render your own picker rows

Use useDetent when you need control of the markup. It takes itemSize for the row dimension along the scroll axis. Render the announcement region next to the picker root.

Read selectedItem or selectedIndex from the hook. Call scrollTo(index, { animate: false }) to jump to a row. Use --offset or --abs-offset in row styles to vary tilt or opacity as each option moves away from the center.

"use client";
import { useDetent } from "@hayderkg/detent";
export function SizePicker() {
  const picker = useDetent({
    items: ["Small", "Medium", "Large"],
    defaultValue: "Medium",
    itemSize: 40,
    visibleCount: 3,
    label: "Size",
  });
  return (
    <>
      <div {...picker.getRootProps()}>
        <div {...picker.getTrackProps()}>
          {picker.rows.map((row) => (
            <div key={row.key} {...row.getProps()}>
              {row.item}
            </div>
          ))}
        </div>
      </div>
      <div {...picker.getLiveRegionProps()} />
    </>
  );
}

Animate labels and numbers

The package also exports DetentText from @hayderkg/detent/text for short, changing labels and DetentNumber from @hayderkg/detent/number for rolling counters and formatted amounts.

For server-rendered amounts, pass an explicit locales value so the server and browser start with matching number formats.

Related resources

Add Comment