From 828927d167856f6a682b5f483fcde74dc34a21d6 Mon Sep 17 00:00:00 2001 From: ajaythapliyal Date: Mon, 27 Feb 2023 22:41:47 +0530 Subject: [PATCH] fixes 8 of nick's list --- frontend/src/App.tsx | 7 ++----- frontend/src/Navigation.tsx | 23 +++++++++++++++++++++-- frontend/src/hooks.ts | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 frontend/src/hooks.ts diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 29b1b60c..9a216223 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -8,15 +8,12 @@ import { ActiveState } from './models/misc'; export default function App() { //TODO : below media query is disjoint from tailwind. Please wire it together. const [navState, setNavState] = useState( - window.matchMedia('((min-width: 768px)').matches ? 'ACTIVE' : 'INACTIVE', + window.matchMedia('(min-width: 768px)').matches ? 'ACTIVE' : 'INACTIVE', ); return (
- setNavState(val)} - /> +
void; + setNavState: React.Dispatch>; }) { const dispatch = useDispatch(); const docs = useSelector(selectSourceDocs); @@ -41,9 +42,27 @@ export default function Navigation({ const [selectedDocsModalState, setSelectedDocsModalState] = useState(isSelectedDocsSet ? 'INACTIVE' : 'ACTIVE'); + const navDiv = useRef(null); + useOutsideAlerter( + navDiv, + () => { + if ( + window.matchMedia('(max-width: 768px)').matches && + navState === 'ACTIVE' && + apiKeyModalState === 'INACTIVE' && + selectedDocsModalState === 'INACTIVE' && + !isDocsListOpen + ) { + setNavState('INACTIVE'); + } + }, + [navState, isDocsListOpen, apiKeyModalState, selectedDocsModalState], + ); + return ( <>
( + ref: RefObject, + handler: () => void, + deps: any[], +) { + useEffect(() => { + function handleClickOutside(this: Document, event: MouseEvent) { + if (ref.current && !ref.current.contains(event.target as Node)) { + handler(); + } + } + document.addEventListener('mousedown', handleClickOutside); + return () => { + document.removeEventListener('mousedown', handleClickOutside); + }; + }, [ref, ...deps]); +}