Skip to content

Commit ffdc5e9

Browse files
fix(web): close the mobile sheet on any navigation
The eleven per-item onClick closers missed the inline SearchBox's Enter path, so "every item in the sheet closes the sheet when activated" (specs/behaviors/app-shell.md) did not hold there. Derive the open state from the location.key the sheet was opened at instead: any client-side navigation closes it, including a same-path re-click. Derived during render rather than in a useLocation effect because react-hooks/set-state-in-effect flags the effect form; this is the same state-sync pattern ProjectEdit.tsx uses. The two anchors that do not change location (Contact mailto:, GitHub new tab) keep an explicit closer. Tests cover a sheet link click and the search Enter path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RdRwHvDupRLV8GuJpYKzEr
1 parent bebd034 commit ffdc5e9

2 files changed

Lines changed: 52 additions & 47 deletions

File tree

‎apps/web/src/components/AppHeader.tsx‎

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState } from 'react';
2-
import { Link, NavLink } from 'react-router';
2+
import { Link, NavLink, useLocation } from 'react-router';
33
import { Button } from '@/components/ui/button';
44
import {
55
DropdownMenu,
@@ -202,7 +202,15 @@ function GitHubLink() {
202202
}
203203

204204
export function AppHeader() {
205-
const [mobileOpen, setMobileOpen] = useState(false);
205+
const location = useLocation();
206+
// The sheet is open only for the location it was opened at, so any
207+
// client-side navigation — a NavLink or Enter in the inline search —
208+
// closes it without per-item onClick closers. Derived during render
209+
// rather than synced in an effect (react-hooks/set-state-in-effect).
210+
const [openedAtKey, setOpenedAtKey] = useState<string | null>(null);
211+
const mobileOpen = openedAtKey === location.key;
212+
const setMobileOpen = (open: boolean) =>
213+
setOpenedAtKey(open ? location.key : null);
206214

207215
return (
208216
<header className="sticky top-0 z-40 w-full border-b border-border bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 shadow-sm print:hidden">
@@ -280,64 +288,32 @@ export function AppHeader() {
280288
aria-label="Mobile navigation"
281289
className="flex flex-col gap-2 px-4 min-h-0 overflow-y-auto"
282290
>
283-
<NavLink
284-
to="/projects"
285-
className={navLinkClass}
286-
onClick={() => setMobileOpen(false)}
287-
>
291+
<NavLink to="/projects" className={navLinkClass}>
288292
Projects
289293
</NavLink>
290-
<NavLink
291-
to="/help-wanted"
292-
className={navLinkClass}
293-
onClick={() => setMobileOpen(false)}
294-
>
294+
<NavLink to="/help-wanted" className={navLinkClass}>
295295
Help Wanted
296296
</NavLink>
297-
<NavLink
298-
to="/members"
299-
className={navLinkClass}
300-
onClick={() => setMobileOpen(false)}
301-
>
297+
<NavLink to="/members" className={navLinkClass}>
302298
Members
303299
</NavLink>
304300
<Separator />
305301
<p className="text-xs text-muted-foreground font-medium uppercase tracking-wide">
306302
About
307303
</p>
308-
<NavLink
309-
to="/pages/mission"
310-
className={navLinkClass}
311-
onClick={() => setMobileOpen(false)}
312-
>
304+
<NavLink to="/pages/mission" className={navLinkClass}>
313305
Mission
314306
</NavLink>
315-
<NavLink
316-
to="/pages/leadership"
317-
className={navLinkClass}
318-
onClick={() => setMobileOpen(false)}
319-
>
307+
<NavLink to="/pages/leadership" className={navLinkClass}>
320308
Leadership
321309
</NavLink>
322-
<NavLink
323-
to="/pages/code-of-conduct"
324-
className={navLinkClass}
325-
onClick={() => setMobileOpen(false)}
326-
>
310+
<NavLink to="/pages/code-of-conduct" className={navLinkClass}>
327311
Code of Conduct
328312
</NavLink>
329-
<NavLink
330-
to="/pages/hackathons"
331-
className={navLinkClass}
332-
onClick={() => setMobileOpen(false)}
333-
>
313+
<NavLink to="/pages/hackathons" className={navLinkClass}>
334314
Hackathons
335315
</NavLink>
336-
<NavLink
337-
to="/sponsor"
338-
className={navLinkClass}
339-
onClick={() => setMobileOpen(false)}
340-
>
316+
<NavLink to="/sponsor" className={navLinkClass}>
341317
Sponsor
342318
</NavLink>
343319
<a
@@ -357,11 +333,7 @@ export function AppHeader() {
357333
>
358334
GitHub
359335
</a>
360-
<NavLink
361-
to="/volunteer"
362-
className={navLinkClass}
363-
onClick={() => setMobileOpen(false)}
364-
>
336+
<NavLink to="/volunteer" className={navLinkClass}>
365337
Volunteer
366338
</NavLink>
367339
</nav>

‎apps/web/tests/AppHeader.test.tsx‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,37 @@ describe('AppHeader', () => {
137137
'/volunteer',
138138
);
139139
});
140+
141+
it('closes the mobile sheet when a sheet link navigates', async () => {
142+
const user = userEvent.setup();
143+
renderWithRouter(<Wrapped />);
144+
145+
await user.click(screen.getByRole('button', { name: /open navigation menu/i }));
146+
const nav = await screen.findByRole('navigation', { name: /mobile navigation/i });
147+
148+
await user.click(within(nav).getByRole('link', { name: 'Members' }));
149+
150+
await waitFor(() => {
151+
expect(screen.queryByRole('dialog', { name: 'Menu' })).not.toBeInTheDocument();
152+
});
153+
});
154+
155+
it('closes the mobile sheet when the inline search navigates', async () => {
156+
const user = userEvent.setup();
157+
renderWithRouter(<Wrapped />);
158+
159+
await user.click(screen.getByRole('button', { name: /open navigation menu/i }));
160+
const dialog = await screen.findByRole('dialog', { name: 'Menu' });
161+
162+
// Scope to the sheet: jsdom applies no breakpoints, so the desktop
163+
// search box is in the DOM too.
164+
await user.type(
165+
within(dialog).getByRole('searchbox', { name: /search the site/i }),
166+
'civic{Enter}',
167+
);
168+
169+
await waitFor(() => {
170+
expect(screen.queryByRole('dialog', { name: 'Menu' })).not.toBeInTheDocument();
171+
});
172+
});
140173
});

0 commit comments

Comments
 (0)