-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCustomHeader.astro
More file actions
172 lines (149 loc) · 4.66 KB
/
CustomHeader.astro
File metadata and controls
172 lines (149 loc) · 4.66 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
---
import Default from '@astrojs/starlight/components/SocialIcons.astro';
const base = import.meta.env.BASE_URL;
---
<nav class="custom-header-links" aria-label="Primary navigation">
<a href={`${base}introduction/overview/`} class="header-link">Overview</a>
<a href={`${base}getting-started/quick-start/`} class="header-link">Quick Start</a>
<a href={`${base}reference/cli/`} class="header-link">CLI</a>
<a href={`${base}guides/ui/`} class="header-link">UI</a>
<a href={`${base}faq/`} class="header-link">FAQ</a>
</nav>
<!-- Hamburger menu for narrow viewports where full nav would overflow -->
<div class="tablet-nav-wrapper">
<button class="hamburger-btn" aria-label="Toggle navigation menu" aria-expanded="false" aria-controls="tablet-nav-dropdown">
<span class="hamburger-icon" aria-hidden="true"></span>
</button>
<nav class="tablet-dropdown" id="tablet-nav-dropdown" aria-label="Primary navigation" hidden>
<a href={`${base}introduction/overview/`} class="dropdown-link">Overview</a>
<a href={`${base}getting-started/quick-start/`} class="dropdown-link">Quick Start</a>
<a href={`${base}reference/cli/`} class="dropdown-link">CLI</a>
<a href={`${base}guides/ui/`} class="dropdown-link">UI</a>
<a href={`${base}faq/`} class="dropdown-link">FAQ</a>
</nav>
</div>
<Default {...Astro.props} />
<style>
.custom-header-links {
display: flex;
align-items: center;
gap: 1rem;
margin-right: 1rem;
}
.header-link {
color: var(--sl-color-text);
text-decoration: none;
font-size: 0.875rem;
font-weight: 500;
padding: 0.25rem 0.75rem;
border-radius: 4px;
transition: color 0.15s ease, background-color 0.15s ease;
white-space: nowrap;
}
.header-link:hover {
color: var(--sl-color-text-accent);
background-color: rgba(110, 118, 129, 0.1);
}
/* Tablet navigation — hidden by default; shown via global CSS at narrow widths */
.tablet-nav-wrapper {
display: none;
position: relative;
margin-right: 0.5rem;
}
.hamburger-btn {
background: none;
border: none;
cursor: pointer;
padding: 0.5rem;
color: var(--sl-color-text);
border-radius: 4px;
min-height: 44px;
min-width: 44px;
display: inline-flex;
align-items: center;
justify-content: center;
}
.hamburger-btn:hover {
background-color: rgba(110, 118, 129, 0.1);
}
.hamburger-icon {
display: block;
width: 20px;
height: 2px;
background: currentColor;
position: relative;
}
.hamburger-icon::before,
.hamburger-icon::after {
content: '';
display: block;
width: 20px;
height: 2px;
background: currentColor;
position: absolute;
left: 0;
}
.hamburger-icon::before { top: -6px; }
.hamburger-icon::after { top: 6px; }
.tablet-dropdown {
position: absolute;
top: calc(100% + 0.5rem);
right: 0;
background: var(--sl-color-bg-nav, #161b22);
border: 1px solid rgba(48, 54, 61, 0.8);
border-radius: 8px;
padding: 0.5rem 0;
min-width: 180px;
z-index: 100;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
.dropdown-link {
display: block;
padding: 0.625rem 1rem;
color: var(--sl-color-text);
text-decoration: none;
font-size: 0.875rem;
font-weight: 500;
transition: background-color 0.15s ease;
}
.dropdown-link:hover {
background-color: rgba(110, 118, 129, 0.1);
color: var(--sl-color-text-accent);
}
</style>
<script>
let hamburgerAbort: AbortController | undefined;
function initHamburgerMenu() {
hamburgerAbort?.abort();
hamburgerAbort = new AbortController();
const { signal } = hamburgerAbort;
const hamburgerBtn = document.querySelector<HTMLButtonElement>('.hamburger-btn');
const tabletDropdown = document.querySelector<HTMLElement>('.tablet-dropdown');
if (!hamburgerBtn || !tabletDropdown) return;
hamburgerBtn.addEventListener('click', (e) => {
e.stopPropagation();
const isOpen = hamburgerBtn.getAttribute('aria-expanded') === 'true';
hamburgerBtn.setAttribute('aria-expanded', String(!isOpen));
tabletDropdown.hidden = isOpen;
if (!isOpen) {
const firstLink = tabletDropdown.querySelector<HTMLAnchorElement>('.dropdown-link');
firstLink?.focus();
}
}, { signal });
document.addEventListener('click', (e) => {
if (!hamburgerBtn.contains(e.target as Node) && !tabletDropdown.contains(e.target as Node)) {
hamburgerBtn.setAttribute('aria-expanded', 'false');
tabletDropdown.hidden = true;
}
}, { signal });
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && hamburgerBtn.getAttribute('aria-expanded') === 'true') {
hamburgerBtn.setAttribute('aria-expanded', 'false');
tabletDropdown.hidden = true;
hamburgerBtn.focus();
}
}, { signal });
}
initHamburgerMenu();
document.addEventListener('astro:page-load', initHamburgerMenu);
</script>