From 61d7e6014b13bbb53046d84867cc37daf944198f Mon Sep 17 00:00:00 2001 From: Hailey Date: Fri, 26 Apr 2024 23:42:49 -0700 Subject: [PATCH] ensure dialog is closed on unmount (#3650) Revert "properly check if the ref is null" This reverts commit 8f563808a5d39389b0bc47a31e73cd147d1e7e8b. properly check if the ref is null ensure dialog is closed on unmount --- src/components/Dialog/index.tsx | 6 +++ src/view/screens/Storybook/Dialogs.tsx | 75 ++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/components/Dialog/index.tsx b/src/components/Dialog/index.tsx index 859e4965ce..e5a6792db6 100644 --- a/src/components/Dialog/index.tsx +++ b/src/components/Dialog/index.tsx @@ -152,6 +152,12 @@ export function Outer({ [open, close], ) + React.useEffect(() => { + return () => { + setDialogIsOpen(control.id, false) + } + }, [control.id, setDialogIsOpen]) + const context = React.useMemo(() => ({close}), [close]) return ( diff --git a/src/view/screens/Storybook/Dialogs.tsx b/src/view/screens/Storybook/Dialogs.tsx index f68f9f4ddf..6d166d4b64 100644 --- a/src/view/screens/Storybook/Dialogs.tsx +++ b/src/view/screens/Storybook/Dialogs.tsx @@ -14,6 +14,43 @@ export function Dialogs() { const prompt = Prompt.usePromptControl() const testDialog = Dialog.useDialogControl() const {closeAllDialogs} = useDialogStateControlContext() + const unmountTestDialog = Dialog.useDialogControl() + const [shouldRenderUnmountTest, setShouldRenderUnmountTest] = + React.useState(false) + const unmountTestInterval = React.useRef() + + const onUnmountTestStartPressWithClose = () => { + setShouldRenderUnmountTest(true) + + setTimeout(() => { + unmountTestDialog.open() + }, 1000) + + setTimeout(() => { + unmountTestDialog.close() + }, 4950) + + setInterval(() => { + setShouldRenderUnmountTest(prev => !prev) + }, 5000) + } + + const onUnmountTestStartPressWithoutClose = () => { + setShouldRenderUnmountTest(true) + + setTimeout(() => { + unmountTestDialog.open() + }, 1000) + + setInterval(() => { + setShouldRenderUnmountTest(prev => !prev) + }, 5000) + } + + const onUnmountTestEndPress = () => { + setShouldRenderUnmountTest(false) + clearInterval(unmountTestInterval.current) + } return ( @@ -70,6 +107,33 @@ export function Dialogs() { Open Tester + + + + + + This is a prompt @@ -257,6 +321,17 @@ export function Dialogs() { + + {shouldRenderUnmountTest && ( + + + + +

Unmount Test Dialog

+

Will unmount in about 5 seconds

+
+
+ )} ) }