From fbc9f0c38af89ca34a7cc10aa6eaf0a906362897 Mon Sep 17 00:00:00 2001 From: Ali Unwala Date: Thu, 10 Sep 2020 17:34:36 -0500 Subject: [PATCH] fix: Adding a cleanup function to add a removeEventListener to prevent hangs while pressing ESC --- .../src/components/GlobalSearch/GlobalSearch.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-theme-carbon/src/components/GlobalSearch/GlobalSearch.js b/packages/gatsby-theme-carbon/src/components/GlobalSearch/GlobalSearch.js index 68af4c8e2..77ac61f48 100644 --- a/packages/gatsby-theme-carbon/src/components/GlobalSearch/GlobalSearch.js +++ b/packages/gatsby-theme-carbon/src/components/GlobalSearch/GlobalSearch.js @@ -79,12 +79,19 @@ const GlobalSearchInput = () => { ); useEffect(() => { - document.addEventListener('keyup', function(e){ + var collapseOpenNavs = function(e){ if(e.which == 27){ toggleNavState('switcherIsOpen', 'close'); toggleNavState('searchIsOpen', 'close'); } - }) + }; + + document.addEventListener('keyup',collapseOpenNavs) + + return function cleanup(){ + document.removeEventListener('keyup', collapseOpenNavs) + }; + }) useEffect(() => {