From 773cba683828609f7dfbbfc63e087a7e1371fd8c Mon Sep 17 00:00:00 2001 From: Amy Date: Wed, 16 Jun 2021 19:16:22 -0400 Subject: [PATCH] Add mobile navbar functionality --- components/Navbar.tsx | 86 +++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/components/Navbar.tsx b/components/Navbar.tsx index 5d2a5f60..40d8fcbe 100644 --- a/components/Navbar.tsx +++ b/components/Navbar.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import { Image } from "./Image"; @@ -13,6 +13,16 @@ interface NavLink { }[]; } +interface NavItemProps { + name: string; + route: string; + submenu?: { + name: string; + route: string; + }[]; + closeMenu: () => void; +} + const menu: NavLink[] = [ { name: "Home", @@ -72,10 +82,19 @@ const menu: NavLink[] = [ }, ]; -function NavItem(props: NavLink) { +function NavItem(props: NavItemProps) { const router = useRouter(); const externalLink = props.route.includes("http://") || props.route.includes("https://"); + const [mobileDropdownOpen, setMobileDropdownOpen] = useState(false); + + function handleClick() { + if (document.activeElement instanceof HTMLElement) { + document.activeElement.blur(); + } + props.closeMenu(); + } + return ( <> {externalLink ? ( @@ -83,11 +102,7 @@ function NavItem(props: NavLink) { href={props.route} target="_blank" rel="noopener noreferrer" - onClick={() => { - if (document.activeElement instanceof HTMLElement) { - document.activeElement.blur(); - } - }} + onClick={handleClick} > {props.name} @@ -102,11 +117,7 @@ function NavItem(props: NavLink) { ? styles.currentPage : "" } - onClick={() => { - if (document.activeElement instanceof HTMLElement) { - document.activeElement.blur(); - } - }} + onClick={handleClick} > {props.name} @@ -114,14 +125,31 @@ function NavItem(props: NavLink) { )} {(props.submenu?.length ?? 0) > 0 ? ( <> - -