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: close mobile nav on blur of nav list #380

Closed
Show file tree
Hide file tree
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
13 changes: 6 additions & 7 deletions components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import styles from "./Header.module.css"

export interface IHeaderProps {
handleNavClick: () => void
showNavMobile: boolean
isNavMobileOpen: boolean
}

export const Header = ({ handleNavClick, showNavMobile }: IHeaderProps) => {
export const Header = ({ handleNavClick, isNavMobileOpen }: IHeaderProps) => {
return (
<>
<header>
Expand All @@ -21,12 +21,11 @@ export const Header = ({ handleNavClick, showNavMobile }: IHeaderProps) => {
className={styles.hamburgerBtn}
onClick={handleNavClick}
aria-label="Navigation menu"
aria-expanded={showNavMobile}>
{!showNavMobile && (
<IoMenuSharp size="1.5rem" aria-hidden="true" />
)}
{showNavMobile && (
aria-expanded={isNavMobileOpen}>
{isNavMobileOpen ? (
<IoCloseSharp size="1.5rem" aria-hidden="true" />
) : (
<IoMenuSharp size="1.5rem" aria-hidden="true" />
)}
</button>
</div>
Expand Down
15 changes: 9 additions & 6 deletions components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ interface ILayoutProps {
}

export const Layout = ({ activeNavLink, children }: ILayoutProps) => {
const [showNavMobile, setShowNavMobile] = useState<boolean>(false)
const [isNavMobileOpen, setIsNavMobileOpen] = useState<boolean>(false)

const handleNavClick = () => {
setShowNavMobile((prevState) => !prevState)
const toggleNavMobileOpen = () => {
setIsNavMobileOpen((prevState) => !prevState)
}

return (
<>
<SkipLink />
<Header handleNavClick={handleNavClick} showNavMobile={showNavMobile} />
<Header
handleNavClick={toggleNavMobileOpen}
isNavMobileOpen={isNavMobileOpen}
/>
<div className={styles.layoutContainer}>
<NavPrimary activeNavLink={activeNavLink} />
{showNavMobile && (
{isNavMobileOpen && (
<NavPrimaryMobile
activeNavLink={activeNavLink}
handleNavClick={handleNavClick}
closeNavMobile={toggleNavMobileOpen} // since, closeNavMobile prop can only be called when <NavPrimaryMobile /> is in open state so, toggleNavMobileOpen handler can be used for closing nav mobile.
/>
)}
<div className={styles.columnContainer}>
Expand Down
5 changes: 4 additions & 1 deletion components/Nav/NavItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { IPage } from "../../data/pages"
export interface INavItemProps {
page: IPage
activeNavLink: string
isLastNavItem?: boolean
handleNavClick?: () => void
}

export const NavItem = ({
page,
activeNavLink,
isLastNavItem = false,
handleNavClick,
}: INavItemProps) => {
const isLinkActive = activeNavLink === page.href
Expand All @@ -19,7 +21,8 @@ export const NavItem = ({
<Link href={page.href}>
<a
onClick={handleNavClick}
aria-current={isLinkActive ? "page" : false}>
aria-current={isLinkActive ? "page" : false}
data-last-nav-item={isLastNavItem}>
{page.name}
</a>
</Link>
Expand Down
17 changes: 13 additions & 4 deletions components/Nav/NavPrimaryMobile.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import { FocusEvent, useCallback } from "react"
import { NavItem } from "./NavItem"
import { pages } from "../../data/pages"
import styles from "./NavPrimary.module.css"

export interface INavProps {
activeNavLink: string
handleNavClick: () => void
closeNavMobile: () => void
}

export const NavPrimaryMobile = ({
activeNavLink,
handleNavClick,
closeNavMobile,
}: INavProps) => {
const handleNavBlur = useCallback((event: FocusEvent<HTMLAnchorElement>) => {
const isLastNavItem = /true/i.test(event.target.dataset?.lastNavItem ?? "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure what's going on in this line. I've not seen this syntax before. Can you explain what's happening?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a regex shorthand checking if the tested string contains true and returning a boolean. the tested string is either whatever event.target.dataset?.lastNavItem returns or an empty string. a needlessly complex way of doing that.

if (isLastNavItem) closeNavMobile()
}, [])
return (
<nav aria-label="Primary" className={styles.navPrimaryMobile}>
<nav
aria-label="Primary"
className={styles.navPrimaryMobile}
onBlur={handleNavBlur}>
<ul className={styles.navList}>
{pages.map((page, index) => (
<NavItem
key={page.name + index}
page={page}
activeNavLink={activeNavLink}
handleNavClick={handleNavClick}
handleNavClick={closeNavMobile}
isLastNavItem={pages.length === index + 1}
/>
))}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion stories/Header.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export const DesktopHeader = Template.bind({})
DesktopHeader.args = {
headerTitle: "Page Title",
handleNavClick: () => true,
showNavMobile: false,
isNavMobileOpen: false,
// eslint-disable-next-line @typescript-eslint/naming-convention
} as IHeaderProps
2 changes: 1 addition & 1 deletion stories/NavPrimaryMobile.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export const MobilePrimaryNav = Template.bind({})

MobilePrimaryNav.args = {
activeNavLink: "/",
handleNavClick: () => true,
closeNavMobile: () => true,
// eslint-disable-next-line @typescript-eslint/naming-convention
} as INavProps
Loading