Add Menu component and implement toggle functionality in the header

This commit is contained in:
Thuan Bui
2026-01-02 12:10:37 +07:00
parent 405555da5d
commit 1135ecfa48
6 changed files with 37 additions and 0 deletions
+2
View File
@@ -1,9 +1,11 @@
--- ---
import Navigation from "./Navigation.astro"; import Navigation from "./Navigation.astro";
import Menu from "./Menu.astro";
--- ---
<header> <header>
<nav> <nav>
<Menu />
<Navigation /> <Navigation />
</nav> </nav>
</header> </header>
+7
View File
@@ -0,0 +1,7 @@
---
---
<button aria-expanded='false' aria-controls='main-menu' class='menu'>
Menu
</button>
+3
View File
@@ -22,5 +22,8 @@ import "../styles/global.css";
<li><a href='/posts/post-3/'>Post 3</a></li> <li><a href='/posts/post-3/'>Post 3</a></li>
</ul> </ul>
<Footer /> <Footer />
<script>
import "../scripts/menu.js";
</script>
</body> </body>
</html> </html>
+3
View File
@@ -18,5 +18,8 @@ const pageTitle = "Thuan's Astro Page";
<Header /> <Header />
<h1>{pageTitle}</h1> <h1>{pageTitle}</h1>
<Footer /> <Footer />
<script>
import "../scripts/menu.js";
</script>
</body> </body>
</html> </html>
+6
View File
@@ -0,0 +1,6 @@
const menu = document.querySelector('.menu');
menu?.addEventListener('click', () => {
const isExpanded = menu.getAttribute('aria-expanded') === 'true';
menu.setAttribute('aria-expanded', `${!isExpanded}`);
});
+16
View File
@@ -21,6 +21,14 @@ h1 {
} }
/* nav styles */ /* nav styles */
.menu {
background-color: #0d0950;
border: none;
color: #fff;
font-size: 1.2rem;
font-weight: bold;
padding: 5px 10px;
}
.nav-links { .nav-links {
width: 100%; width: 100%;
@@ -44,6 +52,10 @@ h1 {
background-color: #ff9776; background-color: #ff9776;
} }
:has(.menu[aria-expanded="true"]) .nav-links {
display: unset;
}
@media screen and (min-width: 636px) { @media screen and (min-width: 636px) {
.nav-links { .nav-links {
margin-left: 5em; margin-left: 5em;
@@ -57,6 +69,10 @@ h1 {
display: inline-block; display: inline-block;
padding: 15px 20px; padding: 15px 20px;
} }
.menu {
display: none;
}
} }
footer { footer {