From 68d93a6310938f5dda378e9185cdfb0086f90de8 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 14 Feb 2022 16:27:14 +0100 Subject: fix: open toolbar menu without double click When the main nav was opened, a double click on the search button or the settings button was necessary to open a new menu. Now, it works as expected: the main nav is closed and the menu is opened in one click. --- src/components/Toolbar/Toolbar.tsx | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src/components/Toolbar/Toolbar.tsx') diff --git a/src/components/Toolbar/Toolbar.tsx b/src/components/Toolbar/Toolbar.tsx index 25418b1..7b28757 100644 --- a/src/components/Toolbar/Toolbar.tsx +++ b/src/components/Toolbar/Toolbar.tsx @@ -9,6 +9,7 @@ const Toolbar = () => { const [isNavOpened, setIsNavOpened] = useState(false); const [isSearchOpened, setIsSearchOpened] = useState(false); const [isSettingsOpened, setIsSettingsOpened] = useState(false); + const mainNavRef = useRef(null); const searchBtnRef = useRef(null); const searchModalRef = useRef(null); const settingsBtnRef = useRef(null); @@ -39,16 +40,14 @@ const Toolbar = () => { ref: RefObject, target: EventTarget ) => { - const currentRef = ref.current; - return currentRef && !currentRef.contains(target as Node); + return ref.current && !ref.current.contains(target as Node); }; const isToggleBtn = (ref: RefObject, target: EventTarget) => { - const currentRef = ref.current; return ( - currentRef && - currentRef.previousElementSibling && - currentRef.previousElementSibling.contains(target as Node) + ref.current && + ref.current.previousElementSibling && + ref.current.previousElementSibling.contains(target as Node) ); }; @@ -68,6 +67,7 @@ const Toolbar = () => { const handleVisibility = useCallback( (e: MouseEvent | FocusEvent) => { let ref: RefObject | null = null; + if (isNavOpened) ref = mainNavRef; if (isSearchOpened) ref = searchModalRef; if (isSettingsOpened) ref = settingsModalRef; @@ -75,6 +75,14 @@ const Toolbar = () => { if (!isClickOutside(ref, e.target as Node)) return; if (isToggleBtn(ref, e.target as Node)) return; + if ( + ref.current.id === 'main-nav' && + !isSettingsBtn(e.target as HTMLElement) && + !isSearchBtn(e.target as HTMLElement) + ) { + setIsNavOpened(false); + } + if ( ref.current.id === 'search-modal' && !isSettingsBtn(e.target as HTMLElement) @@ -86,7 +94,7 @@ const Toolbar = () => { ) setIsSettingsOpened(false); }, - [isSearchOpened, isSettingsOpened, isSearchBtn, isSettingsBtn] + [isNavOpened, isSearchOpened, isSettingsOpened, isSearchBtn, isSettingsBtn] ); useEffect(() => { @@ -109,7 +117,11 @@ const Toolbar = () => { return (
- +