diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-18 14:27:11 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-18 14:27:11 +0200 |
| commit | b214baab3e17d92f784b4f782863deafc5558ee4 (patch) | |
| tree | cdc20c7e77ba6926285917eead8bb088bdc843f8 /src/components/organisms/toolbar/toolbar.tsx | |
| parent | 54883bb5c36cf21462a421605a709fdd6f04b150 (diff) | |
chore: close toolbar modals on click/focus outside
Diffstat (limited to 'src/components/organisms/toolbar/toolbar.tsx')
| -rw-r--r-- | src/components/organisms/toolbar/toolbar.tsx | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/components/organisms/toolbar/toolbar.tsx b/src/components/organisms/toolbar/toolbar.tsx index 6593055..e4188fe 100644 --- a/src/components/organisms/toolbar/toolbar.tsx +++ b/src/components/organisms/toolbar/toolbar.tsx @@ -1,4 +1,5 @@ -import { FC, useState } from 'react'; +import useClickOutside from '@utils/hooks/use-click-outside'; +import { FC, useRef, useState } from 'react'; import MainNav, { type MainNavProps } from '../toolbar/main-nav'; import Search, { type SearchProps } from '../toolbar/search'; import Settings from '../toolbar/settings'; @@ -22,8 +23,18 @@ export type ToolbarProps = Pick<SearchProps, 'searchPage'> & { */ const Toolbar: FC<ToolbarProps> = ({ className = '', nav, searchPage }) => { const [isNavOpened, setIsNavOpened] = useState<boolean>(false); - const [isSettingsOpened, setIsSettingsOpened] = useState<boolean>(false); const [isSearchOpened, setIsSearchOpened] = useState<boolean>(false); + const [isSettingsOpened, setIsSettingsOpened] = useState<boolean>(false); + const mainNavRef = useRef<HTMLDivElement>(null); + const searchRef = useRef<HTMLDivElement>(null); + const settingsRef = useRef<HTMLDivElement>(null); + + useClickOutside(mainNavRef, () => isNavOpened && setIsNavOpened(false)); + useClickOutside(searchRef, () => isSearchOpened && setIsSearchOpened(false)); + useClickOutside( + settingsRef, + () => isSettingsOpened && setIsSettingsOpened(false) + ); return ( <div className={`${styles.wrapper} ${className}`}> @@ -32,18 +43,21 @@ const Toolbar: FC<ToolbarProps> = ({ className = '', nav, searchPage }) => { isActive={isNavOpened} setIsActive={setIsNavOpened} className={styles.modal} + ref={mainNavRef} /> <Search searchPage={searchPage} isActive={isSearchOpened} setIsActive={setIsSearchOpened} className={`${styles.modal} ${styles['modal--search']}`} + ref={searchRef} /> <Settings isActive={isSettingsOpened} setIsActive={setIsSettingsOpened} className={`${styles.modal} ${styles['modal--settings']}`} tooltipClassName={styles.tooltip} + ref={settingsRef} /> </div> ); |
