-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMobileNav.astro
More file actions
114 lines (98 loc) · 2.94 KB
/
Copy pathMobileNav.astro
File metadata and controls
114 lines (98 loc) · 2.94 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
import type { NavLink } from "@lib/nav";
interface Props {
navLinks: NavLink[];
discordUrl: string;
}
const { navLinks, discordUrl } = Astro.props;
const menuId = "mobile-nav-menu";
---
<mobile-nav class="md:hidden">
<button
type="button"
aria-expanded="false"
aria-controls={menuId}
class="border-border bg-surface-elevated text-ink hover:bg-surface-hover inline-flex items-center justify-center rounded-lg border px-3 py-2 text-sm font-medium"
>
Menu
</button>
<nav
id={menuId}
aria-label="Mobile navigation"
aria-hidden="true"
class="border-border bg-surface-elevated absolute top-full right-0 left-0 border-b px-4 py-4 shadow-md aria-hidden:hidden"
>
<ul class="flex flex-col gap-3">
{
navLinks.map((link) => (
<li>
<a
href={link.href}
class="text-ink-muted hover:text-brand-500 block text-sm font-medium"
>
{link.label}
</a>
</li>
))
}
<li>
<a
href={discordUrl}
target="_blank"
rel="noopener noreferrer"
class="bg-mod hover:bg-mod-700 inline-flex rounded-lg px-4 py-2 text-sm font-semibold text-white"
>
Join Discord
</a>
</li>
</ul>
</nav>
</mobile-nav>
<script>
import { defineCustomElement } from "@lib/custom-elements";
class MobileNav extends HTMLElement {
private button!: HTMLButtonElement;
private nav!: HTMLElement;
private firstLink!: HTMLAnchorElement | null;
#open = false;
connectedCallback() {
this.button = this.querySelector("button")!;
this.nav = this.querySelector("nav")!;
this.firstLink = this.nav.querySelector("a");
this.button.addEventListener("click", this.#toggle);
this.nav.addEventListener("click", this.#onNavClick);
document.addEventListener("keydown", this.#onKeyDown);
}
disconnectedCallback() {
this.button.removeEventListener("click", this.#toggle);
this.nav.removeEventListener("click", this.#onNavClick);
document.removeEventListener("keydown", this.#onKeyDown);
}
#toggle = () => {
this.#open ? this.#close() : this.#openMenu();
};
#openMenu() {
this.#open = true;
this.nav.removeAttribute("aria-hidden");
this.button.setAttribute("aria-expanded", "true");
this.button.textContent = "Close";
this.firstLink?.focus();
}
#close() {
this.#open = false;
this.nav.setAttribute("aria-hidden", "true");
this.button.setAttribute("aria-expanded", "false");
this.button.textContent = "Menu";
}
#onNavClick = (e: Event) => {
if ((e.target as HTMLElement).closest("a")) this.#close();
};
#onKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape" && this.#open) {
this.#close();
this.button.focus();
}
};
}
defineCustomElement("mobile-nav", MobileNav);
</script>