An open source design system that's fully customizable and agent ready
TypeScript
fix(BottomSheet): a sheet Suspense hid while closing finishes closing when shown again (#9620)
Cindy Zhangfb2dd8019408
diff --git a/.changeset/bottomsheet-suspense-close.md b/.changeset/bottomsheet-suspense-close.mdnew file mode 100644index 0000000000..91748403ed--- /dev/null+++ b/.changeset/bottomsheet-suspense-close.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[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.++@cixzhangdiff --git a/packages/core/src/BottomSheet/BottomSheetPanel.tsx b/packages/core/src/BottomSheet/BottomSheetPanel.tsxindex 1c58747324..82f4097b7e 100644--- a/packages/core/src/BottomSheet/BottomSheetPanel.tsx+++ b/packages/core/src/BottomSheet/BottomSheetPanel.tsx@@ -675,10 +675,23 @@ export function BottomSheetPanel({if (motion == null) {return;}- startedMotionRef.current = null;+ // React can run this effect again for the same motion, for example when+ // a Suspense boundary shows this content again. The passive effect below+ // does not run again then, so the motion it started stays started, and a+ // completion completes at once instead of waiting for that effect.+ if (startedMotionRef.current !== motion) {+ startedMotionRef.current = null;+ }pendingMotionCompleteRef.current = null;+ const complete = () => {+ if (startedMotionRef.current === motion) {+ onMotionCompleteRef.current?.(motion);+ } else {+ pendingMotionCompleteRef.current = motion;+ }+ };if (motion === 'entering' && reactivatedEntranceRef.current) {- pendingMotionCompleteRef.current = motion;+ complete();return;}// What ends a motion: the fade is opacity, and the entrance, alignment and@@ -690,13 +703,7 @@ export function BottomSheetPanel({return waitForTransition(elementRef.current,motion === 'fading' || crossFadeMotion ? 'opacity' : 'transform',- () => {- if (startedMotionRef.current === motion) {- onMotionCompleteRef.current?.(motion);- } else {- pendingMotionCompleteRef.current = motion;- }- },+ complete,);}, [motion, isCrossFade]);// The snap is transform-only; the layout height reconciles when it lands.diff --git a/packages/core/src/BottomSheet/BottomSheetSuspense.test.tsx b/packages/core/src/BottomSheet/BottomSheetSuspense.test.tsxnew file mode 100644index 0000000000..fb798636c1--- /dev/null+++ b/packages/core/src/BottomSheet/BottomSheetSuspense.test.tsx@@ -0,0 +1,148 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file BottomSheetSuspense.test.tsx+ * @input Uses vitest, @testing-library/react, BottomSheet and a Suspense+ * boundary that hides the sheet while it closes+ * @output Tests that a sheet a Suspense boundary hides during its exit, and+ * then shows again, finishes closing: its dialog closes, it is no longer+ * inert or hidden, and the page under it is usable+ * @position Core testing; validates the motion completion in+ * BottomSheetPanel.tsx when React re-runs its layout effects+ */++import {act, fireEvent, render, screen} from '@testing-library/react';+import {Suspense, use, useState} from 'react';+import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';+import {BottomSheet} from './BottomSheet';+import {resetBottomSheetStackForTests} from './bottomSheetStack';+import {Button} from '../Button/Button';+import {__resetInteractionModalityForTest} from '../utils/interactionModality';++beforeEach(() => {+ resetBottomSheetStackForTests();+ __resetInteractionModalityForTest();+ HTMLDialogElement.prototype.showModal = vi.fn(function (+ this: HTMLDialogElement,+ ) {+ this.setAttribute('open', '');+ });+ HTMLDialogElement.prototype.show = vi.fn(function (this: HTMLDialogElement) {+ this.setAttribute('open', '');+ });+ HTMLDialogElement.prototype.close = vi.fn(function (this: HTMLDialogElement) {+ this.removeAttribute('open');+ });+ if (!Element.prototype.setPointerCapture) {+ Element.prototype.setPointerCapture = vi.fn();+ Element.prototype.releasePointerCapture = vi.fn();+ }+ window.scrollTo = vi.fn();+ vi.stubGlobal(+ 'matchMedia',+ vi.fn().mockReturnValue({+ matches: false,+ media: '',+ onchange: null,+ addEventListener: vi.fn(),+ removeEventListener: vi.fn(),+ addListener: vi.fn(),+ removeListener: vi.fn(),+ dispatchEvent: vi.fn(),+ }),+ );+});++afterEach(() => {+ vi.unstubAllGlobals();+});++function Page({wait}: {wait: Promise<void> | null}) {+ if (wait != null) {+ use(wait);+ }+ return <p>Page</p>;+}++function Shell({wait}: {wait: Promise<void> | null}) {+ const [isOpen, setIsOpen] = useState(false);+ return (+ <Suspense fallback={<p>Loading</p>}>+ <Page wait={wait} />+ <Button label="Open" onClick={() => setIsOpen(true)} />+ <BottomSheet isOpen={isOpen} onOpenChange={setIsOpen} label="Sheet">+ <Button label="Close" onClick={() => setIsOpen(false)} />+ </BottomSheet>+ </Suspense>+ );+}++function sheetState() {+ const dialog = document.querySelector<HTMLDialogElement>(+ 'dialog[aria-label="Sheet"]',+ );+ if (dialog == null) {+ throw new Error('sheet dialog not found');+ }+ return {+ open: dialog.hasAttribute('open'),+ inert: dialog.hasAttribute('inert'),+ ariaHidden: dialog.getAttribute('aria-hidden'),+ };+}++/** Gives the exit a real transition, so it ends on a timer, not at once. */+function slowExit() {+ const panel = document.querySelector<HTMLElement>(+ 'dialog[aria-label="Sheet"] .astryx-bottom-sheet',+ );+ if (panel == null) {+ throw new Error('sheet panel not found');+ }+ panel.style.transitionProperty = 'transform';+ panel.style.transitionDuration = '200ms';+ panel.style.transitionDelay = '0s';+}++const CLOSED = {open: false, inert: false, ariaHidden: null};++async function waitPastExit() {+ await act(async () => {+ await new Promise(resolve => setTimeout(resolve, 600));+ });+}++describe('BottomSheet hidden by Suspense while it closes', () => {+ it('closes normally without the hide', async () => {+ render(<Shell wait={null} />);+ fireEvent.click(screen.getByRole('button', {name: 'Open'}));+ slowExit();+ fireEvent.click(screen.getByRole('button', {name: 'Close'}));+ await waitPastExit();+ expect(sheetState()).toEqual(CLOSED);+ });++ it('finishes closing when it is shown again', async () => {+ const {rerender} = render(<Shell wait={null} />);+ fireEvent.click(screen.getByRole('button', {name: 'Open'}));+ slowExit();+ fireEvent.click(screen.getByRole('button', {name: 'Close'}));++ let resolve: () => void = () => {};+ const pending = new Promise<void>(done => {+ resolve = done;+ });+ rerender(<Shell wait={pending} />);+ expect(screen.getByText('Loading')).toBeInTheDocument();++ await act(async () => {+ resolve();+ await pending;+ });+ rerender(<Shell wait={null} />);+ expect(screen.getByText('Page')).toBeInTheDocument();++ await waitPastExit();+ expect(sheetState()).toEqual(CLOSED);+ });+});