From b9c7c0828a5b7a02406ff3ed3e6a415ae9f93bce Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Tue, 2 May 2023 22:46:37 +0200 Subject: [PATCH] Properly unload popups when not displayed --- src/components/Containers/Popup.tsx | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/components/Containers/Popup.tsx b/src/components/Containers/Popup.tsx index aa79144..ef475b3 100644 --- a/src/components/Containers/Popup.tsx +++ b/src/components/Containers/Popup.tsx @@ -1,4 +1,4 @@ -import { useEffect } from "react"; +import { useEffect, useState } from "react"; import { useHotkeys } from "react-hotkeys-hook"; import { cIf, cJoin } from "helpers/className"; import { atoms } from "contexts/atoms"; @@ -32,6 +32,8 @@ export const Popup = ({ withCloseButton = true, }: Props): JSX.Element => { const setMenuGesturesEnabled = useAtomSetter(atoms.layout.menuGesturesEnabled); + const [isHidden, setHidden] = useState(!isVisible); + const [isActuallyVisible, setActuallyVisible] = useState(isVisible && !isHidden); useHotkeys("escape", () => onCloseRequest?.(), { enabled: isVisible }, [onCloseRequest]); @@ -39,16 +41,32 @@ export const Popup = ({ setMenuGesturesEnabled(!isVisible); }, [isVisible, setMenuGesturesEnabled]); - return ( + // Used to unload the component if not visible + useEffect(() => { + const timeouts: NodeJS.Timeout[] = []; + if (isVisible) { + setHidden(false); + // We delay the visiblity of the element so that the opening animation is played + timeouts.push(setTimeout(() => setActuallyVisible(true), 100)); + } else { + setActuallyVisible(false); + timeouts.push(setTimeout(() => setHidden(true), 600)); + } + return () => timeouts.forEach(clearTimeout); + }, [isVisible]); + + return isHidden ? ( + <>> + ) : (