From a9a932ac5577efa202c429b235d73db926d734d1 Mon Sep 17 00:00:00 2001 From: tjtanjin Date: Sun, 11 Feb 2024 19:18:50 +0800 Subject: [PATCH] refactor: Minor code cleanup --- src/components/ChatBotContainer.tsx | 41 +++++++++++++++++++---------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/components/ChatBotContainer.tsx b/src/components/ChatBotContainer.tsx index 3c65be2c..d619bd9e 100644 --- a/src/components/ChatBotContainer.tsx +++ b/src/components/ChatBotContainer.tsx @@ -175,27 +175,26 @@ const ChatBotContainer = ({ flow }: { flow: Flow }) => { setViewportWidth(window.visualViewport?.width as number); } + const cleanupScrollEventListeners = () => { + window.removeEventListener("scroll", handleMobileScrollOpened); + window.removeEventListener("scroll", handleMobileScrollClosed); + window.visualViewport?.removeEventListener("resize", handleResize); + }; + if (botOptions.isOpen) { - window.removeEventListener('scroll', handleMobileScrollClosed); + cleanupScrollEventListeners(); document.body.style.position = "fixed"; window.addEventListener("scroll", handleMobileScrollOpened); window.visualViewport?.addEventListener("resize", handleResize); - - return () => { - window.removeEventListener("scroll", handleMobileScrollOpened); - window.visualViewport?.removeEventListener("resize", handleResize); - }; } else { document.body.style.position = "static"; - window.removeEventListener("scroll", handleMobileScrollOpened); + cleanupScrollEventListeners(); window.scrollTo({top: scrollPositionRef.current, left: 0, behavior: "auto"}); window.addEventListener('scroll', handleMobileScrollClosed); window.visualViewport?.removeEventListener("resize", handleResize); - - return () => { - window.removeEventListener("scroll", handleMobileScrollClosed); - }; } + + return cleanupScrollEventListeners; }, [botOptions.isOpen]); // performs pre-processing when paths change @@ -487,14 +486,28 @@ const ChatBotContainer = ({ flow }: { flow: Flow }) => { {/* styles and prevents background from scrolling on mobile when chat window is open */} {botOptions.isOpen && !isDesktop && !botOptions.theme?.embedded && - + +
+
+ }