Skip to content

Commit 75e368f

Browse files
committed
fix(Item): an open swipe row closes on a tap on its content or a scroll
1 parent e71f692 commit 75e368f

6 files changed

Lines changed: 300 additions & 13 deletions

File tree

‎.changeset/item-swipe-dismiss.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] An open swipe-action row on `Item` and `ListItem` closes on a tap on its own content, as a UIKit list row does, and that tap no longer activates the row. It also closes when the list or the page scrolls, including a scroll that starts on the row itself, and it counts as open from the release that opens it, before its spring comes to rest.
6+
7+
@vjeux

‎packages/core/src/Item/Item.doc.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export const docs = {
211211
name: 'swipeActions',
212212
type: '{leading?: ItemSwipeAction[]; trailing?: ItemSwipeAction[]}',
213213
description:
214-
"Swipe actions for touch: the verbs a sideways drag uncovers on each side, outermost last, each `{id?, label, icon?, onActivate, isDisabled?, variant?: 'neutral' | 'accent' | 'destructive', hasRemoval?}`. `leading` is uncovered by a drag toward the inline end, `trailing` toward the inline start. A mouse never starts the drag and a mostly vertical drag stays the scroller's. The gesture is a touch accelerator and never a row's only path to a verb: a verb reachable only by swipe is unreachable by keyboard and by mouse, so every entry must also be reachable through the row's own content. Served on a row whose role permits interactive descendants (a listitem, a row with no role) and not on an option, a menu item, or a row that is the enlarged target of a native radio. The element containing the rows clips in the inline axis with overflow-inline: clip — List does; any other host does it once. `hasRemoval` only says the row holds out after the action fires instead of springing back. The gesture and its panels are loaded on demand the first time a row has swipe actions, so a row without them ships none of it.",
214+
"Swipe actions for touch: the verbs a sideways drag uncovers on each side, outermost last, each `{id?, label, icon?, onActivate, isDisabled?, variant?: 'neutral' | 'accent' | 'destructive', hasRemoval?}`. `leading` is uncovered by a drag toward the inline end, `trailing` toward the inline start. A mouse never starts the drag and a mostly vertical drag stays the scroller's. An open row closes on a tap on its own content, which activates nothing, on a touch elsewhere, when the list or page scrolls, and on Escape. The gesture is a touch accelerator and never a row's only path to a verb: a verb reachable only by swipe is unreachable by keyboard and by mouse, so every entry must also be reachable through the row's own content. Served on a row whose role permits interactive descendants (a listitem, a row with no role) and not on an option, a menu item, or a row that is the enlarged target of a native radio. The element containing the rows clips in the inline axis with overflow-inline: clip — List does; any other host does it once. `hasRemoval` only says the row holds out after the action fires instead of springing back. The gesture and its panels are loaded on demand the first time a row has swipe actions, so a row without them ships none of it.",
215215
},
216216
{
217217
name: 'swipeBehavior',

‎packages/core/src/Item/Item.test.tsx‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,62 @@ describe('swipeActions', () => {
13911391
);
13921392
});
13931393

1394+
it('closes a resting row on a tap on its own content, and the tap activates nothing', async () => {
1395+
const {row, onClick} = renderRow();
1396+
dragLeading(row, 45);
1397+
await settled();
1398+
expect(xOf(row)).toBe(72);
1399+
const f = finger(screen.getByRole('button', {name: 'Conversation'}), 2);
1400+
f.down(150);
1401+
// Taken at the lift: its touchend is cancelled, so the browser makes no
1402+
// click on the row.
1403+
expect(f.up().defaultPrevented).toBe(true);
1404+
await settled();
1405+
expect(xOf(row)).toBe(0);
1406+
expect(onClick).not.toHaveBeenCalled();
1407+
});
1408+
1409+
it('counts a row open from the release that opens it: a tap while it settles open closes it', async () => {
1410+
const {row, onClick} = renderRow();
1411+
dragLeading(row, 45);
1412+
// The spring is still on its way to rest open.
1413+
const f = finger(screen.getByRole('button', {name: 'Conversation'}), 2);
1414+
f.down(150);
1415+
expect(f.up().defaultPrevented).toBe(true);
1416+
await settled();
1417+
expect(xOf(row)).toBe(0);
1418+
expect(onClick).not.toHaveBeenCalled();
1419+
});
1420+
1421+
it('leaves a tap on a resting entry to the entry, and a tap on a closed row to the row', async () => {
1422+
const {row} = renderRow();
1423+
let f = finger(screen.getByRole('button', {name: 'Conversation'}), 2);
1424+
f.down(150);
1425+
expect(f.up().defaultPrevented).toBe(false);
1426+
dragLeading(row, 45);
1427+
await settled();
1428+
f = finger(screen.getByRole('button', {name: 'Unread'}), 3);
1429+
f.down(30);
1430+
expect(f.up().defaultPrevented).toBe(false);
1431+
expect(xOf(row)).toBe(72);
1432+
});
1433+
1434+
it('closes a resting row when anything holding it scrolls, and not for another scroller', async () => {
1435+
const {row} = renderRow();
1436+
dragLeading(row, 45);
1437+
await settled();
1438+
const elsewhere = document.createElement('div');
1439+
document.body.append(elsewhere);
1440+
fireEvent.scroll(elsewhere);
1441+
await settled();
1442+
expect(xOf(row)).toBe(72);
1443+
// The row's parent stands in for the list: it scrolls, the row closes.
1444+
fireEvent.scroll(row.parentElement as HTMLElement);
1445+
await settled();
1446+
expect(xOf(row)).toBe(0);
1447+
elsewhere.remove();
1448+
});
1449+
13941450
it('fires the outermost entry and no other on a drag past the commit point, then springs back', async () => {
13951451
const inner = vi.fn();
13961452
const outer = vi.fn();

‎packages/core/src/Item/Item.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ export interface ItemProps extends BaseProps<HTMLElement> {
333333
* a touch accelerator and never a row's only path to a verb: a verb
334334
* reachable only by swipe is unreachable by keyboard and by mouse, so every
335335
* entry must also be reachable through the row's own content (a menu or a
336-
* button in `endContent`).
336+
* button in `endContent`). An open row closes on a tap on its own content,
337+
* which activates nothing, on a touch elsewhere, when the list or page
338+
* scrolls, and on Escape.
337339
*
338340
* Available on a row whose role permits interactive descendants — a
339341
* `listitem`, a row with no role — and not served on one whose role forbids

‎packages/core/src/Item/__tests__/ItemSwipeActions.interaction.browser.spec.ts‎

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
* at every sample; a flick hands its presentation velocity to the settle;
1313
* a finger that lands on a settling row catches it without a jump; the
1414
* settle keeps advancing through a blocked main thread; the drag and the
15-
* settle do no Layout or Paint per frame; a storyboard of the drag and the
16-
* settle.
15+
* settle do no Layout or Paint per frame; a tap on a resting row's own
16+
* content closes it and clicks nothing, and a scroll that starts on it
17+
* closes it; a storyboard of the drag and the settle.
1718
* @position Interaction proofs of Item's swipeActions (the shape of
1819
* BottomSheetDetent.interaction.browser.spec.ts, the harness exemplar). The
1920
* row moves by the individual `translate` property (the compositor
@@ -42,6 +43,7 @@ import {
4243
seekAnimations,
4344
springStep,
4445
startScreencast,
46+
tap,
4547
thawTimers,
4648
timeline,
4749
touchDriver,
@@ -522,6 +524,117 @@ test('the settle keeps advancing every frame while the main thread is blocked',
522524
expect(proof.monotonic).toBe(true);
523525
});
524526

527+
/** A held drag on `open`'s row past half its panel: it rests open. Resolves with the open width. */
528+
async function restOpen({driver, row, entries, start}: OpenRow) {
529+
const openWidth = await entries.evaluate(
530+
node => (node as HTMLElement).offsetWidth,
531+
);
532+
await driver.play(heldDrag(start, -130));
533+
await expect.poll(async () => xOf(row)).toBeCloseTo(-openWidth, 0);
534+
return openWidth;
535+
}
536+
537+
/**
538+
* A tap through the engine's own tap path where clicks are concerned: WebKit's
539+
* Linux build synthesises a click only from its tap gesture, not from injected
540+
* touch events (the gesture system's browser tests note the same).
541+
*/
542+
async function engineTap(
543+
page: Page,
544+
browserName: string,
545+
driver: TouchDriver,
546+
at: {x: number; y: number},
547+
) {
548+
if (browserName === 'webkit') {
549+
await page.touchscreen.tap(Math.round(at.x), Math.round(at.y));
550+
} else {
551+
await driver.play(tap(at));
552+
}
553+
}
554+
555+
/** Counts the clicks that reach `row` (capture, before the row's own handlers). */
556+
async function countClicks(row: Locator) {
557+
await row.evaluate(node => {
558+
const counter = {clicks: 0};
559+
Object.assign(window, {__rowClicks: counter});
560+
node.addEventListener(
561+
'click',
562+
() => {
563+
counter.clicks += 1;
564+
},
565+
true,
566+
);
567+
});
568+
return async () =>
569+
row
570+
.page()
571+
.evaluate(
572+
() =>
573+
(window as unknown as {__rowClicks: {clicks: number}}).__rowClicks
574+
.clicks,
575+
);
576+
}
577+
578+
test('a tap on the resting row’s own content closes it, and no click reaches the row', async ({
579+
page,
580+
browserName,
581+
}) => {
582+
const open = await openRow(page);
583+
const {driver, row, rest} = open;
584+
await restOpen(open);
585+
const clicks = await countClicks(row);
586+
// On the row's own content, clear of the open panel at its trailing end.
587+
const content = {x: rest.x + 60, y: rest.y + rest.height / 2};
588+
await engineTap(page, browserName, driver, content);
589+
await expect.poll(async () => xOf(row)).toBeCloseTo(0, 0);
590+
await page.waitForTimeout(400);
591+
expect(await clicks()).toBe(0);
592+
// The same tap on the closed row is the row's own.
593+
await engineTap(page, browserName, driver, content);
594+
await expect.poll(clicks).toBe(1);
595+
});
596+
597+
test('a scroll that starts on the resting row closes it', async ({
598+
page,
599+
browserName,
600+
}) => {
601+
test.skip(
602+
browserName === 'webkit',
603+
'WebKit’s Linux build cannot scroll from injected touches; Mobile Safari on the simulator covers it',
604+
);
605+
await openStory(page, storybook(), 'core-item--swipe-actions');
606+
const driver = await touchDriver(page);
607+
// A row low on the page, and room below the list to scroll into.
608+
await page.evaluate(() => {
609+
document.body.style.minHeight = '3000px';
610+
});
611+
const row = page.locator('.astryx-item').nth(4);
612+
await expect(row.locator('[data-swipe-panel="trailing"]')).toBeAttached();
613+
const entries = row.locator('[data-swipe-panel="trailing"] > div > div');
614+
const rest = await row.boundingBox();
615+
if (rest == null) {
616+
throw new Error('no row');
617+
}
618+
await restOpen({
619+
driver,
620+
row,
621+
entries,
622+
panel: row.locator('[data-swipe-panel="trailing"]'),
623+
track: row.locator('[data-swipe-panel="trailing"] > div'),
624+
rest,
625+
start: {x: rest.x + rest.width - 24, y: rest.y + rest.height / 2},
626+
});
627+
// Up from the row's own content: the page scrolls, and the row closes.
628+
const from = {x: rest.x + 60, y: rest.y + rest.height / 2};
629+
await driver.play(
630+
drag(from, {x: from.x, y: from.y - 200}, {durationMs: 300, hz: 60}),
631+
);
632+
await expect
633+
.poll(async () => page.evaluate(() => scrollY))
634+
.toBeGreaterThan(50);
635+
await expect.poll(async () => xOf(row)).toBeCloseTo(0, 0);
636+
});
637+
525638
test('the drag and the settle do no Layout or Paint per frame', async ({
526639
page,
527640
browserName,
@@ -642,6 +755,46 @@ test(
642755
},
643756
);
644757

758+
test(
759+
'storyboard: a tap on the open row’s content closes it',
760+
{tag: '@storyboard'},
761+
async ({page, browserName}, testInfo) => {
762+
const open = await openRow(page);
763+
const {driver, row, rest} = open;
764+
await restOpen(open);
765+
await installTimerGate(page);
766+
const clip = {
767+
x: 0,
768+
y: Math.max(0, rest.y - 8),
769+
width: page.viewportSize()?.width ?? 402,
770+
height: rest.height + 16,
771+
};
772+
await engineTap(page, browserName, driver, {
773+
x: rest.x + 60,
774+
y: rest.y + rest.height / 2,
775+
});
776+
await waitForAnimation(row);
777+
await freezeTimers(page);
778+
const {storyboard} = await animationStoryboard(page, {
779+
name: 'item-swipe-tap-closes',
780+
clip,
781+
scope: row,
782+
measure: async () => ({x: await xOf(row)}),
783+
times: timeline((await motionOf(row, 0)).ms + 50, 1000 / 30),
784+
columns: 6,
785+
cellScale: 0.6,
786+
});
787+
await releaseAnimations(page, {finish: true});
788+
await thawTimers(page);
789+
await storyboard.write(testInfo);
790+
const xs = storyboard.frames.map(f => f.data.x as number);
791+
// From open to closed without a step back: the settle spring, from rest.
792+
expect(xs[0]).toBeLessThan(-100);
793+
expect(xs.every((x, i) => i === 0 || x >= xs[i - 1] - 0.5)).toBe(true);
794+
expect(xs.at(-1)).toBeCloseTo(0, 0);
795+
},
796+
);
797+
645798
test(
646799
'storyboard: a flick opens the row and a finger catches it mid-settle',
647800
{tag: '@storyboard'},

0 commit comments

Comments
 (0)