Skip to content

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

TypeScript

fix(core): a held finger stops a paging settle on a busy page too (#10330)

Christopher Chedeau9eee3c4c54a7
diff --git a/.changeset/pager-catch-event-clock.md b/.changeset/pager-catch-event-clock.md
new file mode 100644
index 0000000000..9f7810b2ca
--- /dev/null
+++ b/.changeset/pager-catch-event-clock.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Carousel paging: on a busy page, a finger held on a settling row now stops it, as on an idle page. Before, when the page received the touch and its lift together, a short hold read as a tap and the row ran on to its target.
+
+@vjeux
diff --git a/packages/core/src/hooks/usePager.test.ts b/packages/core/src/hooks/usePager.test.ts
index 244b83f9dc..cef22d471f 100644
--- a/packages/core/src/hooks/usePager.test.ts
+++ b/packages/core/src/hooks/usePager.test.ts
@@ -846,6 +846,8 @@ describe('createPager', () => {
// Still moving 40 ms after the touch.
expect(f.pager.isSettling).toBe(true);
await advance(paging.catchStopMs, f.clock);
+ // The catch's deadline is on the gesture clock.
+ f.clock.runDue(now);
expect(f.pager.isSettling).toBe(false);
const held = -f.x();
await advance(100, f.clock);
@@ -855,6 +857,32 @@ describe('createPager', () => {
expect(f.x()).toBe(-2 * WIDTH);
});
+ it('a finger held past the catch delay catches the settle when a busy page delivers its touch and lift together', async () => {
+ const f = setup({defaultIndex: 0});
+ f.pager.goTo(3);
+ await advance(0, f.clock);
+ await advance(60, f.clock);
+ // One task delivers both events, with no timer turn between them. Their
+ // own times say the finger stayed down 150 ms.
+ touch('touchstart', f.page, 300, 100, now);
+ touch('touchend', f.page, 300, 100, now + 150);
+ await settle(f);
+ const page = -f.x() / WIDTH;
+ expect(page).toBe(Math.round(page));
+ expect(page).toBeLessThan(3);
+ });
+
+ it('a 40 ms tap that a busy page delivers with its lift lets the settle run on', async () => {
+ const f = setup({defaultIndex: 0});
+ f.pager.goTo(3);
+ await advance(0, f.clock);
+ await advance(60, f.clock);
+ touch('touchstart', f.page, 300, 100, now);
+ touch('touchend', f.page, 300, 100, now + 40);
+ await settle(f);
+ expect(f.x()).toBe(-3 * WIDTH);
+ });
+
it('goTo with an offset ends there as a settle, and a still finger that catches it settles on the nearest page', async () => {
const f = setup({defaultIndex: 0});
f.pager.goTo(2, {offset: 2.4 * WIDTH});
@@ -869,6 +897,8 @@ describe('createPager', () => {
await advance(60, f.clock);
touch('touchstart', f.page, 300, 100, now);
await advance(paging.catchStopMs + 40, f.clock);
+ // The catch's deadline is on the gesture clock.
+ f.clock.runDue(now);
expect(f.pager.isSettling).toBe(false);
touch('touchend', f.page, 300, 100, now);
await settle(f);
@@ -986,6 +1016,8 @@ describe('createPager', () => {
await advance(60, f.clock);
touch('touchstart', f.page, 300, 100, now);
await advance(paging.catchStopMs + 40, f.clock);
+ // The catch's deadline is on the gesture clock.
+ f.clock.runDue(now);
expect(pager.isSettling).toBe(false);
const held = f.x();
calls.length = 0;
@@ -2233,6 +2265,7 @@ describe('review fixes', () => {
expect(refuses()).toBe(false);
// The catch stops the settle after UIKit's catch delay, as a finger's.
await advance(120, f.clock);
+ f.clock.runDue(now);
expect(f.pager.isSettling).toBe(false);
expect(refuses()).toBe(false);
mouse('pointerup', f.page, 200, 100, now);
@@ -2443,6 +2476,8 @@ describe('pre-review fixes', () => {
f.pager.setPanEnabled(false);
touch('touchstart', f.page, 200, 100, now);
await advance(paging.catchStopMs + 30, f.clock);
+ // The catch's deadline is on the gesture clock.
+ f.clock.runDue(now);
expect(f.pager.isSettling).toBe(true);
touch('touchend', f.page, 200, 100, now);
await settle(f);
diff --git a/packages/core/src/hooks/usePager.ts b/packages/core/src/hooks/usePager.ts
index f638c56bf8..fbe6e912b2 100644
--- a/packages/core/src/hooks/usePager.ts
+++ b/packages/core/src/hooks/usePager.ts
@@ -314,9 +314,8 @@ export interface PagerEnvironment {
host?: GestureHost;
/** The track's animator; `animatorFor(track)` by default. */
animator?: PagerAnimator;
- /** Timers for the catch delay; the viewport's window's by default. */
+ /** A task timer; the viewport's window's by default. */
setTimeout?: (callback: () => void, ms: number) => unknown;
- clearTimeout?: (handle: unknown) => void;
/** The next animation frame; the viewport's window's by default. */
requestFrame?: (callback: () => void) => void;
}
@@ -397,6 +396,22 @@ class SettleCatcher extends LongPressGestureRecognizer {
return false;
}
+ /**
+ * Calls `callback` `ms` after the touch, on the event clock, unless the
+ * touch ends first. The arena runs it before any input stamped later, so a
+ * busy page that delivers the touch and its lift together still runs it
+ * first. It takes the recognizer's one arena timer, which a press with no
+ * duration and no taps leaves free.
+ */
+ armCatch(ms: number, callback: () => void): void {
+ this.setTimer((this.beganAt ?? this.now()) + ms, callback);
+ }
+
+ /** Drops the catch that `armCatch` set, if it has not run. */
+ disarmCatch(): void {
+ this.clearTimer();
+ }
+
protected override touchesBegan(
touches: ReadonlyArray<GestureTouch>,
event: GestureEventContext,
@@ -485,10 +500,6 @@ export function createPager(
const setTimer =
environment.setTimeout ??
((callback: () => void, ms: number) => realm().setTimeout(callback, ms));
- const clearTimer =
- environment.clearTimeout ??
- ((handle: unknown) =>
- realm().clearTimeout(handle as ReturnType<typeof setTimeout>));
const requestFrame =
environment.requestFrame ??
((callback: () => void) => {
@@ -646,7 +657,7 @@ export function createPager(
let dragStartIndex = 0;
let dragPosition = 0;
let suppressCancelSettle = false;
- let catchTimer: unknown = null;
+ let isCatchTimerArmed = false;
// Whether the finger now holding the track stopped a settle (not one that
// ended on its own inside the catch window).
let caughtSettle = false;
@@ -700,9 +711,9 @@ export function createPager(
}
function clearCatchTimer(): void {
- if (catchTimer != null) {
- clearTimer(catchTimer);
- catchTimer = null;
+ if (isCatchTimerArmed) {
+ isCatchTimerArmed = false;
+ catcher.disarmCatch();
}
}
@@ -1077,18 +1088,22 @@ export function createPager(
holdSelection(true);
clearCatchTimer();
caughtSettle = false;
- catchTimer = setTimer(() => {
- catchTimer = null;
+ isCatchTimerArmed = true;
+ catcher.armCatch(paging.catchStopMs, () => {
+ if (!isCatchTimerArmed) {
+ return;
+ }
+ isCatchTimerArmed = false;
if (settleHandle != null && !isDragging) {
stopSettle();
caughtSettle = true;
}
- }, paging.catchStopMs);
+ });
return;
}
if (recognizer.state === 'ended' || recognizer.state === 'cancelled') {
holdSelection(false);
- if (catchTimer != null) {
+ if (isCatchTimerArmed) {
// Lifted before the catch took hold: the settle runs on.
clearCatchTimer();
return;