Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: navbar should close when user click anywhere on screen #565

Merged
merged 4 commits into from
Apr 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions components/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useState } from 'react';
import React, { useContext, useEffect, useState, useRef } from 'react';
import Head from 'next/head';
import Link from 'next/link';
import classnames from 'classnames';
Expand Down Expand Up @@ -30,6 +30,8 @@ export default function Layout({

const router = useRouter();

const mobileNavRef = useRef<HTMLDivElement>(null);

React.useEffect(
() => useStore.setState({ overlayNavigation: null }),
[router.asPath],
Expand All @@ -48,6 +50,21 @@ export default function Layout({
}
}, []);

useEffect(() => {
const handleCloseNavbar = (event: MouseEvent) => {
if (
mobileNavRef.current &&
(mobileNavRef.current as any).contains(event.target)
) {
useStore.setState({ overlayNavigation: null });
}
};

document.addEventListener('click', handleCloseNavbar);

return () => document.removeEventListener('click', handleCloseNavbar);
}, [mobileNavRef]);

const newTitle = `JSON Schema${metaTitle ? ` - ${metaTitle}` : ''}`;
return (
<div className='min-h-screen relative flex flex-col justify-between '>
Expand Down Expand Up @@ -84,7 +101,7 @@ export default function Layout({
</div>
</header>
{showMobileNav ? (
<div>
<div ref={mobileNavRef}>
<MobileNav />
{children}
</div>
Expand Down
Loading