Skip to content

Commit 42a2cc8

Browse files
committed
Deduplicate mobile navigation close handling
1 parent 91833db commit 42a2cc8

3 files changed

Lines changed: 37 additions & 31 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useState } from 'octane';
2+
3+
const MOBILE_NAVIGATION_CLOSE_MS = 240;
4+
5+
export function useMobileNavigation() {
6+
const [open, setOpen] = useState(false);
7+
const close = () => {
8+
if (!open) return;
9+
setOpen(false);
10+
// Reveal the target only after the panel transition has finished.
11+
const reduce =
12+
typeof window.matchMedia === 'function' &&
13+
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
14+
return new Promise<void>((resolve) =>
15+
window.setTimeout(resolve, reduce ? 0 : MOBILE_NAVIGATION_CLOSE_MS),
16+
);
17+
};
18+
19+
return {
20+
open,
21+
toggle: () => setOpen((current) => !current),
22+
close,
23+
};
24+
}

‎website/src/layouts/DocsLayout.tsrx‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
// Docs section shell: sidebar of documents (from the registry) + the routed
22
// document via <Outlet/>.
33
import { Link, Outlet, useParams } from '@octanejs/tanstack-router';
4-
import { useState } from 'octane';
54
import { OnThisPage } from '../components/OnThisPage.tsrx';
65
import { docGroups, defaultDoc, findDoc } from '../content/docs.ts';
6+
import { useMobileNavigation } from '../hooks/use-mobile-navigation.ts';
77

88
export function DocsLayout() @{
9-
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
10-
const closeMobileNavigation = () => {
11-
if (!mobileMenuOpen) return;
12-
setMobileMenuOpen(false);
13-
// Reveal the target only after the overlay has closed. A timer also covers
14-
// browsers that omit transitionend.
15-
const reduce =
16-
typeof window.matchMedia === 'function' &&
17-
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
18-
return new Promise<void>((resolve) => window.setTimeout(resolve, reduce ? 0 : 240));
19-
};
9+
const mobileNavigation = useMobileNavigation();
2010

2111
// The Link's own data-status covers `/docs/$slug`, but the bare `/docs` index
2212
// renders the default doc (quick-start) without a slug in the URL — so no link
@@ -31,14 +21,14 @@ export function DocsLayout() @{
3121
<button
3222
type="button"
3323
class="sidebar-mobile-toggle"
34-
aria-expanded={mobileMenuOpen}
24+
aria-expanded={mobileNavigation.open}
3525
aria-controls="docs-sidebar-nav"
36-
onClick={() => setMobileMenuOpen((open) => !open)}
26+
onClick={mobileNavigation.toggle}
3727
>
3828
<span>Documentation</span>
3929
<span class="sidebar-mobile-icon" aria-hidden="true">+</span>
4030
</button>
41-
<div class={['sidebar-panel', mobileMenuOpen && 'is-open']}>
31+
<div class={['sidebar-panel', mobileNavigation.open && 'is-open']}>
4232
<div class="sidebar-panel-inner">
4333
<nav id="docs-sidebar-nav" class="sidebar-nav" aria-label="Documentation">
4434
<p class="sidebar-title">Documentation</p>
@@ -56,7 +46,7 @@ export function DocsLayout() @{
5646
params={{ slug: doc.slug }}
5747
class={['sidebar-link', indexActive(doc.slug) && 'is-active']}
5848
onClick={() => {
59-
void closeMobileNavigation();
49+
void mobileNavigation.close();
6050
}}
6151
>
6252
{doc.title as string}
@@ -65,7 +55,7 @@ export function DocsLayout() @{
6555
<OnThisPage
6656
sections={doc.sections}
6757
resetKey={doc.slug}
68-
onNavigate={closeMobileNavigation}
58+
onNavigate={mobileNavigation.close}
6959
/>
7060
}
7161
</li>

‎website/src/pages/benchmarks/Benchmarks.tsrx‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
// then the octane-internal de-opt ("authoring cliff") suites. All numbers are the
77
// checked-in benchmark scores; the page cannot drift from the repo because the
88
// JSON is imported at build time.
9-
import { useState } from 'octane';
109
import { BenchBars } from '../../components/BenchBars.tsrx';
1110
import { BenchmarkExplorer } from '../../components/BenchmarkExplorer.tsrx';
1211
import { OnThisPage } from '../../components/OnThisPage.tsrx';
1312
import { FRAMEWORK_CARDS, OCTANE_CARDS } from '../../content/benchmarks.ts';
1413
import { createHomeSummary } from '../../content/home-benchmark.ts';
14+
import { useMobileNavigation } from '../../hooks/use-mobile-navigation.ts';
1515
import { useTitle } from '../../hooks/use-title.ts';
1616

1717
const OVERVIEW = createHomeSummary(FRAMEWORK_CARDS);
@@ -37,30 +37,22 @@ export const BENCH_SECTIONS: readonly { id: string; title: string; level?: 2 | 3
3737

3838
export function Benchmarks() @{
3939
useTitle('Octane — Benchmarks');
40-
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
41-
const closeMobileNavigation = () => {
42-
if (!mobileMenuOpen) return;
43-
setMobileMenuOpen(false);
44-
const reduce =
45-
typeof window.matchMedia === 'function' &&
46-
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
47-
return new Promise<void>((resolve) => window.setTimeout(resolve, reduce ? 0 : 240));
48-
};
40+
const mobileNavigation = useMobileNavigation();
4941

5042
<div class="benchpage">
5143
<div class="benchpage-frame">
5244
<aside class="benchpage-sidebar">
5345
<button
5446
type="button"
5547
class="benchpage-sidebar-toggle"
56-
aria-expanded={mobileMenuOpen}
48+
aria-expanded={mobileNavigation.open}
5749
aria-controls="benchmark-sidebar-nav"
58-
onClick={() => setMobileMenuOpen((open) => !open)}
50+
onClick={mobileNavigation.toggle}
5951
>
6052
<span>Benchmarks</span>
6153
<span class="benchpage-sidebar-icon" aria-hidden="true">+</span>
6254
</button>
63-
<div class={['benchpage-sidebar-panel', mobileMenuOpen && 'is-open']}>
55+
<div class={['benchpage-sidebar-panel', mobileNavigation.open && 'is-open']}>
6456
<div class="benchpage-sidebar-panel-inner">
6557
<div id="benchmark-sidebar-nav">
6658
<a class="benchpage-sidebar-link" href="/benchmarks" aria-current="page">
@@ -69,7 +61,7 @@ export function Benchmarks() @{
6961
<OnThisPage
7062
sections={BENCH_SECTIONS}
7163
resetKey="benchmarks"
72-
onNavigate={closeMobileNavigation}
64+
onNavigate={mobileNavigation.close}
7365
/>
7466
</div>
7567
</div>

0 commit comments

Comments
 (0)