src/components/layout/Header.astro 3.2 K raw
1
---
2
import ThemeToggle from "../ThemeToggle.astro";
3
import { MENU_LINKS } from "@/data/constants";
4
5
const url = new URL(Astro.request.url);
6
---
7
8
<script>
9
	import { toggleClass } from "@/utils";
10
11
	document.addEventListener("DOMContentLoaded", () => {
12
		const header = document.getElementById("main-header") as HTMLElement;
13
		const toggleMenuButton = document.getElementById("toggle-navigation-menu") as HTMLButtonElement;
14
		let menuOpen = false;
15
16
		toggleMenuButton.addEventListener("click", () => {
17
			toggleClass(header, "menu-open");
18
			menuOpen = !menuOpen;
19
			toggleMenuButton.setAttribute("aria-expanded", menuOpen.toString());
20
		});
21
	});
22
</script>
23
24
<header id="main-header" class="group relative mb-28 flex items-center sm:pl-[4.5rem]">
25
	<div class="flex sm:flex-col">
26
		<a
27
			href="/"
28
			class="inline-flex items-center sm:relative sm:inline-block"
29
			aria-current={url.pathname === "/" ? "page" : false}
30
      >
31
32
      <svg
33
  key="0"
34
  class="mr-3 h-10 w-6 pb-4 sm:absolute sm:left-[-4.5rem] sm:mr-0 sm:h-20 sm:w-12"
35
  fill="none"
36
  stroke="currentColor"
37
  strokeLinecap="round"
38
  strokeLinejoin="round"
39
  strokeWidth="2"
40
  viewBox="0 0 24 24"
41
  xmlns="http://www.w3.org/2000/svg"
42
>
43
  <path d="m8 3 4 8 5-5 5 15H2L8 3z" />
44
</svg>
45
46
47
			<span class="text-xl font-bold sm:text-2xl">Steve Simkins</span>
48
		</a>
49
		<nav
50
			id="navigation-menu"
51
			class="absolute -inset-x-4 top-14 hidden flex-col items-end gap-y-4 rounded-md bg-[color:var(--theme-menu-bg)] py-4 text-accent shadow backdrop-blur group-[.menu-open]:z-50 group-[.menu-open]:flex sm:static sm:z-auto sm:mt-1 sm:-ml-4 sm:flex sm:flex-row sm:items-center sm:divide-x sm:divide-dashed sm:divide-accent sm:rounded-none sm:bg-transparent sm:py-0 sm:shadow-none sm:backdrop-blur-none"
52
			aria-label="Main menu"
53
		>
54
			{
55
				MENU_LINKS.map((link) => (
56
					<a
57
						href={link.path}
58
						class="py-4 px-4 sm:py-0 sm:hover:underline"
59
						aria-current={url.pathname === link.path ? "page" : false}
60
						rel="prefetch"
61
					>
62
						{link.title}
63
					</a>
64
				))
65
			}
66
		</nav>
67
	</div>
68
	<button
69
		id="toggle-navigation-menu"
70
		class="group relative ml-8 h-7 w-7 sm:invisible sm:hidden"
71
		type="button"
72
		aria-label="Open main menu"
73
		aria-expanded="false"
74
		aria-haspopup="menu"
75
	>
76
		<svg
77
			id="line-svg"
78
			class="absolute top-1/2 left-1/2 h-full w-full -translate-x-1/2 -translate-y-1/2 transition-all group-aria-expanded:scale-0 group-aria-expanded:opacity-0"
79
			aria-hidden="true"
80
			focusable="false"
81
			xmlns="http://www.w3.org/2000/svg"
82
			fill="none"
83
			viewBox="0 0 24 24"
84
			stroke-width="1.5"
85
			stroke="currentColor"
86
		>
87
			<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 9h16.5m-16.5 6.75h16.5"></path>
88
		</svg>
89
		<svg
90
			id="cross-svg"
91
			class="absolute top-1/2 left-1/2 h-full w-full -translate-x-1/2 -translate-y-1/2 scale-0 text-accent opacity-0 transition-all group-aria-expanded:scale-100 group-aria-expanded:opacity-100"
92
			class="text-accent"
93
			aria-hidden="true"
94
			focusable="false"
95
			xmlns="http://www.w3.org/2000/svg"
96
			fill="none"
97
			viewBox="0 0 24 24"
98
			stroke-width="1.5"
99
			stroke="currentColor"
100
		>
101
			<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"></path>
102
		</svg>
103
	</button>
104
</header>