import { useEffect, useId, useRef, useState, type ReactNode } from 'react' import { Animated, Easing, KeyboardAvoidingView, Modal, Platform, Pressable, StyleSheet, useWindowDimensions, View, type StyleProp, type ViewStyle, } from 'react-native' import { useSafeAreaInsets } from 'react-native-safe-area-context' import { strings } from '../locales/i18n' import { anchorRise, easingCurve, motionTokens, nextSheetPhase, sheetPhaseDuration, type Rect, type SheetPhase, } from './motion' import { useResponsiveLayout } from './responsive' 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. * * The sheet itself stays fully opaque while it travels (only the backdrop * fades), because a translucent panel shows the page underneath through it. * Closing is animated too: `visible` switching to false keeps the modal mounted * until the exit finishes, rendering the last children it was given. * * Geometry matches the previous `ModalSurface` exactly so no screen moves at * rest; callers with their own sheet styles override them afterwards. */ export function SheetModal({ visible, onClose, onRequestClose, anchor, children, backdropStyle, sheetStyle, avoidKeyboard = false, dismissLabel, }: { visible: boolean onClose: () => void /** Hardware back / modal request; defaults to `onClose`. */ onRequestClose?: () => void anchor?: Rect children: ReactNode backdropStyle?: StyleProp sheetStyle?: StyleProp /** Keep input sheets and their actions above the soft keyboard. */ avoidKeyboard?: boolean dismissLabel?: string }) { const styles = useThemedStyles(createStyles) const insets = useSafeAreaInsets() 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(() => { 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, { toValue: phase === 'opening' ? 1 : 0, duration: sheetPhaseDuration(phase, reduceMotion), easing: Easing.bezier(...easingCurve(phase === 'opening' ? 'expand' : 'retreat')), useNativeDriver: true, }) animationRef.current = animation animation.start(({ finished }) => { if (!finished) return setPhase(current => current === 'opening' ? 'open' : current === 'closing' ? 'unmounted' : current) }) return () => animation.stop() }, [phase, progress, reduceMotion]) useEffect(() => () => animationRef.current?.stop(), []) if (phase === 'unmounted') return null const rise = anchorRise(anchor, windowHeight) const translateY = progress.interpolate({ inputRange: [0, 1], outputRange: [rise, 0] }) const requestClose = onRequestClose ?? onClose const content = visible ? children : retainedChildren.current const body = ( setAvailableHeight(event.nativeEvent.layout.height)} > {content} ) return ( {avoidKeyboard ? ( {body} ) : body} ) } const createStyles = (colors: ThemeColors) => StyleSheet.create({ container: { flex: 1, justifyContent: 'flex-end' }, backdrop: { flex: 1, backgroundColor: colors.modalBackdrop, justifyContent: 'flex-end' }, sheet: { zIndex: 1, elevation: 1, backgroundColor: colors.background, borderTopLeftRadius: radius.xxl, borderTopRightRadius: radius.xxl, padding: spacing.lg, paddingBottom: spacing.xxl, }, })