web/src/components/scafold/NavBar.astro

266 lines · 5969 bytes

  1---
  2import FontAwesome from '@components/form/FontAwesome.svelte';
  3import ThemeSwitcher from '@components/form/ThemeSwitcher.svelte';
  4
  5const navLinks = [
  6	{ label: 'View All', href: '/all' },
  7	{ label: 'Search', href: '/search' },
  8	{ label: 'Changelog', href: '/changelog' },
  9	// { label: 'API', href: '/api' }, // TODO: restore once API is built out
 10	{ label: 'Saved', href: '/inventory', id: 'saved-nav-link', hidden: true },
 11	{ label: 'About', href: '/about' },
 12	{
 13		label: 'GitHub',
 14		href: 'https://github.com/lissy93/awesome-privacy',
 15		external: true,
 16	},
 17];
 18---
 19
 20<header class="site-header">
 21	<a href="/" class="logo" aria-label="Awesome Privacy - Home">
 22		<FontAwesome iconName="logo" />
 23		<span>Awesome Privacy</span>
 24	</a>
 25	<button
 26		class="burger"
 27		type="button"
 28		aria-label="Toggle navigation menu"
 29		aria-expanded="false"
 30		aria-controls="nav-menu"
 31	>
 32		<span class="burger-line"></span><span class="burger-line"></span><span
 33			class="burger-line"></span>
 34	</button>
 35	<nav id="nav-menu" aria-label="Main navigation">
 36		{
 37			navLinks.map((link) => (
 38				<a
 39					href={link.href}
 40					class="nav-link"
 41					id={link.id}
 42					style={link.hidden ? 'display:none' : undefined}
 43					target={link.external ? '_blank' : undefined}
 44					rel={link.external ? 'noopener noreferrer' : undefined}
 45				>
 46					{link.label}
 47					{link.external && <span class="sr-only">(opens in new tab)</span>}
 48				</a>
 49			))
 50		}
 51		<div class="theme-switcher">
 52			<ThemeSwitcher client:load />
 53		</div>
 54	</nav>
 55</header>
 56
 57<script>
 58	document.addEventListener('astro:page-load', () => {
 59		const burger = document.querySelector('.burger');
 60		const nav = document.getElementById('nav-menu');
 61		if (!burger || !nav) return;
 62
 63		const close = () => {
 64			nav.classList.remove('open');
 65			burger.classList.remove('is-active');
 66			burger.setAttribute('aria-expanded', 'false');
 67		};
 68
 69		burger.addEventListener('click', () => {
 70			const isOpen = nav.classList.toggle('open');
 71			burger.classList.toggle('is-active', isOpen);
 72			burger.setAttribute('aria-expanded', String(isOpen));
 73		});
 74
 75		nav.querySelectorAll('.nav-link').forEach((link) => {
 76			link.addEventListener('click', close);
 77		});
 78
 79		document.addEventListener('keydown', (e) => {
 80			if (e.key === 'Escape' && nav.classList.contains('open')) {
 81				close();
 82				(burger as HTMLElement).focus();
 83			}
 84		});
 85
 86		// Show "Saved" link if user has saved items
 87		try {
 88			const saved = JSON.parse(localStorage.getItem('savedServices') || '[]');
 89			const el = document.getElementById('saved-nav-link');
 90			if (el && saved.length > 0) el.style.display = '';
 91		} catch {
 92			/* ignore parse errors */
 93		}
 94	});
 95</script>
 96
 97<style lang="scss">
 98	.site-header {
 99		background: var(--accent-fg);
100		display: flex;
101		flex-wrap: wrap;
102		justify-content: space-between;
103		align-items: center;
104		border-bottom: 2px solid var(--accent-3);
105		position: relative;
106
107		.logo {
108			text-decoration: none;
109			display: flex;
110			align-items: center;
111			padding: 0 var(--space-sm);
112			span {
113				font-size: 2.4rem;
114				padding: 0 var(--space-md);
115				color: var(--foreground);
116				font-family: var(--font-subtitle);
117				font-weight: bold;
118			}
119			:global(svg) {
120				width: 2.5rem;
121				height: 2.5rem;
122				color: var(--accent-3);
123				transition: var(--transition-normal);
124			}
125			&:hover :global(svg) {
126				color: var(--accent);
127				transform: scale(1.05);
128			}
129		}
130
131		.burger {
132			display: none;
133			flex-direction: column;
134			justify-content: center;
135			gap: 5px;
136			padding: var(--space-sm);
137			margin-right: var(--space-sm);
138			background: none;
139			border: none;
140			cursor: pointer;
141			.burger-line {
142				display: block;
143				width: 24px;
144				height: 2px;
145				background: var(--foreground);
146				transition: var(--transition-fast);
147			}
148			&.is-active .burger-line {
149				&:nth-child(1) {
150					transform: translateY(7px) rotate(45deg);
151				}
152				&:nth-child(2) {
153					opacity: 0;
154				}
155				&:nth-child(3) {
156					transform: translateY(-7px) rotate(-45deg);
157				}
158			}
159		}
160
161		nav {
162			display: flex;
163			align-items: center;
164			height: 3rem;
165			.nav-link {
166				padding: var(--space-md);
167				font-size: var(--text-md);
168				font-family: var(--font-subtitle);
169				font-weight: bold;
170				color: var(--foreground);
171				text-decoration: none;
172				transition:
173					background 0.3s ease-in-out,
174					color 0.3s ease-in-out;
175				&:hover {
176					color: var(--accent-fg);
177					&:nth-child(4n + 1) {
178						background: var(--accent);
179					}
180					&:nth-child(4n + 2) {
181						background: var(--accent-2);
182						color: var(--foreground);
183					}
184					&:nth-child(4n + 3) {
185						background: var(--accent-3);
186					}
187					&:nth-child(4n + 4) {
188						background: var(--accent-4);
189						color: var(--foreground);
190					}
191				}
192			}
193			.theme-switcher {
194				transform: scale(0.7);
195				margin: 0.2rem auto;
196			}
197		}
198
199		@media (max-width: 1100px) {
200			nav .nav-link {
201				padding: var(--space-sm) var(--space-sm);
202				font-size: var(--text-base);
203			}
204			.logo {
205				span {
206					font-size: var(--text-3xl);
207				}
208			}
209		}
210
211		@media (max-width: 768px) {
212			flex-wrap: wrap;
213			.logo {
214				margin: 0 auto;
215				:global(svg) {
216					@media (max-width: 428px) {
217						display: none;
218					}
219				}
220				span {
221					@media (max-width: 428px) {
222						font-size: var(--text-2xl);
223					}
224				}
225			}
226			.burger {
227				display: flex;
228			}
229			nav {
230				display: none;
231				flex-direction: column;
232				width: 100%;
233				position: absolute;
234				top: 100%;
235				left: 0;
236				background: var(--accent-fg);
237				border-bottom: 2px solid var(--accent-3);
238				z-index: 100;
239				height: auto;
240				&.open {
241					display: flex;
242				}
243				.nav-link {
244					width: 100%;
245					text-align: center;
246					padding: var(--space-md) var(--space-lg);
247					border-bottom: 1px solid var(--accent-3-translucent);
248				}
249				.theme-switcher {
250					display: none;
251				}
252			}
253		}
254	}
255
256	.sr-only {
257		position: absolute;
258		width: 1px;
259		height: 1px;
260		padding: 0;
261		margin: -1px;
262		overflow: hidden;
263		clip: rect(0, 0, 0, 0);
264		border: 0;
265	}
266</style>