aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Toolbar/Toolbar.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-02-14 16:27:14 +0100
committerArmand Philippot <git@armandphilippot.com>2022-02-14 16:27:14 +0100
commit68d93a6310938f5dda378e9185cdfb0086f90de8 (patch)
tree892e161c33fa31f5d7e591aeed74411a7844733b /src/components/Toolbar/Toolbar.tsx
parent1b34f85f0e3188861c6804666f02b4495cab033c (diff)
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.
Diffstat (limited to 'src/components/Toolbar/Toolbar.tsx')
-rw-r--r--src/components/Toolbar/Toolbar.tsx28
1 files changed, 20 insertions, 8 deletions
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<boolean>(false);
const [isSearchOpened, setIsSearchOpened] = useState<boolean>(false);
const [isSettingsOpened, setIsSettingsOpened] = useState<boolean>(false);
+ const mainNavRef = useRef<HTMLDivElement>(null);
const searchBtnRef = useRef<HTMLButtonElement>(null);
const searchModalRef = useRef<HTMLDivElement>(null);
const settingsBtnRef = useRef<HTMLButtonElement>(null);
@@ -39,16 +40,14 @@ const Toolbar = () => {
ref: RefObject<HTMLDivElement>,
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<HTMLDivElement>, 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<HTMLDivElement> | null = null;
+ if (isNavOpened) ref = mainNavRef;
if (isSearchOpened) ref = searchModalRef;
if (isSettingsOpened) ref = settingsModalRef;
@@ -76,6 +76,14 @@ const Toolbar = () => {
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 (
<div className={styles.wrapper}>
- <MainNav isOpened={isNavOpened} setIsOpened={setIsNavOpened} />
+ <MainNav
+ ref={mainNavRef}
+ isOpened={isNavOpened}
+ setIsOpened={setIsNavOpened}
+ />
<ButtonToolbar
ref={searchBtnRef}
type="search"