diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2022-10-11 15:29:58 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2022-10-11 15:29:58 +0200 |
| commit | 6e8909f48774317f377040004394d21e708e4756 (patch) | |
| tree | 84457b3c62723d04c648fccca611b2da1cd29c4f /gui/src/renderer/components | |
| parent | 71a9376bebc0a043ca8a4526b052984961948b21 (diff) | |
| download | mullvadvpn-6e8909f48774317f377040004394d21e708e4756.tar.xz mullvadvpn-6e8909f48774317f377040004394d21e708e4756.zip | |
Move prev isOpen into shared state to fix issue with setter running asynchronously
Diffstat (limited to 'gui/src/renderer/components')
| -rw-r--r-- | gui/src/renderer/components/Modal.tsx | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/gui/src/renderer/components/Modal.tsx b/gui/src/renderer/components/Modal.tsx index 97631caec4..8e0d099b5b 100644 --- a/gui/src/renderer/components/Modal.tsx +++ b/gui/src/renderer/components/Modal.tsx @@ -162,31 +162,34 @@ interface IModalAlertProps { close?: () => void; } +interface OpenState { + isClosing: boolean; + wasOpen: boolean; +} + export function ModalAlert(props: IModalAlertProps & { isOpen: boolean }) { const { isOpen, ...otherProps } = props; const activeModalContext = useContext(ActiveModalContext); - const [closing, setClosing] = useState(false); - const prevIsOpen = useRef(isOpen); + const [openState, setOpenState] = useState<OpenState>({ isClosing: false, wasOpen: isOpen }); const willExit = useWillExit(); // Modal shouldn't prepare for being opened again while view is disappearing. const onTransitionEnd = useCallback(() => { if (!willExit) { - setClosing(false); + setOpenState({ isClosing: false, wasOpen: isOpen }); } - }, [willExit]); + }, [willExit, isOpen]); useEffect(() => { - setClosing((closing) => closing || (prevIsOpen.current && !isOpen)); - - // Unmounting the Modal during view transitions result in a visual glitch. - if (!willExit) { - prevIsOpen.current = isOpen; - } + setOpenState(({ isClosing, wasOpen }) => ({ + isClosing: isClosing || (wasOpen && !isOpen), + // Unmounting the Modal during view transitions result in a visual glitch. + wasOpen: willExit ? wasOpen : isOpen, + })); }, [isOpen]); - if (!prevIsOpen.current && !isOpen && !closing) { + if (!openState.wasOpen && !isOpen && !openState.isClosing) { return null; } @@ -194,7 +197,7 @@ export function ModalAlert(props: IModalAlertProps & { isOpen: boolean }) { <ModalAlertImpl {...activeModalContext} {...otherProps} - closing={closing} + closing={openState.isClosing} onTransitionEnd={onTransitionEnd} /> ); |
