Skip to content

Commit 3f84c4b

Browse files
committed
Add an interactive menu button component + integration
1 parent b7c4327 commit 3f84c4b

7 files changed

Lines changed: 50 additions & 1 deletion

File tree

tutorial/src/components/Header.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
---
2-
import Navigation from '../components/Navigation.astro';
2+
import Navigation from './Navigation.astro';
3+
import Menu from './Menu.astro';
34
---
45

56
<header>
67
<nav>
8+
<Menu />
79
<Navigation />
810
</nav>
911
</header>

tutorial/src/components/Menu.astro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
---
3+
<button aria-expanded="false" aria-controls="main-menu" class="menu">
4+
Menu
5+
</button>

tutorial/src/pages/about.astro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,10 @@ const skillColor = "crimson";
6969
{skills.map((skill) => <li class="skill">{skill}</li>)}
7070
</ul>
7171
<Footer />
72+
73+
<script>
74+
import "../scripts/menu.js";
75+
</script>
76+
7277
</body>
7378
</html>

tutorial/src/pages/blog.astro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ const pgTitle = "My Astro Learning Blog";
2626
<a href="/myAstroTest/posts/post-3">Post 3</a>
2727
</ul>
2828
<Footer />
29+
30+
<script>
31+
import "../scripts/menu.js";
32+
</script>
33+
2934
</body>
3035
</html>

tutorial/src/pages/index.astro

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@ const pgTitle = "My Astro Website Deployed with an Action";
2828
<Header />
2929
<h1>{pgTitle}</h1>
3030
<Footer />
31+
32+
<script>
33+
import "../scripts/menu.js";
34+
</script>
35+
36+
3137
</body>
3238
</html>

tutorial/src/scripts/menu.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const menu = document.querySelector('.menu');
2+
3+
menu?.addEventListener('click', () => {
4+
const isExpanded = menu.getAttribute('aria-expanded') === 'true';
5+
menu.setAttribute('aria-expanded', `${!isExpanded}`);
6+
});

tutorial/src/styles/global.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ h1 {
2020
font-size: 2.5rem;
2121
}
2222

23+
/* Menu */
24+
25+
.menu {
26+
background-color: #0d0950;
27+
border: none;
28+
color: #fff;
29+
font-size: 1.2rem;
30+
font-weight: bold;
31+
padding: 5px 10px;
32+
}
33+
2334
/* nav styles */
2435

2536
.nav-links {
@@ -44,6 +55,10 @@ h1 {
4455
background-color: #ff9776;
4556
}
4657

58+
:has(.menu[aria-expanded="true"]) .nav-links {
59+
display: unset;
60+
}
61+
4762
@media screen and (min-width: 636px) {
4863
.nav-links {
4964
margin-left: 5em;
@@ -57,4 +72,9 @@ h1 {
5772
display: inline-block;
5873
padding: 15px 20px;
5974
}
75+
76+
.menu {
77+
display: none;
78+
}
79+
6080
}

0 commit comments

Comments
 (0)