Skip to content

Commit fbcf2f0

Browse files
committed
feat(header): close mobile menu on link click
1 parent d9550ca commit fbcf2f0

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/components/sections/header.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ function DesktopMenuItem({ link, onTriggerClick }: { link: NavItem; onTriggerCli
5656
);
5757
}
5858

59-
function MobileMenuGroup({ link }: { link: NavItem }) {
59+
/**
60+
* Mobile-only group that renders a top-level link and optional children.
61+
*
62+
* @param link Top-level nav item to render.
63+
* @param onLinkClick Invoked when any link is clicked;
64+
*/
65+
function MobileMenuGroup({ link, onLinkClick }: { link: NavItem; onLinkClick: () => void }) {
6066
return (
6167
<>
6268
<SmartLink
6369
href={link.href}
6470
className="hover:text-foreground/80"
71+
onClick={onLinkClick}
6572
>
6673
{link.label}
6774
</SmartLink>
@@ -72,6 +79,7 @@ function MobileMenuGroup({ link }: { link: NavItem }) {
7279
key={child.href}
7380
href={child.href}
7481
className="hover:text-foreground/80"
82+
onClick={onLinkClick}
7583
>
7684
{child.label}
7785
</SmartLink>
@@ -82,12 +90,14 @@ function MobileMenuGroup({ link }: { link: NavItem }) {
8290
);
8391
}
8492
export function Header() {
85-
const [isOpen, setIsOpen] = useState(false);
8693
const router = useRouter();
94+
const [isOpen, setIsOpen] = useState(false);
95+
const handleCloseMenu = () => setIsOpen(false);
8796

8897
return (
8998
<Collapsible
9099
asChild
100+
open={isOpen}
91101
onOpenChange={setIsOpen}
92102
>
93103
<div className="sticky top-0 z-[var(--z-header)]">
@@ -187,7 +197,10 @@ export function Header() {
187197
key={link.href}
188198
className="grid gap-4"
189199
>
190-
<MobileMenuGroup link={link} />
200+
<MobileMenuGroup
201+
link={link}
202+
onLinkClick={handleCloseMenu}
203+
/>
191204
</div>
192205
))}
193206
</nav>

0 commit comments

Comments
 (0)