Skip to content

Commit 7d8bbbc

Browse files
feat(lab): drag Table columns by their reorder handle
A mouse, a pen or a finger drags a column of useTableDragAndDropColumns sideways by its handle, through Core's public pan. A line marks the drop place nearest the dragged column, the scroll area scrolls near its inline edges and reaches the columns that windowing mounts, and the columns move once, at the release, with Core's picked-up, dropped and cancelled announcements.
1 parent b8fb4a8 commit 7d8bbbc

6 files changed

Lines changed: 476 additions & 25 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: 168 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
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: a line marks the drop between two header cells, and the columns
9+
* move once, at the drop. A drag near the scroll area's edge scrolls it and
10+
* drops past the columns mounted at its start. Reorder mode moves a column
11+
* by keyboard, mirrored in right-to-left, keeps the focus on its handle and
12+
* in view, shows the hint's buttons and announces each place. A pinned,
13+
* kept column bounds the moves, and windowed columns stay windowed
1214
* @position Chromium proof for Lab's useTableDragAndDropColumns
1315
*/
1416

@@ -82,6 +84,80 @@ const lineAt = async (page: Page) =>
8284

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

87+
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 ({
88+
page,
89+
}) => {
90+
await openStory(page, 'lab-tabledraganddropcolumns--default');
91+
const before = await headers(page);
92+
expect(before.map(cell => cell.text)).toEqual([
93+
'Task',
94+
'Owner',
95+
'Status',
96+
'Due',
97+
'Points',
98+
]);
99+
const box = await handleOf(page, 'Owner').boundingBox();
100+
if (box == null) {
101+
throw new Error('The Owner handle must render.');
102+
}
103+
const x = box.x + box.width / 2;
104+
const y = box.y + box.height / 2;
105+
await page.mouse.move(x, y);
106+
await page.mouse.down();
107+
await page.mouse.move(x + 12, y, {steps: 3});
108+
await page.mouse.move(x + 300, y, {steps: 10});
109+
// The line stands at the end edge of Due, the column the drop passes.
110+
const line = await lineAt(page);
111+
expect(line?.center).toBeGreaterThanOrEqual(before[3].right - 2);
112+
expect(line?.center).toBeLessThanOrEqual(before[3].right + 2);
113+
expect(line?.height).toBeGreaterThan(150);
114+
// Nothing moves until the drop.
115+
await expect(page.getByTestId('order')).toHaveText(
116+
'title owner status due points',
117+
);
118+
await page.mouse.up();
119+
await expect(page.getByTestId('order')).toHaveText(
120+
'title status due owner points',
121+
);
122+
expect((await headers(page)).map(cell => cell.text)).toEqual([
123+
'Task',
124+
'Status',
125+
'Due',
126+
'Owner',
127+
'Points',
128+
]);
129+
expect(await lineAt(page)).toBeNull();
130+
await expect
131+
.poll(() => announced(page))
132+
.toContain('Dropped, position 4 of 5');
133+
});
134+
135+
test('a drag released back on its handle drops the column in its place and turns on no reorder mode', async ({
136+
page,
137+
}) => {
138+
await openStory(page, 'lab-tabledraganddropcolumns--default');
139+
const handle = handleOf(page, 'Owner');
140+
const box = await handle.boundingBox();
141+
if (box == null) {
142+
throw new Error('The Owner handle must render.');
143+
}
144+
const x = box.x + box.width / 2;
145+
const y = box.y + box.height / 2;
146+
await page.mouse.move(x, y);
147+
await page.mouse.down();
148+
await page.mouse.move(x + 60, y, {steps: 6});
149+
await page.mouse.move(x, y, {steps: 6});
150+
// The release on the handle also clicks it.
151+
await page.mouse.up();
152+
await expect
153+
.poll(() => announced(page))
154+
.toContain('Dropped, position 2 of 5');
155+
await expect(handle).toHaveAttribute('aria-pressed', 'false');
156+
await expect(page.getByTestId('order')).toHaveText(
157+
'title owner status due points',
158+
);
159+
});
160+
85161
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 ({
86162
page,
87163
}) => {
@@ -194,7 +270,7 @@ test('a handle the reorderable reveals shows while its header cell is hovered or
194270
await expect(handle).toHaveCSS('opacity', '1');
195271
});
196272

197-
test('windowed columns stay windowed; reorder mode keeps a moving column in view and names unmounted neighbours', async ({
273+
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 ({
198274
page,
199275
}) => {
200276
await openStory(page, 'lab-tabledraganddropcolumns--windowed-columns');
@@ -215,4 +291,89 @@ test('windowed columns stay windowed; reorder mode keeps a moving column in view
215291
await expect(handle).toBeInViewport();
216292
await page.keyboard.press('Escape');
217293
expect(await mounted()).toBeLessThan(20);
294+
295+
await scrollAreaOf(page).evaluate(area => {
296+
area.scrollLeft = 0;
297+
});
298+
const mountedAtStart = await mounted();
299+
const first = handleOf(page, 'Metric 1');
300+
const box = await first.boundingBox();
301+
const area = await scrollAreaOf(page).boundingBox();
302+
if (box == null || area == null) {
303+
throw new Error('The handle and the scroll area must render.');
304+
}
305+
const x = box.x + box.width / 2;
306+
const y = box.y + box.height / 2;
307+
await page.mouse.move(x, y);
308+
await page.mouse.down();
309+
await page.mouse.move(x + 12, y, {steps: 3});
310+
await page.mouse.move(area.x + area.width - 8, y, {steps: 20});
311+
await expect
312+
.poll(() => scrollAreaOf(page).evaluate(element => element.scrollLeft))
313+
.toBeGreaterThan(600);
314+
await page.mouse.move(area.x + area.width / 2, y, {steps: 5});
315+
await page.mouse.up();
316+
await expect
317+
.poll(() => announced(page))
318+
.toMatch(/Dropped, position (\d+) of 60/);
319+
const position = Number(
320+
/Dropped, position (\d+) of 60/.exec(await announced(page))?.[1],
321+
);
322+
// Past the columns mounted when the drag began.
323+
expect(position).toBeGreaterThan(mountedAtStart);
324+
expect(await mounted()).toBeLessThan(20);
325+
});
326+
327+
test.describe('on a touch screen', () => {
328+
test.use({
329+
hasTouch: true,
330+
isMobile: true,
331+
viewport: {width: 390, height: 844},
332+
deviceScaleFactor: 3,
333+
});
334+
335+
test('a finger drags a column sideways by its handle', async ({
336+
context,
337+
page,
338+
}) => {
339+
await openStory(page, 'lab-tabledraganddropcolumns--default');
340+
// The Task column scrolls away, so the drag stays clear of the edges.
341+
await scrollAreaOf(page).evaluate(area => {
342+
area.scrollLeft = 240;
343+
});
344+
const box = await handleOf(page, 'Owner').boundingBox();
345+
const owner = await page
346+
.locator(`${ROOT} thead th`, {hasText: 'Owner'})
347+
.boundingBox();
348+
const status = await page
349+
.locator(`${ROOT} thead th`, {hasText: 'Status'})
350+
.boundingBox();
351+
if (box == null || owner == null || status == null) {
352+
throw new Error('The handle and the headers must render.');
353+
}
354+
const cdp = await context.newCDPSession(page);
355+
const x = box.x + box.width / 2;
356+
const y = box.y + box.height / 2;
357+
const touch = (
358+
type: 'touchStart' | 'touchMove' | 'touchEnd',
359+
points: Array<{x: number; y: number}>,
360+
) =>
361+
cdp.send('Input.dispatchTouchEvent', {
362+
type,
363+
touchPoints: points.map(point => ({...point, id: 1})),
364+
});
365+
await touch('touchStart', [{x, y}]);
366+
// To the centre of Status, the place the drop takes.
367+
const distance = owner.width / 2 + status.width / 2;
368+
for (let step = 1; step <= 10; step++) {
369+
await touch('touchMove', [{x: x + (distance * step) / 10, y}]);
370+
}
371+
await expect(page.getByTestId('order')).toHaveText(
372+
'title owner status due points',
373+
);
374+
await touch('touchEnd', []);
375+
await expect(page.getByTestId('order')).toHaveText(
376+
'title status owner due points',
377+
);
378+
});
218379
});

0 commit comments

Comments
 (0)