Skip to content

Commit 480951b

Browse files
feat(lab): drag Table columns by their reorder handle
A mouse drags a column sideways by its handle at once, and a finger after List's hold. A line marks the drop place nearest the dragged column, the scroll area scrolls near its inline edges at List's speeds, and the columns move once, at the drop. The drag uses List's pointer input and edge autoscroll, with one listener for the table. It reads the header cells' boxes as it begins, and again only once column windowing mounts other cells; each move reads the scroll offset alone.
1 parent f422d75 commit 480951b

6 files changed

Lines changed: 742 additions & 41 deletions

File tree

‎apps/storybook/stories/TableDragAndDropColumns.stories.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ function DefaultDemo() {
118118
}
119119

120120
/**
121-
* Focus the handle at the end of a column's header and press Space or Enter,
122-
* then Left and Right, to move the column one place at a time. Or tap the
123-
* handle, then the hint's moves. Escape or Done ends reorder mode.
121+
* Drag a column by the handle at the end of its header, or focus the handle
122+
* and press Space or Enter, then Left and Right, to move it one place at a
123+
* time. Escape ends reorder mode.
124124
*/
125125
export const Default: StoryObj = {
126126
render: () => <DefaultDemo />,

‎packages/lab/src/TableDragAndDrop/__tests__/TableDragAndDropColumns.a11y.chromium.spec.ts‎

Lines changed: 287 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
/**
44
* @file TableDragAndDropColumns.a11y.chromium.spec.ts
55
* @input The Lab/TableDragAndDropColumns stories in a built Storybook, driven
6-
* by a real Chromium keyboard and mouse
7-
* @output Browser evidence that reorder mode moves a column by keyboard and
8-
* by the hint's buttons, mirrored in right-to-left. It keeps the focus on
9-
* the handle and the column in view, marks the edge it came in through
10-
* with a line, and announces each place. Kept, pinned, revealed and
11-
* windowed columns compose with it
6+
* by a real Chromium mouse, keyboard and touch input (CDP)
7+
* @output Browser evidence that a column drags sideways by its header's
8+
* handle, under a finger after a hold: a line marks the drop between two
9+
* header cells, and the columns move once, at the drop. Its moves read no
10+
* box or scroll offset, and a pinned header cell takes the drop where it
11+
* shows. A drag near the scroll area's edge scrolls it and drops past the
12+
* columns mounted at its start. Reorder mode moves a column by keyboard,
13+
* mirrored in right-to-left, keeps the focus on its handle and in view,
14+
* shows the hint's buttons and announces each place. A pinned, kept column
15+
* bounds the moves, and windowed columns stay windowed
1216
* @position Chromium proof for Lab's useTableDragAndDropColumns
1317
*/
1418

@@ -82,6 +86,177 @@ const lineAt = async (page: Page) =>
8286

8387
const scrollAreaOf = (page: Page) => page.locator(`${ROOT} [role="group"]`);
8488

89+
test('a mouse drags a column by its handle: a line marks the drop between two header cells, and the columns move once, at the drop', async ({
90+
page,
91+
}) => {
92+
await openStory(page, 'lab-tabledraganddropcolumns--default');
93+
const before = await headers(page);
94+
expect(before.map(cell => cell.text)).toEqual([
95+
'Task',
96+
'Owner',
97+
'Status',
98+
'Due',
99+
'Points',
100+
]);
101+
const box = await handleOf(page, 'Owner').boundingBox();
102+
if (box == null) {
103+
throw new Error('The Owner handle must render.');
104+
}
105+
const x = box.x + box.width / 2;
106+
const y = box.y + box.height / 2;
107+
await page.mouse.move(x, y);
108+
await page.mouse.down();
109+
await page.mouse.move(x + 12, y, {steps: 3});
110+
await page.mouse.move(x + 300, y, {steps: 10});
111+
// The line stands at the end edge of Due, the column the drop passes.
112+
const line = await lineAt(page);
113+
expect(line?.center).toBeGreaterThanOrEqual(before[3].right - 2);
114+
expect(line?.center).toBeLessThanOrEqual(before[3].right + 2);
115+
expect(line?.height).toBeGreaterThan(150);
116+
// Nothing moves until the drop.
117+
await expect(page.getByTestId('order')).toHaveText(
118+
'title owner status due points',
119+
);
120+
await page.mouse.up();
121+
await expect(page.getByTestId('order')).toHaveText(
122+
'title status due owner points',
123+
);
124+
expect((await headers(page)).map(cell => cell.text)).toEqual([
125+
'Task',
126+
'Status',
127+
'Due',
128+
'Owner',
129+
'Points',
130+
]);
131+
expect(await lineAt(page)).toBeNull();
132+
await expect
133+
.poll(() => announced(page))
134+
.toContain('Dropped, position 4 of 5');
135+
});
136+
137+
test('a drag reads the header cells as it begins, and no box or scroll offset on each move', async ({
138+
page,
139+
}) => {
140+
await openStory(page, 'lab-tabledraganddropcolumns--default');
141+
const box = await handleOf(page, 'Owner').boundingBox();
142+
if (box == null) {
143+
throw new Error('The handle must render.');
144+
}
145+
const x = box.x + box.width / 2;
146+
const y = box.y + box.height / 2;
147+
await page.mouse.move(x, y);
148+
await page.mouse.down();
149+
await page.mouse.move(x + 12, y, {steps: 3});
150+
// Counts the boxes read while the page handles each pointer move.
151+
await page.evaluate(() => {
152+
const reads = {count: 0, isMoving: false};
153+
(window as unknown as {reads: typeof reads}).reads = reads;
154+
window.addEventListener(
155+
'pointermove',
156+
() => {
157+
reads.isMoving = true;
158+
},
159+
true,
160+
);
161+
window.addEventListener('pointermove', () => {
162+
reads.isMoving = false;
163+
});
164+
const read = Element.prototype.getBoundingClientRect;
165+
Element.prototype.getBoundingClientRect = function (this: Element) {
166+
if (reads.isMoving) {
167+
reads.count += 1;
168+
}
169+
return read.call(this);
170+
};
171+
const offset = Object.getOwnPropertyDescriptor(
172+
Element.prototype,
173+
'scrollLeft',
174+
);
175+
Object.defineProperty(Element.prototype, 'scrollLeft', {
176+
...offset,
177+
get(this: Element) {
178+
if (reads.isMoving) {
179+
reads.count += 1;
180+
}
181+
return offset?.get?.call(this);
182+
},
183+
});
184+
});
185+
await page.mouse.move(x + 120, y, {steps: 10});
186+
expect(
187+
await page.evaluate(
188+
() => (window as unknown as {reads: {count: number}}).reads.count,
189+
),
190+
).toBe(0);
191+
await page.mouse.up();
192+
});
193+
194+
test('a pinned header cell takes the drop where it shows after the table scrolls under it', async ({
195+
page,
196+
}) => {
197+
await page.setViewportSize({width: 520, height: 800});
198+
await openStory(page, 'lab-tabledraganddropcolumns--default');
199+
const task = page.locator(`${ROOT} thead th[data-column-key="title"]`);
200+
// Pins the Task column's header cell, as a pinned column's sticks.
201+
await task.evaluate(cell => {
202+
Object.assign((cell as HTMLElement).style, {
203+
position: 'sticky',
204+
insetInlineStart: '0px',
205+
zIndex: '2',
206+
});
207+
});
208+
const box = await handleOf(page, 'Owner').boundingBox();
209+
if (box == null) {
210+
throw new Error('The handle must render.');
211+
}
212+
const y = box.y + box.height / 2;
213+
await page.mouse.move(box.x + box.width / 2, y);
214+
await page.mouse.down();
215+
await page.mouse.move(box.x + box.width / 2 + 12, y, {steps: 3});
216+
// Owner scrolls under the pinned Task header cell.
217+
await scrollAreaOf(page).evaluate(area => {
218+
area.scrollLeft = 240;
219+
});
220+
const pinned = await task.boundingBox();
221+
if (pinned == null) {
222+
throw new Error('The pinned header cell must render.');
223+
}
224+
await page.mouse.move(pinned.x + 200, y, {steps: 5});
225+
const line = await lineAt(page);
226+
expect(line?.center).toBeGreaterThanOrEqual(pinned.x - 2);
227+
expect(line?.center).toBeLessThanOrEqual(pinned.x + 2);
228+
await page.mouse.up();
229+
await expect(page.getByTestId('order')).toHaveText(
230+
'owner title status due points',
231+
);
232+
});
233+
234+
test('a drag released back on its handle drops the column in its place and turns on no reorder mode', async ({
235+
page,
236+
}) => {
237+
await openStory(page, 'lab-tabledraganddropcolumns--default');
238+
const handle = handleOf(page, 'Owner');
239+
const box = await handle.boundingBox();
240+
if (box == null) {
241+
throw new Error('The Owner handle must render.');
242+
}
243+
const x = box.x + box.width / 2;
244+
const y = box.y + box.height / 2;
245+
await page.mouse.move(x, y);
246+
await page.mouse.down();
247+
await page.mouse.move(x + 60, y, {steps: 6});
248+
await page.mouse.move(x, y, {steps: 6});
249+
// The release on the handle also clicks it.
250+
await page.mouse.up();
251+
await expect
252+
.poll(() => announced(page))
253+
.toContain('Dropped, position 2 of 5');
254+
await expect(handle).toHaveAttribute('aria-pressed', 'false');
255+
await expect(page.getByTestId('order')).toHaveText(
256+
'title owner status due points',
257+
);
258+
});
259+
85260
test('reorder mode moves a column by keyboard, keeps the focus on its handle, shows the hint and a line at the edge it came in through, and announces each place', async ({
86261
page,
87262
}) => {
@@ -194,7 +369,7 @@ test('a handle the reorderable reveals shows while its header cell is hovered or
194369
await expect(handle).toHaveCSS('opacity', '1');
195370
});
196371

197-
test('windowed columns stay windowed; reorder mode keeps a moving column in view and names unmounted neighbours', async ({
372+
test('windowed columns stay windowed; reorder mode keeps a moving column in view and names unmounted neighbours, and a drag near the edge scrolls and drops past the columns mounted at its start', async ({
198373
page,
199374
}) => {
200375
await openStory(page, 'lab-tabledraganddropcolumns--windowed-columns');
@@ -215,4 +390,109 @@ test('windowed columns stay windowed; reorder mode keeps a moving column in view
215390
await expect(handle).toBeInViewport();
216391
await page.keyboard.press('Escape');
217392
expect(await mounted()).toBeLessThan(20);
393+
394+
await scrollAreaOf(page).evaluate(area => {
395+
area.scrollLeft = 0;
396+
});
397+
const mountedAtStart = await mounted();
398+
const first = handleOf(page, 'Metric 1');
399+
const box = await first.boundingBox();
400+
const area = await scrollAreaOf(page).boundingBox();
401+
if (box == null || area == null) {
402+
throw new Error('The handle and the scroll area must render.');
403+
}
404+
const x = box.x + box.width / 2;
405+
const y = box.y + box.height / 2;
406+
await page.mouse.move(x, y);
407+
await page.mouse.down();
408+
await page.mouse.move(x + 12, y, {steps: 3});
409+
await page.mouse.move(area.x + area.width - 8, y, {steps: 20});
410+
// List's autoscroll speed: past several columns mounted at the start.
411+
await expect
412+
.poll(() => scrollAreaOf(page).evaluate(element => element.scrollLeft), {
413+
timeout: 10_000,
414+
})
415+
.toBeGreaterThan(1600);
416+
await page.mouse.move(area.x + area.width / 2, y, {steps: 5});
417+
await page.mouse.up();
418+
await expect
419+
.poll(() => announced(page))
420+
.toMatch(/Dropped, position (\d+) of 60/);
421+
const position = Number(
422+
/Dropped, position (\d+) of 60/.exec(await announced(page))?.[1],
423+
);
424+
// Past the columns mounted when the drag began.
425+
expect(position).toBeGreaterThan(mountedAtStart);
426+
expect(await mounted()).toBeLessThan(20);
427+
});
428+
429+
test.describe('on a touch screen', () => {
430+
test.use({
431+
hasTouch: true,
432+
isMobile: true,
433+
viewport: {width: 390, height: 844},
434+
deviceScaleFactor: 3,
435+
});
436+
437+
test('a finger held still on a handle drags its column sideways, and a flick drags none', async ({
438+
context,
439+
page,
440+
}) => {
441+
await openStory(page, 'lab-tabledraganddropcolumns--default');
442+
// The Task column scrolls away, so the drag stays clear of the edges.
443+
await scrollAreaOf(page).evaluate(area => {
444+
area.scrollLeft = 240;
445+
});
446+
const box = await handleOf(page, 'Owner').boundingBox();
447+
const owner = await page
448+
.locator(`${ROOT} thead th`, {hasText: 'Owner'})
449+
.boundingBox();
450+
const status = await page
451+
.locator(`${ROOT} thead th`, {hasText: 'Status'})
452+
.boundingBox();
453+
if (box == null || owner == null || status == null) {
454+
throw new Error('The handle and the headers must render.');
455+
}
456+
const cdp = await context.newCDPSession(page);
457+
const x = box.x + box.width / 2;
458+
const y = box.y + box.height / 2;
459+
const touch = (
460+
type: 'touchStart' | 'touchMove' | 'touchEnd',
461+
points: Array<{x: number; y: number}>,
462+
) =>
463+
cdp.send('Input.dispatchTouchEvent', {
464+
type,
465+
touchPoints: points.map(point => ({...point, id: 1})),
466+
});
467+
// To the centre of Status, the place the drop takes.
468+
const distance = owner.width / 2 + status.width / 2;
469+
// The hold claims the drag after the content-touch delay.
470+
await touch('touchStart', [{x, y}]);
471+
await page.waitForTimeout(700);
472+
for (let step = 1; step <= 10; step++) {
473+
await touch('touchMove', [{x: x + (distance * step) / 10, y}]);
474+
}
475+
await expect(page.getByTestId('order')).toHaveText(
476+
'title owner status due points',
477+
);
478+
await touch('touchEnd', []);
479+
await expect(page.getByTestId('order')).toHaveText(
480+
'title status owner due points',
481+
);
482+
// A flick that starts on a handle scrolls the table and moves no column.
483+
const flick = await handleOf(page, 'Owner').boundingBox();
484+
if (flick == null) {
485+
throw new Error('The handle must render.');
486+
}
487+
const from = flick.x + flick.width / 2;
488+
const at = flick.y + flick.height / 2;
489+
await touch('touchStart', [{x: from, y: at}]);
490+
for (let step = 1; step <= 4; step++) {
491+
await touch('touchMove', [{x: from - (distance * step) / 4, y: at}]);
492+
}
493+
await touch('touchEnd', []);
494+
await expect(page.getByTestId('order')).toHaveText(
495+
'title status owner due points',
496+
);
497+
});
218498
});

0 commit comments

Comments
 (0)