Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(BottomSheet): a sheet whose close is suspended takes the scrim (#10776)

Christopher Chedeau7f45363bd498
diff --git a/.changeset/bottom-sheet-suspended-close-scrim.md b/.changeset/bottom-sheet-suspended-close-scrim.md
new file mode 100644
index 0000000000..9bd2c3dc75
--- /dev/null
+++ b/.changeset/bottom-sheet-suspended-close-scrim.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A stacked `BottomSheet` whose close waits in a suspended transition takes over the dim when the sheet beneath it closes, so the page behind it stays dimmed.
+@vjeux
diff --git a/packages/core/src/BottomSheet/BottomSheet.tsx b/packages/core/src/BottomSheet/BottomSheet.tsx
index 5f3b7121e2..500c28da2a 100644
--- a/packages/core/src/BottomSheet/BottomSheet.tsx
+++ b/packages/core/src/BottomSheet/BottomSheet.tsx
@@ -54,6 +54,7 @@ import {
use,
useCallback,
useEffect,
+ useInsertionEffect,
useLayoutEffect,
useRef,
useState,
@@ -503,7 +504,13 @@ function StandaloneBottomSheet({
BOTTOM_SHEET_STACK_DEFAULT_VIEW,
);
const isOpenRef = useRef(isOpen);
- isOpenRef.current = isOpen;
+ // Written at commit: a render React discards, as a suspended transition
+ // does, must not change the scrim. The insertion effect runs before any
+ // layout effect, so a nested sheet's registry call in the same commit sees
+ // the new state too.
+ useInsertionEffect(() => {
+ isOpenRef.current = isOpen;
+ }, [isOpen]);
// Closed and being replaced by the sheet that opened in the same update:
// the panel is retained (stays, aligns, fades) instead of sliding out.
const isReplaced = !isOpen && isPresented && stackView.replacedMotion != null;
diff --git a/packages/core/src/BottomSheet/BottomSheetStacking.test.tsx b/packages/core/src/BottomSheet/BottomSheetStacking.test.tsx
index 73841c57d4..e0a2fc7676 100644
--- a/packages/core/src/BottomSheet/BottomSheetStacking.test.tsx
+++ b/packages/core/src/BottomSheet/BottomSheetStacking.test.tsx
@@ -2164,6 +2164,68 @@ describe('R10 concurrent React', () => {
expect(HTMLDialogElement.prototype.showModal).toHaveBeenCalledTimes(1);
});
+ it('a sheet whose close waits in a suspended transition takes the scrim when the sheet beneath it closes', () => {
+ const never = new Promise<never>(() => {});
+ function Forever(): ReactNode {
+ // eslint-disable-next-line @typescript-eslint/only-throw-error -- Suspense protocol: a thrown promise suspends the subtree
+ throw never;
+ }
+ // A keeps its own state, so an update to it renders only A.
+ let closeBeneath!: () => void;
+ function Beneath() {
+ const [isOpen, setIsOpen] = useState(true);
+ closeBeneath = () => setIsOpen(false);
+ return (
+ <BottomSheet isOpen={isOpen} onOpenChange={setIsOpen} label="A">
+ a
+ </BottomSheet>
+ );
+ }
+ let closeTopInTransition!: () => void;
+ function App() {
+ const [isTopOpen, setIsTopOpen] = useState(true);
+ const [isWaiting, setIsWaiting] = useState(false);
+ closeTopInTransition = () =>
+ startTransition(() => {
+ setIsTopOpen(false);
+ setIsWaiting(true);
+ });
+ return (
+ <>
+ <Beneath />
+ <Suspense fallback={null}>
+ <BottomSheet
+ isOpen={isTopOpen}
+ onOpenChange={setIsTopOpen}
+ label="B">
+ b
+ </BottomSheet>
+ {isWaiting ? <Forever /> : null}
+ </Suspense>
+ </>
+ );
+ }
+ render(<App />);
+ const a = getDialog('A');
+ const b = getDialog('B');
+ expect(scrim(a)).toBe('1');
+ expect(scrim(b)).toBe('0');
+ // B's close renders, then suspends for good: B stays open on screen.
+ act(() => {
+ closeTopInTransition();
+ });
+ expect(openDialogs()).toEqual(['A', 'B']);
+ // A closes from code beneath the open B. The dim is B's from here on.
+ act(() => {
+ closeBeneath();
+ });
+ expect(scrim(a)).toBe('0');
+ expect(scrim(b)).toBe('1');
+ finishExit(a);
+ expect(stackSummary()).toEqual(['B:top:0']);
+ expect(scrim(b)).toBe('1');
+ });
+
it('Suspense hide takes a sheet out of the stack; reveal re-presents it on top', async () => {
let resolveGate!: () => void;
const gate = new Promise<void>(resolve => {