Skip to content

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

TypeScript

fix(List): a drag follows a scroll around the list's scroll box (#10742)

Christopher Chedeau06ebae229c55
diff --git a/.changeset/list-reorder-wheel-inner-scroller.md b/.changeset/list-reorder-wheel-inner-scroller.md
new file mode 100644
index 0000000000..60d10c23ce
--- /dev/null
+++ b/.changeset/list-reorder-wheel-inner-scroller.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A reorderable `List` in its own scroll box keeps the dragged row under the pointer when a wheel during a mouse or pen drag scrolls the page, as a wheel at the box's end does. The wheel scrolls as before.
+@vjeux
diff --git a/packages/core/locales/en.json b/packages/core/locales/en.json
index 6233d0e778..0d878863cd 100644
--- a/packages/core/locales/en.json
+++ b/packages/core/locales/en.json
@@ -661,7 +661,7 @@
},
"@astryx.list.reorderPickedUp": {
"defaultMessage": "Picked up, position {position, number} of {total, number}.",
- "description": "Screen-reader announcement when a row of a List is picked up to be reordered, from its handle's keys or by a drag. `{position}` is the row's 1-based place, `{total}` the number of rows."
+ "description": "Screen-reader announcement when a drag picks up a row of a List to reorder it. `{position}` is the row's 1-based place, `{total}` the number of rows."
},
"@astryx.list.reorderDropped": {
"defaultMessage": "Dropped, position {position, number} of {total, number}",
diff --git a/packages/core/src/List/ListReorderTouch.ts b/packages/core/src/List/ListReorderTouch.ts
index f1e409f01b..d50ca84e58 100644
--- a/packages/core/src/List/ListReorderTouch.ts
+++ b/packages/core/src/List/ListReorderTouch.ts
@@ -17,8 +17,8 @@
*
* UIKit's reorder control (iOS 26.2, measured): a finger held still on it for
* the scroll view's content-touch delay (150 ms) claims the drag. A flick that
- * starts on it scrolls the table instead. So the handle lets the browser pan
- * vertically and pinch-zoom until the claim, through its own `touch-action`.
+ * starts on it scrolls the table instead. So until the claim, the handle
+ * leaves the browser its pans and pinch-zoom (`manipulation`).
* The long press then holds the touch, through the gesture system's blocking
* listener for a mid-gesture claim. A second finger before the claim makes
* it the browser's pinch (WCAG 1.4.4). After the claim, extra fingers change
diff --git a/packages/core/src/List/__tests__/ListReorderPointer.interaction.browser.spec.ts b/packages/core/src/List/__tests__/ListReorderPointer.interaction.browser.spec.ts
index 2a698ccf10..e38996f6d2 100644
--- a/packages/core/src/List/__tests__/ListReorderPointer.interaction.browser.spec.ts
+++ b/packages/core/src/List/__tests__/ListReorderPointer.interaction.browser.spec.ts
@@ -13,7 +13,9 @@
* scrolls in, and stops at the end. A wheel during a drag scrolls the
* page, and the preview and the drop stay under the pointer. Each scrolled
* frame reads the scroll offset once at most and forces no layout. A drop
- * measures the rows in one pass, before its writes, whatever their count. The
+ * measures the rows in one pass, before its writes, whatever their count. A
+ * wheel at a box's end scrolls the page or a box around it, and the drop
+ * still lands under the pointer. The
* preview and the insertion line run along a row set in from the list's
* start, in either direction. A checked radio in the dragged row stays
* checked through the drag. With a mouse, the handle keeps a plain
@@ -247,7 +249,7 @@ test('a wheel during a drag scrolls the page, and the preview and the drop stay
// shows the preview and the line under the pointer again.
await page.evaluate(target => {
const state = {frame: 0, lastScroll: -1, settledAt: -1};
- Object.assign(window, {c06Frames: state});
+ Object.assign(window, {scrollFrames: state});
const check = () => {
if (state.lastScroll < 0 || state.settledAt >= 0) {
return;
@@ -306,9 +308,9 @@ test('a wheel during a drag scrolls the page, and the preview and the drop stay
() =>
(
window as unknown as {
- c06Frames: {lastScroll: number; settledAt: number};
+ scrollFrames: {lastScroll: number; settledAt: number};
}
- ).c06Frames,
+ ).scrollFrames,
);
// The preview and the line come back under the pointer.
await expect
@@ -353,7 +355,7 @@ test('a wheel during a drag forces no layout, and reads the scroll offset once a
'scrollTop',
);
const counts = {reads: 0, frames: 0};
- Object.assign(window, {c06Counts: counts});
+ Object.assign(window, {scrollCounts: counts});
Object.defineProperty(scroller, 'scrollTop', {
configurable: true,
get() {
@@ -407,8 +409,8 @@ test('a wheel during a drag forces no layout, and reads the scroll offset once a
};
const counts = await page.evaluate(
() =>
- (window as unknown as {c06Counts: {reads: number; frames: number}})
- .c06Counts,
+ (window as unknown as {scrollCounts: {reads: number; frames: number}})
+ .scrollCounts,
);
await page.mouse.up();
const mainThreads = new Set(
@@ -439,6 +441,126 @@ test('a wheel during a drag forces no layout, and reads the scroll offset once a
}
});
+test('a wheel at the end of a box during a drag forces no layout, and measures the box once a scrolled frame at most', async ({
+ browser,
+ browserName,
+}) => {
+ test.skip(browserName !== 'chromium', 'the layout trace is Chromium CDP');
+ const story = await openFinePointerStory(browser, LONG_STORY);
+ try {
+ const {page} = story;
+ // The list scrolls in a 400 px box, scrolled to its end, on a page that
+ // can scroll too.
+ await page.evaluate(() => {
+ const root = document.querySelector<HTMLElement>('#storybook-root');
+ if (root == null) {
+ return;
+ }
+ root.style.blockSize = '400px';
+ root.style.overflowY = 'auto';
+ const room = document.createElement('div');
+ room.style.blockSize = '2000px';
+ root.after(room);
+ root.scrollTop = root.scrollHeight;
+ });
+ const order = await labels(page);
+ const {at, rowHeight} = await handleOf(page, order[order.length - 4]);
+ await page.mouse.move(at.x, at.y);
+ await page.mouse.down();
+ await page.mouse.move(at.x, at.y - rowHeight * 1.5, {steps: 8});
+ await expect(page.locator('[data-astryx-reorder-preview]')).toHaveCount(1);
+ // Counts the measures of the box, and the frames a scroll of the box or
+ // the page lands in.
+ await page.evaluate(() => {
+ const box = document.querySelector<HTMLElement>('#storybook-root');
+ if (box == null) {
+ return;
+ }
+ const counts = {reads: 0, frames: 0};
+ Object.assign(window, {boxCounts: counts});
+ const measure = Element.prototype.getBoundingClientRect;
+ box.getBoundingClientRect = function getBoundingClientRect() {
+ counts.reads += 1;
+ return measure.call(this);
+ };
+ let isCounted = false;
+ document.addEventListener(
+ 'scroll',
+ () => {
+ if (!isCounted) {
+ isCounted = true;
+ counts.frames += 1;
+ requestAnimationFrame(() => {
+ isCounted = false;
+ });
+ }
+ },
+ {capture: true, passive: true},
+ );
+ });
+ await browser.startTracing(page, {
+ categories: [
+ 'devtools.timeline',
+ 'disabled-by-default-devtools.timeline',
+ 'disabled-by-default-devtools.timeline.stack',
+ ],
+ });
+ // The box is at its end, so the wheel scrolls the page.
+ await page.mouse.wheel(0, rowHeight * 2);
+ // The pointer moves too, so moves and scrolls share frames.
+ for (let step = 1; step <= 16; step += 1) {
+ await page.mouse.move(at.x, at.y - rowHeight * 1.5 - step);
+ await page.waitForTimeout(16);
+ }
+ await page.waitForTimeout(400);
+ const trace = JSON.parse((await browser.stopTracing()).toString()) as {
+ traceEvents: {
+ name: string;
+ ph: string;
+ pid: number;
+ tid: number;
+ args?: {
+ name?: string;
+ beginData?: {stackTrace?: {functionName?: string}[]};
+ };
+ }[];
+ };
+ const counts = await page.evaluate(
+ () =>
+ (window as unknown as {boxCounts: {reads: number; frames: number}})
+ .boxCounts,
+ );
+ const scrollY = await page.evaluate(() => window.scrollY);
+ await page.mouse.up();
+ const mainThreads = new Set(
+ trace.traceEvents
+ .filter(e => e.ph === 'M' && e.name === 'thread_name')
+ .filter(e => e.args?.name === 'CrRendererMain')
+ .map(e => `${e.pid}:${e.tid}`),
+ );
+ // A style or layout pass with a script stack ran inside script: a read
+ // after a write forced it, instead of the frame's own pass.
+ const forced = trace.traceEvents.filter(
+ e =>
+ (e.name === 'Layout' || e.name === 'UpdateLayoutTree') &&
+ mainThreads.has(`${e.pid}:${e.tid}`) &&
+ (e.args?.beginData?.stackTrace ?? []).length > 0,
+ );
+ expect(
+ forced.map(
+ e =>
+ `${e.name} in ${e.args?.beginData?.stackTrace?.[0]?.functionName ?? '?'}`,
+ ),
+ ).toEqual([]);
+ expect(scrollY).toBeGreaterThan(rowHeight);
+ expect(counts.frames).toBeGreaterThan(0);
+ expect(counts.reads).toBeGreaterThan(0);
+ expect(counts.reads).toBeLessThanOrEqual(counts.frames);
+ } finally {
+ await story.close();
+ }
+});
+
test('a drop measures the rows in one pass, before its writes, whatever their count', async ({
browser,
browserName,
@@ -524,6 +646,146 @@ test('a row held near the bottom of a box the list scrolls in scrolls that box,
}
});
+for (const outer of [
+ 'the page',
+ 'the box around it',
+ 'the page under a fixed box',
+ 'the page under a stuck sticky box',
+ 'the page under a sticky box before it sticks',
+] as const) {
+ test(`a wheel at the end of a box the list scrolls in scrolls ${outer} during a drag, and the drop lands under the pointer`, async ({
+ browser,
+ }) => {
+ const story = await openFinePointerStory(browser, LONG_STORY);
+ try {
+ const {page} = story;
+ const wrapped = outer === 'the box around it';
+ const fixed = outer === 'the page under a fixed box';
+ const sticky = outer.includes('sticky');
+ const spaced = outer.endsWith('before it sticks');
+ // The list scrolls in a 400 px box, scrolled to its end. What is around
+ // it can scroll too: the page, or a box that is the box's direct parent.
+ // A fixed box, or a stuck sticky one, stays put while the page scrolls
+ // under it. A sticky box below its sticking point moves with the page.
+ await page.evaluate(
+ ([inBox, isFixed, isSticky, isSpaced]) => {
+ const root = document.querySelector<HTMLElement>('#storybook-root');
+ if (root == null) {
+ return;
+ }
+ root.style.blockSize = '400px';
+ root.style.overflowY = 'auto';
+ if (isFixed) {
+ root.style.position = 'fixed';
+ root.style.insetBlockStart = '0';
+ root.style.insetInlineStart = '0';
+ root.style.inlineSize = '800px';
+ }
+ if (isSticky) {
+ root.style.position = 'sticky';
+ root.style.insetBlockStart = '0';
+ }
+ if (isSpaced) {
+ root.style.marginBlockStart = '300px';
+ }
+ if (inBox) {
+ const around = document.createElement('div');
+ around.id = 'reorder-around';
+ around.style.blockSize = '600px';
+ around.style.overflowY = 'auto';
+ const room = document.createElement('div');
+ room.style.blockSize = '2000px';
+ root.before(around);
+ around.append(root, room);
+ } else {
+ // Room inside the body, so a sticky box has room to stick.
+ const room = document.createElement('div');
+ room.style.blockSize = '2000px';
+ root.after(room);
+ }
+ root.scrollTop = root.scrollHeight;
+ },
+ [wrapped, fixed, sticky, spaced] as const,
+ );
+ const order = await labels(page);
+ const target = order[order.length - 4];
+ const {at, rowHeight} = await handleOf(page, target);
+ await page.mouse.move(at.x, at.y);
+ await page.mouse.down();
+ await page.mouse.move(at.x, at.y - rowHeight * 1.5, {steps: 8});
+ await expect(page.locator('[data-astryx-reorder-preview]')).toHaveCount(
+ 1,
+ );
+ // Two rows: what is around the box moves it up, and the pointer stays
+ // over it.
+ await page.mouse.wheel(0, rowHeight * 2);
+ // Rendered frames, not time: the wheel's scroll would start within them.
+ await page.evaluate(
+ async () =>
+ new Promise<void>(resolve => {
+ let frames = 0;
+ const tick = () => {
+ frames += 1;
+ if (frames >= 30) {
+ resolve();
+ } else {
+ requestAnimationFrame(tick);
+ }
+ };
+ requestAnimationFrame(tick);
+ }),
+ );
+ // The box is at its end, so the wheel scrolls what is around it, as
+ // browsers do.
+ expect(
+ await page.evaluate(
+ inBox =>
+ inBox
+ ? (document.getElementById('reorder-around')?.scrollTop ?? 0)
+ : window.scrollY,
+ wrapped,
+ ),
+ ).toBeGreaterThan(rowHeight);
+ const pointerY = at.y - rowHeight * 1.5;
+ // The pointer is still over the box.
+ const box = await page.locator('#storybook-root').boundingBox();
+ if (box == null) {
+ throw new Error('no box');
+ }
+ expect(box.y + box.height).toBeGreaterThan(pointerY);
+ // The preview stays under the pointer as the box moves.
+ const preview = await page
+ .locator('[data-astryx-reorder-preview]')
+ .boundingBox();
+ if (preview == null) {
+ throw new Error('no preview');
+ }
+ expect(preview.y).toBeLessThanOrEqual(pointerY);
+ expect(preview.y + preview.height).toBeGreaterThanOrEqual(pointerY);
+ await page.mouse.up();
+ // The row lands under the pointer.
+ await expect
+ .poll(
+ async () =>
+ page.evaluate(
+ ({label, y}) => {
+ const row = Array.from(
+ document.querySelectorAll('#storybook-root ul > li'),
+ ).find(element => element.textContent?.trim() === label);
+ const box = row?.getBoundingClientRect();
+ return box != null && box.top <= y && y <= box.bottom;
+ },
+ {label: target, y: pointerY},
+ ),
+ {message: 'the dropped row lands under the pointer', timeout: 600},
+ )
+ .toBe(true);
+ } finally {
+ await story.close();
+ }
+ });
+}
+
test('a checked radio in the dragged row stays checked through the drag and a drop in place', async ({
browser,
}) => {
diff --git a/packages/core/src/List/listReorderEngine.tsx b/packages/core/src/List/listReorderEngine.tsx
index 41f10bdc56..461bd1efad 100644
--- a/packages/core/src/List/listReorderEngine.tsx
+++ b/packages/core/src/List/listReorderEngine.tsx
@@ -429,8 +429,8 @@ function isTransparent(color: string): boolean {
/** The element autoscroll writes to and its visible band in the viewport. */
interface Scroller {
readonly element: Element;
- readonly top: number;
- readonly bottom: number;
+ top: number;
+ bottom: number;
readonly max: number;
position: number;
}
@@ -479,7 +479,10 @@ interface DragSession {
/** The finger's distance from the dragged row's top. */
readonly grabOffset: number;
readonly scroller: Scroller;
- /** How far autoscroll has moved the content since the begin. */
+ /**
+ * How far the list has moved up on screen since the begin. Autoscroll moves
+ * it, and so does any scroll of its scroller or of one around it.
+ */
scrolled: number;
lastY: number;
/** The edge autoscroll's timer, on UIKit's 20 ms clock. */
@@ -1405,14 +1408,27 @@ export class ListReorderController implements ListReorderSession {
return () => {};
}
const {element} = session.scroller;
+ const page = doc.scrollingElement ?? doc.documentElement;
// The page's own scroll is reported on the document.
- const target =
- element === (doc.scrollingElement ?? doc.documentElement) ? doc : element;
+ const own = element === page ? doc : element;
+ // A scroll around the list's scroller can move it on screen too, as when
+ // a wheel at the end of a box scrolls the page.
+ const isAround = (target: EventTarget) =>
+ own !== doc &&
+ (target === doc ||
+ (target instanceof Element &&
+ target !== element &&
+ target.contains(element)));
return subscribeInput(view, 'scroll', event => {
- if (event.target !== target || this.session !== session) {
+ const {target} = event;
+ if (
+ target == null ||
+ this.session !== session ||
+ (target !== own && !isAround(target))
+ ) {
return;
}
- if (session.ownScroll) {
+ if (target === own && session.ownScroll) {
session.ownScroll = false;
return;
}
@@ -1460,9 +1476,22 @@ export class ListReorderController implements ListReorderSession {
private syncScroll(session: DragSession): void {
session.scrollStale = false;
const {scroller} = session;
- const position = scroller.element.scrollTop;
+ const {element} = scroller;
+ const position = element.scrollTop;
session.scrolled += position - scroller.position;
scroller.position = position;
+ const doc = element.ownerDocument;
+ if (element === (doc.scrollingElement ?? doc.documentElement)) {
+ return;
+ }
+ // An outer scroll moves the scroller on screen by the change in its
+ // measured top. A fixed or stuck scroller does not move. The list moves
+ // with its scroller.
+ const top = element.getBoundingClientRect().top + element.clientTop;
+ const moved = scroller.top - top;
+ session.scrolled += moved;
+ scroller.top = top;
+ scroller.bottom -= moved;
}
private stopAutoscroll(session: DragSession): void {
diff --git a/packages/lab/src/ListInput/ListInput.tsx b/packages/lab/src/ListInput/ListInput.tsx
index 27d9be58c6..4d05f1af3d 100644
--- a/packages/lab/src/ListInput/ListInput.tsx
+++ b/packages/lab/src/ListInput/ListInput.tsx
@@ -590,10 +590,7 @@ export function ListInput<T>({
// Each record row reveals its row actions (Remove and, when reorderable, the
// reorder handle) on hover or keyboard focus; touch keeps them visible.
const {getContainerProps, getContentRevealProps} = useContainerReveal();
- // Core List's reorder session, run by the owner's object. The records are
- // the list's rows, each ending with its handle. Each move goes to the
- // object's onReorder. A disabled or loading list keeps its handles,
- // focusable but inert.
+ // A disabled or loading list keeps its handles focusable but inert.
const reorderScope = useListReorderScope(
reorderable,
() => listRef.current,