diff --git a/src/components/Modal/BaseModal.tsx b/src/components/Modal/BaseModal.tsx index e8736aa2572a..c95e26e21823 100644 --- a/src/components/Modal/BaseModal.tsx +++ b/src/components/Modal/BaseModal.tsx @@ -109,6 +109,8 @@ function BaseModal( const insets = useSafeAreaInsets(); const isVisibleRef = useRef(isVisible); + const hideModalCallbackRef = useRef<(callHideCallback: boolean) => void>(undefined); + const wasVisible = usePrevious(isVisible); const uniqueModalId = useMemo(() => modalId ?? ComposerFocusManager.getId(), [modalId]); @@ -158,6 +160,22 @@ function BaseModal( }; }, [isVisible, wasVisible, onClose, type]); + useEffect(() => { + hideModalCallbackRef.current = hideModal; + }, [hideModal]); + + useEffect( + () => () => { + // Only trigger onClose and setModalVisibility if the modal is unmounting while visible. + if (!isVisibleRef.current) { + return; + } + hideModalCallbackRef.current?.(true); + }, + // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps + [], + ); + const handleShowModal = useCallback(() => { if (shouldSetModalVisibility) { setModalVisibility(true, type);