diff --git a/apps/android/src/ui/sheet.tsx b/apps/android/src/ui/sheet.tsx index 6d1c591..c6fc3cf 100644 --- a/apps/android/src/ui/sheet.tsx +++ b/apps/android/src/ui/sheet.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState, type ReactNode } from 'react' +import { useEffect, useId, useRef, useState, type ReactNode } from 'react' import { Animated, Easing, @@ -28,6 +28,23 @@ import { radius, spacing, type ThemeColors } from './theme' import { useReduceMotion } from './use-reduce-motion' import { useThemedStyles } from './use-themed-styles' +// When a user jumps from one sheet straight to another (e.g. `+` menu -> picker), +// immediately finish any closing sheet's exit so two transparent Modal windows +// never stack on Android or steal autoFocus when the old Dialog unmounts. +const sheetOpenListeners = new Set<(openerId: string) => void>() +let routeDismissEpoch = 0 +function notifySheetOpening(openerId: string) { + for (const listener of sheetOpenListeners) { + listener(openerId) + } +} + +/** Finish any in-flight or synchronous sheet exit immediately before switching to a new route. */ +export function dismissClosingSheets() { + routeDismissEpoch += 1 + notifySheetOpening('__route__') +} + /** * Bottom sheet container with an interruptible enter/exit transition. * @@ -58,7 +75,7 @@ export function SheetModal({ children: ReactNode backdropStyle?: StyleProp sheetStyle?: StyleProp - /** Wrap in `KeyboardAvoidingView` for sheets with text inputs (rename dialogs). */ + /** Keep input sheets and their actions above the soft keyboard. */ avoidKeyboard?: boolean dismissLabel?: string }) { @@ -67,18 +84,52 @@ export function SheetModal({ const { sheetMaxHeight } = useResponsiveLayout() const { height: windowHeight } = useWindowDimensions() const reduceMotion = useReduceMotion() + const sheetId = useId() const [phase, setPhase] = useState(() => visible ? 'opening' : 'unmounted') + const [availableHeight, setAvailableHeight] = useState() const progress = useRef(new Animated.Value(0)).current const animationRef = useRef(null) + const visibleRef = useRef(visible) + visibleRef.current = visible + const openedAtRouteEpochRef = useRef(routeDismissEpoch) + const supersededByOtherRef = useRef(false) // Keep the closing sheet populated after the caller clears its target. const retainedChildren = useRef(children) if (visible) retainedChildren.current = children useEffect(() => { - setPhase(current => nextSheetPhase(current, visible)) - }, [visible]) + const onOtherSheetOpen = (openerId: string) => { + if (openerId === sheetId || visibleRef.current) return + supersededByOtherRef.current = true + setPhase(current => { + if (current === 'unmounted') return current + animationRef.current?.stop() + return nextSheetPhase(current, false, true) + }) + } + sheetOpenListeners.add(onOtherSheetOpen) + return () => { sheetOpenListeners.delete(onOtherSheetOpen) } + }, [sheetId]) useEffect(() => { + if (visible) { + openedAtRouteEpochRef.current = routeDismissEpoch + supersededByOtherRef.current = false + notifySheetOpening(sheetId) + setPhase(current => nextSheetPhase(current, true)) + return + } + const superseded = supersededByOtherRef.current || routeDismissEpoch !== openedAtRouteEpochRef.current + if (superseded) animationRef.current?.stop() + setPhase(current => nextSheetPhase(current, false, superseded)) + }, [visible, sheetId]) + + useEffect(() => { + if (phase === 'unmounted') { + animationRef.current?.stop() + progress.setValue(0) + return + } if (phase !== 'opening' && phase !== 'closing') return animationRef.current?.stop() const animation = Animated.timing(progress, { @@ -105,7 +156,11 @@ export function SheetModal({ const content = visible ? children : retainedChildren.current const body = ( - + setAvailableHeight(event.nativeEvent.layout.height)} + > {body}