blob: 6a213c0a3f94289066e2688bdeffaf65806d75ce (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
"use client";
import { useEffect, useId, useState, type ReactNode } from "react";
interface HeaderMenuProps {
children: ReactNode;
closeLabel: string;
label: string;
menuLabel: string;
}
/**
* The navigation of a site header.
*
* A wide screen shows the links in the header line and never shows the menu
* button. A phone shows the brand and the menu button; the button opens the
* links and the preferences as a panel under the header. A link chosen in the
* panel, the Escape key and a second press of the button close it.
*/
export function HeaderMenu({
children,
closeLabel,
label,
menuLabel,
}: HeaderMenuProps) {
const [open, setOpen] = useState(false);
const panelId = useId();
useEffect(() => {
if (!open) return;
const onKey = (event: KeyboardEvent) => {
if (event.key === "Escape") setOpen(false);
};
window.addEventListener("keydown", onKey);
return () => {
window.removeEventListener("keydown", onKey);
};
}, [open]);
return (
<div className="header-menu" data-open={open ? "true" : "false"}>
<button
aria-controls={panelId}
aria-expanded={open}
aria-label={open ? closeLabel : menuLabel}
className="menu-toggle"
onClick={() => {
setOpen((value) => !value);
}}
type="button"
>
<span aria-hidden="true" className="menu-toggle-bars" />
<span className="menu-toggle-text">{menuLabel}</span>
</button>
<nav
aria-label={label}
className="primary-nav"
id={panelId}
onClick={(event) => {
if ((event.target as HTMLElement).closest("a")) setOpen(false);
}}
>
{children}
</nav>
</div>
);
}
|