Skip to content

Commit d9a82d0

Browse files
committed
fix(BottomSheet, Tokenizer, PowerSearch): the scrim returns with a sheet whose swipe dismissal the owner refuses
1 parent dedecd0 commit d9a82d0

4 files changed

Lines changed: 381 additions & 10 deletions

File tree

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] BottomSheet, Tokenizer, PowerSearch: a swipe that asks to close a sheet whose owner keeps it open — the Tokenizer and PowerSearch sheets ask before a changed draft is discarded — no longer leaves the page behind the sheet undimmed. The scrim the drag thinned returns with the sheet to its resting detent, so the page stays dimmed while the confirmation is open and after Keep editing; Discard still closes the sheet with its exit.
6+
7+
@imdreamrunner
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
// @pr-a11y scope core/Tokenizer core/PowerSearch ^packages/core/src/internal/SheetConfirmation ^apps/storybook/stories/(Tokenizer|PowerSearch)
3+
4+
/**
5+
* @file BottomSheetRefusedDismissal.a11y.browser.spec.ts
6+
* @input The Tokenizer and PowerSearch sheet stories in a built Storybook, on
7+
* a phone viewport with touch; the swipe is real touch input through the
8+
* interaction harness.
9+
* @output Real-engine proof that a sheet whose dismissal its host refuses — a
10+
* swipe, a scrim tap or Escape on a changed draft opens the discard
11+
* confirmation — keeps its scrim while the confirmation is open, keeps it
12+
* after Keep editing, and leaves with it on Discard.
13+
* @position Chromium and WebKit. The scrim is the sheet dialog's `::backdrop`,
14+
* whose opacity the drag thins on the compositor and the host restores; only
15+
* a shipping engine computes the pseudo-element's style.
16+
*/
17+
18+
import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';
19+
import {
20+
DEFAULT_STORYBOOK_DIR,
21+
serveStorybook,
22+
type StaticServer,
23+
} from '@astryxdesign/a11y-spec/storybook';
24+
import {drag, touchDriver} from '@astryxdesign/interaction-harness';
25+
26+
let storybook: StaticServer;
27+
test.beforeAll(async () => {
28+
storybook = await serveStorybook(
29+
process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,
30+
);
31+
});
32+
test.afterAll(async () => {
33+
await storybook?.close();
34+
});
35+
36+
test.use({viewport: {width: 390, height: 844}, hasTouch: true});
37+
38+
interface SheetFixture {
39+
readonly name: string;
40+
readonly story: string;
41+
/** The sheet dialog's accessible name. */
42+
readonly sheetLabel: string;
43+
/** The control that reopens the sheet, and takes focus back after a close. */
44+
trigger(page: Page): ReturnType<Page['locator']>;
45+
/** One edit, so the draft differs from the selection at open. */
46+
dirty(page: Page): Promise<void>;
47+
/** Where Keep editing puts focus. */
48+
readonly focusAfterKeepEditing: string;
49+
}
50+
51+
const FIXTURES: ReadonlyArray<SheetFixture> = [
52+
{
53+
name: 'Tokenizer',
54+
story: 'core-tokenizer--sheet-presentation',
55+
sheetLabel: 'Tags',
56+
trigger: page => page.getByRole('button', {name: 'Tags'}),
57+
dirty: async page => {
58+
await page.getByRole('checkbox', {name: 'Tag 03'}).tap();
59+
await expect(
60+
page.getByRole('checkbox', {name: 'Tag 03'}),
61+
).toHaveAttribute('aria-checked', 'true');
62+
},
63+
focusAfterKeepEditing: 'Search Tags',
64+
},
65+
{
66+
name: 'PowerSearch',
67+
story: 'core-powersearch--sheet-presentation',
68+
sheetLabel: 'Filters',
69+
trigger: page => page.locator('button[aria-haspopup="dialog"]').first(),
70+
dirty: async page => {
71+
const remove = page
72+
.getByRole('button', {name: /^Remove .* filter$/})
73+
.first();
74+
const name = (await remove.getAttribute('aria-label')) ?? '';
75+
await remove.tap();
76+
await expect(page.getByRole('button', {name})).toHaveCount(0);
77+
},
78+
focusAfterKeepEditing: 'Add filter',
79+
},
80+
];
81+
82+
type ClosePath = 'swipe' | 'scrim' | 'escape';
83+
const CLOSE_PATHS: ReadonlyArray<ClosePath> = ['swipe', 'scrim', 'escape'];
84+
85+
interface ScrimReading {
86+
readonly open: boolean;
87+
readonly modal: boolean;
88+
/** The sheet dialog's `::backdrop` opacity, computed. */
89+
readonly opacity: number;
90+
/** The panel's transform at rest is the identity. */
91+
readonly panelAtRest: boolean;
92+
}
93+
94+
function sheetDialog(page: Page, fixture: SheetFixture) {
95+
return page.locator(`dialog[aria-label="${fixture.sheetLabel}"]`);
96+
}
97+
98+
async function readScrim(
99+
page: Page,
100+
fixture: SheetFixture,
101+
): Promise<ScrimReading> {
102+
return sheetDialog(page, fixture).evaluate(node => {
103+
const dialog = node as HTMLDialogElement;
104+
const panel = dialog.querySelector<HTMLElement>('[tabindex="-1"]');
105+
const transform =
106+
panel == null ? 'none' : getComputedStyle(panel).transform;
107+
return {
108+
open: dialog.open,
109+
modal: dialog.matches(':modal'),
110+
opacity: Number(getComputedStyle(dialog, '::backdrop').opacity),
111+
panelAtRest:
112+
transform === 'none' || transform === 'matrix(1, 0, 0, 1, 0, 0)',
113+
};
114+
});
115+
}
116+
117+
/** The sheet's own motion has ended: its entrance, or a return to rest. */
118+
async function waitForSheetRest(page: Page, fixture: SheetFixture) {
119+
const dialog = sheetDialog(page, fixture);
120+
await expect
121+
.poll(async () =>
122+
dialog.evaluate(
123+
node =>
124+
node
125+
.getAnimations({subtree: true})
126+
.filter(animation => animation.playState === 'running').length,
127+
),
128+
)
129+
.toBe(0);
130+
}
131+
132+
async function openDirtySheet(page: Page, fixture: SheetFixture) {
133+
await page.goto(
134+
`${storybook.origin}/iframe.html?id=${fixture.story}&viewMode=story`,
135+
{waitUntil: 'load'},
136+
);
137+
await fixture.trigger(page).waitFor({state: 'visible'});
138+
await page.evaluate(async () => {
139+
await document.fonts.ready;
140+
});
141+
await fixture.trigger(page).tap();
142+
await expect(
143+
page.getByRole('dialog', {name: fixture.sheetLabel}),
144+
).toBeVisible();
145+
await waitForSheetRest(page, fixture);
146+
await fixture.dirty(page);
147+
const before = await readScrim(page, fixture);
148+
expect(before).toEqual({
149+
open: true,
150+
modal: true,
151+
opacity: 1,
152+
panelAtRest: true,
153+
});
154+
}
155+
156+
/** Asks to close the sheet by `path`; the changed draft then asks first. */
157+
async function requestClose(
158+
page: Page,
159+
fixture: SheetFixture,
160+
path: ClosePath,
161+
) {
162+
const panel = sheetDialog(page, fixture).locator('[tabindex="-1"]').first();
163+
const box = await panel.boundingBox();
164+
if (box == null) {
165+
throw new Error('The sheet panel has no box');
166+
}
167+
const x = Math.round(box.x + box.width / 2);
168+
if (path === 'swipe') {
169+
// A finger on the grabber, pulled past the dismiss line and lifted: the
170+
// live drag thins the scrim with the sheet's travel.
171+
const driver = await touchDriver(page);
172+
const from = {x, y: Math.round(box.y + 12)};
173+
const to = {x, y: Math.round(Math.min(box.y + box.height, 844 - 8))};
174+
await driver.play(drag(from, to, {durationMs: 320}), {pacing: 'spacing'});
175+
} else if (path === 'scrim') {
176+
// The dialog above the panel is the scrim.
177+
await page.touchscreen.tap(x, Math.max(12, Math.round(box.y - 40)));
178+
} else {
179+
// A tooltip a focused control shows would take the press first (the
180+
// dismissal stack's order), so the press starts from the sheet's panel,
181+
// once that tooltip has gone.
182+
await panel.focus();
183+
await expect(page.getByRole('tooltip')).toHaveCount(0);
184+
await page.keyboard.press('Escape');
185+
}
186+
}
187+
188+
const confirmation = (page: Page) =>
189+
page.getByRole('alertdialog', {name: 'Discard changes?'});
190+
191+
for (const fixture of FIXTURES) {
192+
for (const path of CLOSE_PATHS) {
193+
test(`${fixture.name}: the scrim stays while the confirmation a ${path} opened is up, and after Keep editing`, async ({
194+
page,
195+
}) => {
196+
await openDirtySheet(page, fixture);
197+
await requestClose(page, fixture, path);
198+
await expect(confirmation(page)).toBeVisible();
199+
await expect(
200+
page.getByRole('button', {name: 'Keep editing'}),
201+
).toBeFocused();
202+
203+
// The sheet stayed presented and modal, with its backdrop painting.
204+
// After a swipe the panel returns to rest and the scrim with it, over
205+
// the sheet's own motion, so the reading waits for that rest.
206+
await waitForSheetRest(page, fixture);
207+
await expect
208+
.poll(async () => readScrim(page, fixture), {
209+
message: `the ${fixture.name} sheet's scrim while the confirmation is open`,
210+
})
211+
.toEqual({open: true, modal: true, opacity: 1, panelAtRest: true});
212+
213+
await page.getByRole('button', {name: 'Keep editing'}).tap();
214+
await expect(confirmation(page)).toHaveCount(0);
215+
await expect(
216+
page.getByRole('dialog', {name: fixture.sheetLabel}),
217+
).toBeVisible();
218+
await expect(
219+
page.getByRole(fixture.name === 'Tokenizer' ? 'textbox' : 'button', {
220+
name: fixture.focusAfterKeepEditing,
221+
}),
222+
).toBeFocused();
223+
// Keep editing changes nothing of the sheet's own presentation.
224+
expect(await readScrim(page, fixture)).toEqual({
225+
open: true,
226+
modal: true,
227+
opacity: 1,
228+
panelAtRest: true,
229+
});
230+
});
231+
}
232+
233+
test(`${fixture.name}: Discard after a swipe closes the sheet with its exit, and focus returns to the trigger`, async ({
234+
page,
235+
}) => {
236+
await openDirtySheet(page, fixture);
237+
await requestClose(page, fixture, 'swipe');
238+
await expect(confirmation(page)).toBeVisible();
239+
await waitForSheetRest(page, fixture);
240+
await page.getByRole('button', {name: 'Discard changes'}).tap();
241+
await expect(confirmation(page)).toHaveCount(0);
242+
// The exit runs: the dialog is still presented for a moment, then gone,
243+
// and the backdrop has faded with it.
244+
await expect(
245+
page.locator(`dialog[aria-label="${fixture.sheetLabel}"][open]`),
246+
).toHaveCount(0, {timeout: 5000});
247+
await expect(fixture.trigger(page)).toBeFocused();
248+
});
249+
}

‎packages/core/src/BottomSheet/useSheetGestures.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
1313
import {act, renderHook} from '@testing-library/react';
14+
import {useState} from 'react';
1415
import {rubberBand} from '../motion/rubberBand';
1516
import {
1617
isInsideNestedLayer,
@@ -814,6 +815,88 @@ describe('useSheetGestures', () => {
814815
expect(onDismiss).toHaveBeenCalledTimes(1);
815816
});
816817

818+
describe('a dismissal the host refuses', () => {
819+
// The host answers a dismissal through `isOpenRequested`: it stays true
820+
// when a guarded `onOpenChange` keeps the sheet open (a confirmation
821+
// asks before a changed draft is discarded). The panel then rests on its
822+
// detent again, and the scrim the drag thinned toward the dismiss line
823+
// must rest with it.
824+
it('restores the full scrim of a sheet resting fully open', () => {
825+
const onScrimOpacity = vi.fn();
826+
const {hook, onDismiss} = setup({
827+
snapHeights: () => [200],
828+
onScrimOpacity,
829+
});
830+
const t = makeTarget();
831+
down(hook, 0, 0, t);
832+
move(hook, 160, 400, t);
833+
move(hook, 330, 900, t);
834+
// The live drag has thinned the scrim past the shortest detent.
835+
expect(onScrimOpacity.mock.lastCall?.[0]).toBeLessThan(1);
836+
// The release asks to close; `isOpen` stays true, so the host refused.
837+
up(hook, 330, 1400, t);
838+
expect(onDismiss).toHaveBeenCalledTimes(1);
839+
expect(hook.result.current.settledOffset).toBe(0);
840+
expect(hook.result.current.isDismissedByTouchDrag).toBe(false);
841+
expect(onScrimOpacity).toHaveBeenLastCalledWith(1);
842+
});
843+
844+
it('restores the thinned scrim of a sheet resting at its peek', () => {
845+
const onScrimOpacity = vi.fn();
846+
// Sheet 400, stops at 300 and a 60px sliver -> offsets [0, 100, 340];
847+
// the sliver is a peek, which rests under the peek floor (0.3).
848+
const {hook, onDismiss} = setup({
849+
snapHeights: () => [60, 300],
850+
onScrimOpacity,
851+
});
852+
const t = makeTarget();
853+
down(hook, 0, 0, t);
854+
move(hook, 170, 400, t);
855+
move(hook, 340, 900, t);
856+
up(hook, 340, 1400, t);
857+
expect(hook.result.current.settledOffset).toBe(340);
858+
// From the peek, a slow pull past the dismiss line asks to close.
859+
down(hook, 340, 2000, t);
860+
move(hook, 380, 2400, t);
861+
move(hook, 420, 2900, t);
862+
up(hook, 420, 3400, t);
863+
expect(onDismiss).toHaveBeenCalledTimes(1);
864+
expect(hook.result.current.settledOffset).toBe(340);
865+
expect(Number(onScrimOpacity.mock.lastCall?.[0])).toBeCloseTo(0.3);
866+
});
867+
868+
it('leaves the thinned scrim alone when the host takes the dismissal', () => {
869+
const onScrimOpacity = vi.fn();
870+
const onDismiss = vi.fn();
871+
// A host that takes the close: its controlled state answers in the
872+
// same update the release asked in.
873+
const hook = renderHook(() => {
874+
const [isOpenRequested, setIsOpenRequested] = useState(true);
875+
return useSheetGestures({
876+
isOpen: true,
877+
isOpenRequested,
878+
onDismiss: () => {
879+
onDismiss();
880+
setIsOpenRequested(false);
881+
},
882+
snapHeights: () => [200],
883+
onScrimOpacity,
884+
});
885+
});
886+
const t = makeTarget();
887+
down(hook, 0, 0, t);
888+
move(hook, 160, 400, t);
889+
move(hook, 330, 900, t);
890+
const thinned = onScrimOpacity.mock.lastCall?.[0];
891+
expect(thinned).toBeLessThan(1);
892+
up(hook, 330, 1400, t);
893+
expect(onDismiss).toHaveBeenCalledTimes(1);
894+
// The host's exit owns the scrim from here, so the hook writes nothing
895+
// more.
896+
expect(onScrimOpacity.mock.lastCall?.[0]).toBe(thinned);
897+
});
898+
});
899+
817900
describe('body overscroll', () => {
818901
// A scrollable body element nested inside the sheet, with a settable
819902
// scrollTop so we can simulate "at the top" vs "scrolled".

0 commit comments

Comments
 (0)