diff --git a/docs/floating-panels.md b/docs/floating-panels.md index a1d557e290..8fc3839e75 100644 --- a/docs/floating-panels.md +++ b/docs/floating-panels.md @@ -433,10 +433,17 @@ wide layouts keep local geometry. That export is what `patches/@gorhom+bottom-sheet+*.patch` adds. A sheet whose owner may veto the close (unsaved work) passes `enableDismissOnClose={false}`. Every -close path then parks the sheet at index -1 with its content mounted; the owner hears -`onChange(-1)`, runs its guard, and either snaps back to 0 or drops its state, which unmounts the -sheet and takes it off the stack. Calling `dismiss()` on such a sheet bypasses the guard and -unmounts it, so its backdrop and Back press call `close()`. +close path then parks the sheet at index -1 with its content mounted; the owner runs its guard and +either snaps back to 0 or drops its state, which unmounts the sheet and takes it off the stack. +Calling `dismiss()` on such a sheet bypasses the guard and unmounts it, so its backdrop and Back +press call `close()`. + +Run that guard from the wrapper's `onClosed`. Gorhom only calls `onChange` when a close ends at a +different index than the one it already recorded, and an interrupted mount animation leaves that +record at -1, so the close that follows is invisible: the sheet parks with its backdrop still over +the app, and every tap in the covered area is lost. `onClosed` watches the close animation settle at +the closed detent and reports it either way. Keeping `onChange` as well is harmless as long as the +guard is idempotent. The wrapper must give Gorhom an object ref. Gorhom accepts a callback ref at the React boundary but stores it in the modal queue and later reads `.current`; provider dismissal, replacement, diff --git a/packages/app/e2e/browser/compact-workspace-sheet.spec.ts b/packages/app/e2e/browser/compact-workspace-sheet.spec.ts index 80c38a378e..047bbd55a1 100644 --- a/packages/app/e2e/browser/compact-workspace-sheet.spec.ts +++ b/packages/app/e2e/browser/compact-workspace-sheet.spec.ts @@ -282,3 +282,34 @@ test("compact opens files and subagents in a sheet, leaving the tabs for agents" await workspace.cleanup(); } }); + +test("a sheet dismissed while it is still opening closes its tab", async ({ page }) => { + await page.setViewportSize({ width: 390, height: 844 }); + const workspace = await seedMockAgentWorkspace({ + repoPrefix: "compact-sheet-opening-dismiss-", + title: "Sheet parent", + initialPrompt: promptMentioning(FILE_LINK), + }); + try { + await writeFile(path.join(workspace.cwd, "linked.ts"), "one\ntwo\nthree\n", "utf8"); + await openAgentRoute(page, { workspaceId: workspace.workspaceId, agentId: workspace.agentId }); + const fileLink = page.getByText(FILE_LINK, { exact: true }); + await expect(fileLink).toBeVisible({ timeout: 15_000 }); + + // The backdrop is mounted from the first frame, so this closes the sheet inside its mount + // animation. A close that ends at the index the sheet's bookkeeping already held used to be + // dropped, leaving the closed sheet on the stack with its backdrop still covering the app. + await fileLink.click(); + await tapSheetBackdrop(page); + + await expectNoSheetOpen(page); + await expectOpenTabCount(page, 1); + await page + .getByRole("button", { name: /Switch tabs/ }) + .filter({ visible: true }) + .click(); + await expect(page.getByText("Workspace tabs", { exact: true })).toBeVisible(); + } finally { + await workspace.cleanup(); + } +}); diff --git a/packages/app/src/components/ui/isolated-bottom-sheet-modal/index.tsx b/packages/app/src/components/ui/isolated-bottom-sheet-modal/index.tsx index 9b7c375cb4..9a06d443f9 100644 --- a/packages/app/src/components/ui/isolated-bottom-sheet-modal/index.tsx +++ b/packages/app/src/components/ui/isolated-bottom-sheet-modal/index.tsx @@ -2,6 +2,7 @@ import { BottomSheetModal as GorhomBottomSheetModal, type BottomSheetModalProps, type BottomSheetBackdropProps, + useBottomSheetInternal, } from "@gorhom/bottom-sheet"; import React from "react"; import { @@ -25,6 +26,7 @@ import { type AttachSheetContentDrag, type SheetContentDragGesture, } from "@/components/ui/bottom-sheet-scope"; +import { runOnJS, useAnimatedReaction, useSharedValue } from "react-native-reanimated"; import { SheetBackdrop } from "./sheet-backdrop"; type GorhomBottomSheetModalMethods = ElementRef; @@ -56,7 +58,7 @@ type IsolatedBottomSheetModalProps = Omit< /** * Whether closing (drag, backdrop, Back) also dismisses, which unmounts the content. Default * true: the owner hears `onDismiss` and drops its state. Pass `false` for a sheet whose close the - * owner may veto: it parks at index -1 with its content mounted, the owner hears `onChange(-1)`, + * owner may veto: it parks at index -1 with its content mounted, the owner hears `onClosed`, * and either snaps it back or unmounts it, which dismisses it from the stack. */ enableDismissOnClose?: boolean; @@ -74,6 +76,15 @@ type IsolatedBottomSheetModalProps = Omit< * inside the sheet and it throws on device only. */ contextBridge: ContextBridge | null; + /** + * Runs when a close animation settles at the closed detent, whoever started it. + * + * Gorhom reports that through `onChange(-1)`, but skips `onChange` when its index bookkeeping + * already reads -1. An interrupted mount animation leaves it there, so the next close ends where + * it began and the owner never hears about it. Watch this signal, not `onChange`, when a close + * the owner may veto still has to reach the owner. + */ + onClosed?: () => void; }; export type IsolatedBottomSheetModalRef = GorhomBottomSheetModalMethods; @@ -105,6 +116,7 @@ export const IsolatedBottomSheetModal = forwardRef< contextBridge, backdropOpacity, enableDismissOnClose = true, + onClosed, waitFor, simultaneousHandlers, ...bottomSheetProps @@ -151,6 +163,7 @@ export const IsolatedBottomSheetModal = forwardRef< simultaneousHandlers={contentDragHandlers.simultaneousHandlers} > + {onClosed ? : null} {contextBridge ? contextBridge(children) : children} @@ -159,6 +172,43 @@ export const IsolatedBottomSheetModal = forwardRef< return modal; }); +/** + * Reports a close animation that settles at the closed detent — the signal Gorhom only routes + * through `onChange(-1)`, which it drops when the sheet's current index already reads -1. + * + * `animatedAnimationState.nextIndex` is -1 for the whole close animation and undefined once it + * completes. The settled position then tells a real close from a spring back and an interruption. + */ +function SheetCloseSignal({ onClosed }: { onClosed: () => void }) { + const { animatedAnimationState, animatedDetentsState, animatedPosition } = + useBottomSheetInternal(); + const closing = useSharedValue(false); + useAnimatedReaction( + () => { + const { nextIndex } = animatedAnimationState.get(); + if (nextIndex === -1) return -1; + return nextIndex === undefined ? 0 : 1; + }, + (phase, previousPhase) => { + if (phase === -1) { + closing.value = true; + return; + } + if (phase === 1) { + closing.value = false; + return; + } + if (previousPhase !== -1 || !closing.value) return; + closing.value = false; + const closedPosition = animatedDetentsState.get().closedDetentPosition; + if (closedPosition === undefined) return; + if (Math.abs(animatedPosition.value - closedPosition) < 1) runOnJS(onClosed)(); + }, + [animatedAnimationState, animatedDetentsState, animatedPosition, onClosed], + ); + return null; +} + export function useIsolatedBottomSheetVisibility({ visible, isEnabled, diff --git a/packages/app/src/screens/workspace/sheet-tabs.tsx b/packages/app/src/screens/workspace/sheet-tabs.tsx index 30bebaac66..9f9597f5db 100644 --- a/packages/app/src/screens/workspace/sheet-tabs.tsx +++ b/packages/app/src/screens/workspace/sheet-tabs.tsx @@ -95,20 +95,36 @@ function SheetTabSheet({ sheet, isTop, onClose }: SheetTabSheetProps) { }; }, []); - // Every close path (drag, backdrop, Close, Back) slides the sheet down first; `onChange(-1)` then - // runs the one guarded close. The sheet keeps its content through the close, so a declined + // Every close path (drag, backdrop, Close, Back) slides the sheet down first; the owner then runs + // the one guarded close. The sheet keeps its content through the close, so a declined // unsaved-changes prompt only has to bring it back up. - const close = useCallback(() => sheetRef.current?.close(), []); + // + // Both signals feed that one attempt. `onChange(-1)` covers the ordinary case; `onClosed` covers + // a close whose end index Gorhom's bookkeeping already held, which it therefore never reports as + // a change (an interrupted mount animation leaves it at -1). + const closeRequested = useRef(false); + const requestClose = useCallback(() => { + if (unmounted.current || closeRequested.current) return; + closeRequested.current = true; + void onClose(tab.tabId).then((stillOpen) => { + if (stillOpen) { + closeRequested.current = false; + sheetRef.current?.snapToIndex(0); + } + return stillOpen; + }); + }, [onClose, tab.tabId]); const handleChange = useCallback( (index: number) => { - if (index !== -1 || unmounted.current) return; - void onClose(tab.tabId).then((stillOpen) => { - if (stillOpen) sheetRef.current?.snapToIndex(0); - return stillOpen; - }); + if (index !== -1) { + closeRequested.current = false; + return; + } + requestClose(); }, - [onClose, tab.tabId], + [requestClose], ); + const close = useCallback(() => sheetRef.current?.close(), []); // Hidden workspaces stay mounted in the deck and under Settings; only the visible one owns Back. const ownsBackPress = isTop && isWorkspaceFocused && isScreenFocused; useEffect(() => { @@ -133,6 +149,7 @@ function SheetTabSheet({ sheet, isTop, onClose }: SheetTabSheetProps) { handleComponent={SheetHandle} backdropOpacity={0.45} onChange={handleChange} + onClosed={requestClose} accessible={false} >