Skip to content

Commit fb2dd80

Browse files
authored
fix(BottomSheet): a sheet Suspense hid while closing finishes closing when shown again (#9620)
fix(BottomSheet): a sheet Suspense hid while closing finishes closing when shown again Fixes #9542. BottomSheetPanel ends a motion through a layout effect that waits for the transition and a passive effect that marks the motion started and flushes a completion that arrived first. When a Suspense boundary hides and shows content, React runs the layout effect again but not the passive one. The layout effect cleared startedMotionRef on every run, so the completion was parked and never flushed: the dialog stayed open, modal and inert, and the page under it was unusable. The layout effect now keeps the started mark when it runs again for the same motion, so the completion completes at once.
1 parent 723e045 commit fb2dd80

3 files changed

Lines changed: 171 additions & 9 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: a sheet that a Suspense boundary hid while it was closing, and then showed again, never finished closing. Its dialog stayed open, modal and inert, so the whole page under it ignored taps, clicks and focus until a reload. This happens, for example, when a sheet's action navigates to a route that suspends. Now the sheet finishes closing when it is shown again, and the page is usable.
6+
7+
@cixzhang

‎packages/core/src/BottomSheet/BottomSheetPanel.tsx‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,23 @@ export function BottomSheetPanel({
675675
if (motion == null) {
676676
return;
677677
}
678-
startedMotionRef.current = null;
678+
// React can run this effect again for the same motion, for example when
679+
// a Suspense boundary shows this content again. The passive effect below
680+
// does not run again then, so the motion it started stays started, and a
681+
// completion completes at once instead of waiting for that effect.
682+
if (startedMotionRef.current !== motion) {
683+
startedMotionRef.current = null;
684+
}
679685
pendingMotionCompleteRef.current = null;
686+
const complete = () => {
687+
if (startedMotionRef.current === motion) {
688+
onMotionCompleteRef.current?.(motion);
689+
} else {
690+
pendingMotionCompleteRef.current = motion;
691+
}
692+
};
680693
if (motion === 'entering' && reactivatedEntranceRef.current) {
681-
pendingMotionCompleteRef.current = motion;
694+
complete();
682695
return;
683696
}
684697
// What ends a motion: the fade is opacity, and the entrance, alignment and
@@ -690,13 +703,7 @@ export function BottomSheetPanel({
690703
return waitForTransition(
691704
elementRef.current,
692705
motion === 'fading' || crossFadeMotion ? 'opacity' : 'transform',
693-
() => {
694-
if (startedMotionRef.current === motion) {
695-
onMotionCompleteRef.current?.(motion);
696-
} else {
697-
pendingMotionCompleteRef.current = motion;
698-
}
699-
},
706+
complete,
700707
);
701708
}, [motion, isCrossFade]);
702709
// The snap is transform-only; the layout height reconciles when it lands.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file BottomSheetSuspense.test.tsx
5+
* @input Uses vitest, @testing-library/react, BottomSheet and a Suspense
6+
* boundary that hides the sheet while it closes
7+
* @output Tests that a sheet a Suspense boundary hides during its exit, and
8+
* then shows again, finishes closing: its dialog closes, it is no longer
9+
* inert or hidden, and the page under it is usable
10+
* @position Core testing; validates the motion completion in
11+
* BottomSheetPanel.tsx when React re-runs its layout effects
12+
*/
13+
14+
import {act, fireEvent, render, screen} from '@testing-library/react';
15+
import {Suspense, use, useState} from 'react';
16+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
17+
import {BottomSheet} from './BottomSheet';
18+
import {resetBottomSheetStackForTests} from './bottomSheetStack';
19+
import {Button} from '../Button/Button';
20+
import {__resetInteractionModalityForTest} from '../utils/interactionModality';
21+
22+
beforeEach(() => {
23+
resetBottomSheetStackForTests();
24+
__resetInteractionModalityForTest();
25+
HTMLDialogElement.prototype.showModal = vi.fn(function (
26+
this: HTMLDialogElement,
27+
) {
28+
this.setAttribute('open', '');
29+
});
30+
HTMLDialogElement.prototype.show = vi.fn(function (this: HTMLDialogElement) {
31+
this.setAttribute('open', '');
32+
});
33+
HTMLDialogElement.prototype.close = vi.fn(function (this: HTMLDialogElement) {
34+
this.removeAttribute('open');
35+
});
36+
if (!Element.prototype.setPointerCapture) {
37+
Element.prototype.setPointerCapture = vi.fn();
38+
Element.prototype.releasePointerCapture = vi.fn();
39+
}
40+
window.scrollTo = vi.fn();
41+
vi.stubGlobal(
42+
'matchMedia',
43+
vi.fn().mockReturnValue({
44+
matches: false,
45+
media: '',
46+
onchange: null,
47+
addEventListener: vi.fn(),
48+
removeEventListener: vi.fn(),
49+
addListener: vi.fn(),
50+
removeListener: vi.fn(),
51+
dispatchEvent: vi.fn(),
52+
}),
53+
);
54+
});
55+
56+
afterEach(() => {
57+
vi.unstubAllGlobals();
58+
});
59+
60+
function Page({wait}: {wait: Promise<void> | null}) {
61+
if (wait != null) {
62+
use(wait);
63+
}
64+
return <p>Page</p>;
65+
}
66+
67+
function Shell({wait}: {wait: Promise<void> | null}) {
68+
const [isOpen, setIsOpen] = useState(false);
69+
return (
70+
<Suspense fallback={<p>Loading</p>}>
71+
<Page wait={wait} />
72+
<Button label="Open" onClick={() => setIsOpen(true)} />
73+
<BottomSheet isOpen={isOpen} onOpenChange={setIsOpen} label="Sheet">
74+
<Button label="Close" onClick={() => setIsOpen(false)} />
75+
</BottomSheet>
76+
</Suspense>
77+
);
78+
}
79+
80+
function sheetState() {
81+
const dialog = document.querySelector<HTMLDialogElement>(
82+
'dialog[aria-label="Sheet"]',
83+
);
84+
if (dialog == null) {
85+
throw new Error('sheet dialog not found');
86+
}
87+
return {
88+
open: dialog.hasAttribute('open'),
89+
inert: dialog.hasAttribute('inert'),
90+
ariaHidden: dialog.getAttribute('aria-hidden'),
91+
};
92+
}
93+
94+
/** Gives the exit a real transition, so it ends on a timer, not at once. */
95+
function slowExit() {
96+
const panel = document.querySelector<HTMLElement>(
97+
'dialog[aria-label="Sheet"] .astryx-bottom-sheet',
98+
);
99+
if (panel == null) {
100+
throw new Error('sheet panel not found');
101+
}
102+
panel.style.transitionProperty = 'transform';
103+
panel.style.transitionDuration = '200ms';
104+
panel.style.transitionDelay = '0s';
105+
}
106+
107+
const CLOSED = {open: false, inert: false, ariaHidden: null};
108+
109+
async function waitPastExit() {
110+
await act(async () => {
111+
await new Promise(resolve => setTimeout(resolve, 600));
112+
});
113+
}
114+
115+
describe('BottomSheet hidden by Suspense while it closes', () => {
116+
it('closes normally without the hide', async () => {
117+
render(<Shell wait={null} />);
118+
fireEvent.click(screen.getByRole('button', {name: 'Open'}));
119+
slowExit();
120+
fireEvent.click(screen.getByRole('button', {name: 'Close'}));
121+
await waitPastExit();
122+
expect(sheetState()).toEqual(CLOSED);
123+
});
124+
125+
it('finishes closing when it is shown again', async () => {
126+
const {rerender} = render(<Shell wait={null} />);
127+
fireEvent.click(screen.getByRole('button', {name: 'Open'}));
128+
slowExit();
129+
fireEvent.click(screen.getByRole('button', {name: 'Close'}));
130+
131+
let resolve: () => void = () => {};
132+
const pending = new Promise<void>(done => {
133+
resolve = done;
134+
});
135+
rerender(<Shell wait={pending} />);
136+
expect(screen.getByText('Loading')).toBeInTheDocument();
137+
138+
await act(async () => {
139+
resolve();
140+
await pending;
141+
});
142+
rerender(<Shell wait={null} />);
143+
expect(screen.getByText('Page')).toBeInTheDocument();
144+
145+
await waitPastExit();
146+
expect(sheetState()).toEqual(CLOSED);
147+
});
148+
});

0 commit comments

Comments
 (0)