Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(DateInput): a wheel commits at a mouse drag's release, not at every row it passes (#8483)

Christopher Chedeau802da1a797f0
diff --git a/.changeset/wheel-mouse-drag-commit.md b/.changeset/wheel-mouse-drag-commit.md
new file mode 100644
index 0000000000..a16b25f506
--- /dev/null
+++ b/.changeset/wheel-mouse-drag-commit.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Dragging a picker wheel with a mouse now commits the value when the button is released, as a finger's drag does. Before, the wheel committed every row it passed while the button was still down, so in DateInput the calendar behind the month and year wheels changed under the hand. This applies to the wheels in the TimeInput, DateTimeInput and DateInput bottom sheets. Touch, keys and a click on a row are unchanged.
+
+@vjeux
diff --git a/packages/core/src/DateInput/DateInputTouch.test.tsx b/packages/core/src/DateInput/DateInputTouch.test.tsx
index 3182c3cae2..6564c6774f 100644
--- a/packages/core/src/DateInput/DateInputTouch.test.tsx
+++ b/packages/core/src/DateInput/DateInputTouch.test.tsx
@@ -2325,6 +2325,50 @@ describe('DateInput — a mouse can drag a wheel', () => {
});
});
+ /**
+ * Every `scrollTop` the drag assigns ends in its own `scrollend` in
+ * Chromium, and the hand can rest with the button still down. Neither is
+ * rest: the month the wheel lands on commits at the release, as it does
+ * when a finger lifts.
+ */
+ it('commits nothing while a mouse drag holds the wheel, and commits at the release', () => {
+ vi.useFakeTimers();
+ try {
+ // A year of months, so May is reachable (the default range ends in April).
+ renderAndOpen(
+ <Controlled initial="2026-03-21" min="2026-01-01" max="2026-12-31" />,
+ );
+ fireEvent.click(title());
+ withWheelLayout(() => {
+ const el = wheel();
+ trackable(el);
+ // March, the committed month, sits under the band.
+ el.scrollTop = 2 * 28;
+ const selected = () =>
+ el.querySelector('[aria-selected="true"]')?.textContent;
+ el.dispatchEvent(pointer('pointerdown', {y: 200}));
+ // Two rows on: May is under the band.
+ el.dispatchEvent(pointer('pointermove', {y: 144}));
+ fireEvent.scroll(el);
+ el.dispatchEvent(new Event('scrollend'));
+ act(() => {
+ vi.advanceTimersByTime(SCROLL_QUIET_MS * 2);
+ });
+ expect(selected()).toBe('March');
+ expect(title()).toHaveTextContent('March 2026');
+
+ el.dispatchEvent(pointer('pointerup', {y: 144}));
+ act(() => {
+ vi.advanceTimersByTime(SCROLL_QUIET_MS * 2);
+ });
+ expect(selected()).toBe('May');
+ expect(title()).toHaveTextContent('May 2026');
+ });
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
/**
* BottomSheet starts its own drag-to-dismiss from a `pointerdown` on its
* body and CAPTURES the pointer for it, which retargets every later pointer
diff --git a/packages/core/src/DateInput/Wheel.tsx b/packages/core/src/DateInput/Wheel.tsx
index 059e4dfd6d..36c2590d70 100644
--- a/packages/core/src/DateInput/Wheel.tsx
+++ b/packages/core/src/DateInput/Wheel.tsx
@@ -12,7 +12,7 @@
* rubber-banding, and the settle animation are the platform's; all this adds
* is `scroll-snap-align: center` on each option, half a viewport of padding at
* each end so the first and last option can reach the middle, and a commit
- * when the scrolling stops.
+ * when the scrolling stops and no finger or mouse drag still holds it.
*
* The falloff (rows fading and tipping away from the centre) is a CSS
* scroll-driven animation on a `view()` timeline — the browser interpolates it
@@ -259,6 +259,13 @@ export function Wheel({
// `selectedIndex` only catches up when the wheel settles.
const [activeIndex, setActiveIndex] = useState(selectedIndex);
+ // A mouse cannot drag a scroll container, so without this the wheel ignores
+ // the one gesture its shape invites. Touch is untouched — it pans natively,
+ // with momentum this could not match. See usePointerDragScroll. The drag it
+ // holds is not rest either: the settle below waits for its release, as it
+ // waits for a finger's (useScrollSettle).
+ const {isDraggingRef} = usePointerDragScroll(scrollerRef, isActive);
+
// Row height in px, read from layout rather than assumed, so a theme that
// retunes --date-input-touch-wheel-item-size still lands on the right row.
const itemBlockSize = useCallback((): number => {
@@ -306,6 +313,7 @@ export function Wheel({
}
},
isActive,
+ isDraggingRef,
);
// Park the committed row under the band whenever the wheel is shown, or the
@@ -375,11 +383,6 @@ export function Wheel({
// there is no way to share — it takes every touch that lands on it.
useOwnScrollGesture(scrollerRef, 'all', {isEnabled: isActive});
- // A mouse cannot drag a scroll container, so without this the wheel ignores
- // the one gesture its shape invites. Touch is untouched — it pans natively,
- // with momentum this could not match. See usePointerDragScroll.
- usePointerDragScroll(scrollerRef, isActive);
-
const moveBy = useCallback(
(delta: number) => {
const next = Math.min(
diff --git a/packages/core/src/DateInput/usePointerDragScroll.ts b/packages/core/src/DateInput/usePointerDragScroll.ts
index 140cc0cc38..6f937ed6b3 100644
--- a/packages/core/src/DateInput/usePointerDragScroll.ts
+++ b/packages/core/src/DateInput/usePointerDragScroll.ts
@@ -5,7 +5,8 @@
/**
* @file usePointerDragScroll.ts
* @input A scroll container ref
- * @output Lets a mouse drag the container's contents the way a finger does
+ * @output Lets a mouse drag the container's contents the way a finger does,
+ * and reports while a drag holds it (`isDraggingRef`)
* @position Internal behavior hook; consumed by Wheel.tsx
*
* ## Why this exists
@@ -38,12 +39,20 @@
* then jumps a whole one. The drag therefore suspends snapping, and restores
* it once the release has settled somewhere legal.
*
+ * ## A held drag is not rest
+ *
+ * Every `scrollTop` the drag assigns is a programmatic scroll, and Chromium
+ * ends each one with a `scrollend` — 14 moves, 14 of them. A settle listening
+ * for `scrollend` alone would commit the row under the band at every step,
+ * with the button still down. `isDraggingRef` tells `useScrollSettle` the
+ * drag is still held, the way a touch that has not ended does.
+ *
* SYNC: When modified, update:
* - /packages/core/src/DateInput/Wheel.tsx
* - /packages/core/src/DateInput/DateInputTouch.test.tsx
*/
-import {useEffect} from 'react';
+import {useEffect, useRef} from 'react';
/**
* Movement below this stays a click. A mouse shifts a pixel or two under the
@@ -54,6 +63,16 @@ export const DRAG_SLOP = 4;
/** How long to wait for a release's settle before restoring snapping. */
const SETTLE_FALLBACK_MS = 260;
+/** What {@link usePointerDragScroll} reports back about the drag. */
+export type PointerDragScrollState = {
+ /**
+ * True while a mouse drag holds the container: from the press that moved
+ * past {@link DRAG_SLOP} until its release. Pass it to `useScrollSettle`,
+ * which treats a held drag as it treats a finger still down.
+ */
+ isDraggingRef: React.RefObject<boolean>;
+};
+
/**
* Let a mouse drag `ref`'s contents, on a container that already scrolls and
* snaps for touch.
@@ -65,7 +84,9 @@ const SETTLE_FALLBACK_MS = 260;
export function usePointerDragScroll(
ref: React.RefObject<HTMLElement | null>,
isEnabled: boolean = true,
-): void {
+): PointerDragScrollState {
+ const isDraggingRef = useRef(false);
+
useEffect(() => {
const element = ref.current;
if (element == null || !isEnabled) {
@@ -75,9 +96,9 @@ export function usePointerDragScroll(
let pointerId: number | null = null;
let originY = 0;
let originScrollTop = 0;
- let isDragging = false;
+ isDraggingRef.current = false;
// Survives from the drag's end to the click it produces, which is the
- // whole reason it is not just `isDragging`.
+ // whole reason it is not just `isDraggingRef`.
let wasDragged = false;
let restoreTimer: number | undefined;
@@ -120,7 +141,7 @@ export function usePointerDragScroll(
pointerId = event.pointerId;
originY = event.clientY;
originScrollTop = element.scrollTop;
- isDragging = false;
+ isDraggingRef.current = false;
// Not captured yet: a press that never moves has to stay a click on the
// row under it, and capturing here would retarget that click.
};
@@ -130,11 +151,11 @@ export function usePointerDragScroll(
return;
}
const delta = event.clientY - originY;
- if (!isDragging) {
+ if (!isDraggingRef.current) {
if (Math.abs(delta) < DRAG_SLOP) {
return;
}
- isDragging = true;
+ isDraggingRef.current = true;
element.setPointerCapture(event.pointerId);
suspendSnap();
// Dragging across rows would otherwise select their text, leaving the
@@ -151,10 +172,10 @@ export function usePointerDragScroll(
return;
}
pointerId = null;
- if (!isDragging) {
+ if (!isDraggingRef.current) {
return;
}
- isDragging = false;
+ isDraggingRef.current = false;
wasDragged = true;
element.style.removeProperty('user-select');
if (element.hasPointerCapture(event.pointerId)) {
@@ -204,9 +225,12 @@ export function usePointerDragScroll(
element.removeEventListener('pointerup', onPointerEnd);
element.removeEventListener('pointercancel', onPointerEnd);
element.removeEventListener('click', onClickCapture, {capture: true});
+ isDraggingRef.current = false;
restoreSnap();
};
}, [ref, isEnabled]);
+
+ return {isDraggingRef};
}
/**
diff --git a/packages/core/src/DateInput/useScrollSettle.ts b/packages/core/src/DateInput/useScrollSettle.ts
index 06c53a69f0..2deb897ddc 100644
--- a/packages/core/src/DateInput/useScrollSettle.ts
+++ b/packages/core/src/DateInput/useScrollSettle.ts
@@ -4,7 +4,8 @@
/**
* @file useScrollSettle.ts
- * @input A scroll container ref and a settle callback
+ * @input A scroll container ref, a settle callback, and optionally whether a
+ * mouse drag holds it (usePointerDragScroll's `isDraggingRef`)
* @output Calls back once, after scrolling (including momentum) stops
* @position Internal behavior hook; consumed by Wheel.tsx and MonthScroller.tsx
*
@@ -27,7 +28,10 @@
* moment without lifting and the scroll events stop, so the timer fires
* and the value commits to whatever is under the band. Chrome hides this
* because it has `scrollend`, which does not fire until the touch is
- * released.
+ * released. A mouse drag (usePointerDragScroll) is held the same way, and
+ * there Chrome gives no cover at all: each `scrollTop` the drag assigns
+ * ends in its own `scrollend`, so the settle waits for the drag's
+ * `pointerup` as it waits for `touchend`.
*
* 2. **Momentum is not rest either.** iOS momentum runs on the UI thread and
* keeps going for a second or more after the finger is gone, and its scroll
@@ -41,7 +45,7 @@
* underneath, and the fresh scroll events start the next premature settle —
* each one landing a row further along, which is why the month climbed.
*
- * So: the settle waits for the touch to end, and then for a quiet period
+ * So: the settle waits for the touch or the mouse drag to end, and then for a quiet period
* measured from the LAST scroll event, which is what makes it outlast
* momentum however long it runs. {@link useScrollSettle} also reports whether
* the scroller is at rest, so a caller can refuse to reposition one that is
@@ -77,11 +81,14 @@ export type ScrollSettleState = {
* @param ref - the scroll container
* @param onSettle - called with the settled element; may change every render
* @param isEnabled - skip attaching while false (e.g. a hidden panel)
+ * @param isHeldRef - true while a mouse drag holds the scroller
+ * (usePointerDragScroll's `isDraggingRef`): not rest, however quiet
*/
export function useScrollSettle(
ref: React.RefObject<HTMLElement | null>,
onSettle: (element: HTMLElement) => void,
isEnabled: boolean = true,
+ isHeldRef?: React.RefObject<boolean>,
): ScrollSettleState {
// The callback closes over render-fresh values (the option list, the current
// value); keeping it in a ref means the listeners attach once instead of
@@ -101,11 +108,18 @@ export function useScrollSettle(
let timer: ReturnType<typeof setTimeout> | undefined;
let hasSettled = false;
let isTouching = false;
+ // Whether a mouse drag held this gesture, so its release re-arms the
+ // settle; a plain click does not.
+ let wasHeld = false;
const settle = () => {
- // A finger still on the glass is not rest, however quiet the scroller
- // has gone. The release re-arms this.
- if (hasSettled || isTouching) {
+ // A finger still on the glass, or a mouse button still holding a drag,
+ // is not rest, however quiet the scroller has gone. The release re-arms
+ // this.
+ if (isHeldRef?.current) {
+ wasHeld = true;
+ }
+ if (hasSettled || isTouching || isHeldRef?.current) {
return;
}
hasSettled = true;
@@ -126,6 +140,9 @@ export function useScrollSettle(
// the last of it however long it runs.
hasSettled = false;
isAtRestRef.current = false;
+ if (isHeldRef?.current) {
+ wasHeld = true;
+ }
arm();
};
@@ -144,11 +161,23 @@ export function useScrollSettle(
arm();
};
+ const onPointerEnd = () => {
+ // The drag's release: its glide to the nearest row re-arms this on its
+ // own, and the timer covers a release already on a row, which scrolls
+ // no further.
+ if (wasHeld) {
+ wasHeld = false;
+ arm();
+ }
+ };
+
element.addEventListener('scroll', onScroll, {passive: true});
element.addEventListener('scrollend', settle);
element.addEventListener('touchstart', onTouchStart, {passive: true});
element.addEventListener('touchend', onTouchEnd, {passive: true});
element.addEventListener('touchcancel', onTouchEnd, {passive: true});
+ element.addEventListener('pointerup', onPointerEnd);
+ element.addEventListener('pointercancel', onPointerEnd);
return () => {
clearTimeout(timer);
@@ -157,8 +186,10 @@ export function useScrollSettle(
element.removeEventListener('touchstart', onTouchStart);
element.removeEventListener('touchend', onTouchEnd);
element.removeEventListener('touchcancel', onTouchEnd);
+ element.removeEventListener('pointerup', onPointerEnd);
+ element.removeEventListener('pointercancel', onPointerEnd);
};
- }, [ref, isEnabled]);
+ }, [ref, isEnabled, isHeldRef]);
return {isAtRestRef};
}