Wheel picker
A flat scroll-snap column for choosing one value from a short ordered list, with a centered selection band and a hidden input for the form.
pnpm dlx shadcn@latest add @haze/wheel-picker---
import { ChevronDown } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
type Option = string | { value: string; label?: string };
interface Props {
id: string;
name?: string;
label: string;
options?: Option[];
value?: string;
/** Visible rows, an odd number. */
rows?: number;
disabled?: boolean;
class?: string;
[key: string]: unknown;
}
const { id, name = id, label, options = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], value, rows = 5, disabled, class: className, ...rest } = Astro.props;
const items = options.map((option) => (typeof option === "string" ? { value: option, label: option } : { value: option.value, label: option.label ?? option.value }));
const found = items.findIndex((item) => item.value === value);
const start = found < 0 ? 0 : found;
const visible = rows % 2 === 1 && rows >= 3 ? rows : 5;
const pad = (visible - 1) / 2;
---
<div data-haze-wheel-picker data-initial={start} data-name={name} class={cn("grid min-w-0 justify-items-center gap-1.5", className)}>
<span id={`${id}-label`} class="text-xs font-medium text-muted-foreground">{label}</span>
<div class="relative h-10 w-full min-w-16 [[data-enhanced]_&]:hidden">
<select id={`${id}-fallback`} name={name} aria-labelledby={`${id}-label`} disabled={disabled} class="peer h-full w-full min-w-0 appearance-none rounded-lg border border-frame bg-field pr-9 pl-3 text-[0.8125rem] tracking-[-0.01em] text-foreground transition-[border-color,background-color,box-shadow] duration-150 ease-snappy hover:border-frame-strong hover:bg-accent focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/24 focus-visible:outline-hidden disabled:cursor-not-allowed disabled:opacity-50 [&>option]:bg-popover [&>option]:text-popover-foreground">
{items.map((item, index) => <option value={item.value} selected={index === start}>{item.label}</option>)}
</select>
<ChevronDown class="pointer-events-none absolute top-1/2 right-3 size-4 -translate-y-1/2 text-muted-foreground peer-disabled:opacity-50" aria-hidden="true" />
</div>
<div class="relative hidden w-full min-w-16 [[data-enhanced]_&]:block">
<div aria-hidden="true" class="pointer-events-none absolute inset-x-0 top-1/2 h-10 -translate-y-1/2 rounded-lg bg-muted"></div>
<div
id={id}
role="listbox"
tabindex={disabled ? undefined : 0}
aria-labelledby={`${id}-label`}
aria-activedescendant={items.length ? `${id}-option-${start}` : undefined}
aria-disabled={disabled ? "true" : undefined}
style={`height:${visible * 2.5}rem;padding-block:${pad * 2.5}rem`}
class="relative snap-y snap-mandatory overflow-y-auto overscroll-contain rounded-lg text-center text-[0.9375rem]/5 text-foreground tabular-nums [-ms-overflow-style:none] [scrollbar-width:none] focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring aria-disabled:pointer-events-none aria-disabled:opacity-50 [&::-webkit-scrollbar]:hidden"
{...rest}
>
{items.map((item, index) => (
<div id={`${id}-option-${index}`} role="option" data-value={item.value} aria-selected={index === start ? "true" : "false"} class="flex h-10 cursor-pointer snap-center items-center justify-center select-none">{item.label}</div>
))}
</div>
</div>
<input type="hidden" value={items[start]?.value ?? ""} disabled={disabled} />
<span data-wheel-status role="status" aria-live="polite" class="sr-only"></span>
</div>
<script>
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
const list = root.querySelector<HTMLElement>("[role=listbox]");
const hidden = root.querySelector<HTMLInputElement>("input[type=hidden]");
const status = root.querySelector<HTMLElement>("[data-wheel-status]");
const options = [...root.querySelectorAll<HTMLElement>("[role=option]")];
if (!list || !hidden || !status || !options.length) return;
const fallback = root.querySelector("select");
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)");
const initial = Number(root.dataset.initial) || 0;
const rowHeight = () => options[0].offsetHeight || 40;
const page = () => Math.max(1, Math.floor(list.clientHeight / rowHeight()) - 1);
let current = -1, frame = 0, typed = "", typedAt = 0, settle = 0;
const clamp = (index: number) => Math.min(options.length - 1, Math.max(0, index));
const paint = () => {
frame = 0;
const at = list.scrollTop / rowHeight();
options.forEach((option, index) => {
if (reduce.matches) { option.style.opacity = ""; option.style.scale = ""; return; }
const d = Math.min(Math.abs(index - at), 3);
option.style.opacity = String(Math.max(0.45, 1 - d * 0.2));
option.style.scale = String(1 - d * 0.04);
});
};
const select = (index: number, announce: boolean) => {
if (index === current) return;
if (current >= 0) options[current].setAttribute("aria-selected", "false");
current = index;
options[index].setAttribute("aria-selected", "true");
list.setAttribute("aria-activedescendant", options[index].id);
hidden.value = options[index].dataset.value ?? "";
if (announce) { status.textContent = options[index].textContent ?? ""; hidden.dispatchEvent(new Event("change", { bubbles: true })); }
};
const go = (index: number) => list.scrollTo({ top: index * rowHeight(), behavior: reduce.matches ? "auto" : "smooth" });
const done = () => {
clearTimeout(settle);
const index = clamp(Math.round(list.scrollTop / rowHeight()));
select(index, true);
if (Math.abs(list.scrollTop - index * rowHeight()) > 1) list.scrollTo({ top: index * rowHeight(), behavior: "auto" });
};
list.addEventListener("scroll", () => {
if (!frame) frame = requestAnimationFrame(paint);
clearTimeout(settle);
settle = window.setTimeout(done, 150);
}, { passive: true });
list.addEventListener("scrollend", done);
list.addEventListener("keydown", (event) => {
const steps: Record<string, number> = { ArrowDown: 1, ArrowUp: -1, PageDown: page(), PageUp: -page() };
let target = current;
if (event.key in steps) target = current + steps[event.key];
else if (event.key === "Home") target = 0;
else if (event.key === "End") target = options.length - 1;
else if (event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey && event.key !== " ") {
const now = Date.now();
typed = now - typedAt > 700 ? event.key.toLowerCase() : typed + event.key.toLowerCase();
typedAt = now;
const text = (option: HTMLElement) => (option.textContent ?? "").trim().toLowerCase();
const exact = /^[0-9]+$/.test(typed) ? options.findIndex((option) => text(option) !== "" && Number(text(option)) === Number(typed)) : -1;
const hit = exact >= 0 ? exact : options.findIndex((option) => text(option).startsWith(typed));
if (hit < 0) return;
target = hit;
} else return;
event.preventDefault();
target = clamp(target);
select(target, true); go(target);
});
list.addEventListener("click", (event) => {
const row = (event.target as HTMLElement).closest<HTMLElement>("[role=option]");
const index = row ? options.indexOf(row) : -1;
if (index >= 0) { select(index, true); go(index); }
});
reduce.addEventListener("change", paint);
hidden.form?.addEventListener("reset", () => queueMicrotask(() => { select(initial, false); list.scrollTo({ top: initial * rowHeight(), behavior: "auto" }); }));
fallback?.remove(); hidden.name = root.dataset.name ?? "";
root.setAttribute("data-enhanced", "");
list.scrollTop = initial * rowHeight();
select(initial, false); paint();
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-wheel-picker]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-wheel-picker-listener")) {
document.documentElement.setAttribute("data-haze-wheel-picker-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- A short ordered list such as hours, minutes, a quantity or a size is chosen by scrolling, mostly on touch.
- Several columns sit side by side to compose one value, for example hours and minutes.
When not to use
- Use select or combobox when the list is long or needs searching.
- Use number-field or stepper when the value is typed or stepped numerically.
- Use time-picker when a native time input with min and max is enough.
Installation
Add the Haze registry to components.json once, then install Wheel picker with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/wheel-pickernpx shadcn@latest add @haze/wheel-pickeryarn dlx shadcn@latest add @haze/wheel-pickerbunx --bun shadcn@latest add @haze/wheel-pickerFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique id of the listbox; option ids are derived from it. |
| name | string | id | Name of the hidden input that submits the selected value. |
| label* | string | – | Visible label above the column, also the accessible name. |
| options | (string | { value: string; label?: string })[] | ["1" ... "10"] | Ordered options; a string is both value and label. |
| value | string | – | Value selected on first render; the first option when missing. |
| rows | number | 5 | Visible rows, an odd number of at least 3. |
| disabled | boolean | false | Removes the column from the tab order and from form submission. |
| class | string | – | Additional classes on this component's outer wrapper. |
| [key: string] | unknown | – | Additional attributes on the listbox, for example aria-describedby. |
Keyboard interactions
- ArrowUpArrowDown
- Move the selection by one option.
- PageUpPageDown
- Move the selection by a page of options.
- HomeEnd
- Select the first or last option.
- Digits or letters
- Jump to the first option whose label starts with what was typed.
Accessibility
- The column is a role=listbox with role=option rows and aria-activedescendant, so it is one tab stop and the selection follows the scroll position. Native radios would add one tab stop per row and could not follow scrolling.
- The selected value is announced politely after scrolling settles and submitted through a hidden input; a change event bubbles from it.
- Rows further from the center fade and shrink through a script that reads the scroll position; reduced motion removes the fade, the scale and smooth scrolling.
- Pointer and touch use native scrolling with scroll-snap, and a click on a row selects it.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Wheel picker" with `npx shadcn@latest add @haze/wheel-picker`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import WheelPicker from "@/components/haze/ui/wheel-picker.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/wheel-picker.md
Customize with real content
Adapt the Haze wheel-picker ui for our product. Replace the demo values of these props with our real content: id, name, label, options, value, rows, disabled, class, [key: string]. Keep the layout, the heading level rules and these accessibility details: The column is a role=listbox with role=option rows and aria-activedescendant, so it is one tab stop and the selection follows the scroll position. Native radios would add one tab stop per row and could not follow scrolling. The selected value is announced politely after scrolling settles and submitted through a hidden input; a change event bubbles from it. Rows further from the center fade and shrink through a script that reads the scroll position; reduced motion removes the fade, the scale and smooth scrolling. Pointer and touch use native scrolling with scroll-snap, and a click on a row selects it. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using wheel-picker, check it fits: use it when a short ordered list such as hours, minutes, a quantity or a size is chosen by scrolling, mostly on touch.; several columns sit side by side to compose one value, for example hours and minutes.. Avoid it when use select or combobox when the list is long or needs searching.; use number-field or stepper when the value is typed or stepped numerically.; use time-picker when a native time input with min and max is enough.. Alternatives: @haze/time-picker, @haze/select, @haze/number-field, @haze/stepper.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.