Use checkbox for navigation toggle to remove use of JavaScript

This commit is contained in:
Helen Chong 2024-06-07 17:24:50 +08:00
parent 5d112476b7
commit d8e4374363
1 changed files with 12 additions and 29 deletions

View File

@ -1,8 +1,9 @@
<nav class="navbar"> <nav class="navbar">
<button class="navbar__toggle" aria-label="Navigation menu toggle"> <input type="checkbox" name="navigation" id="nav-toggle" class="visually-hidden">
<span aria-hidden="true">☰</span> <label for="nav-toggle" aria-label="Navigation menu toggle" class="navbar__control">
<h2 class="navbar__title">Navigation</h2> <span aria-hidden="true">☰</span> Navigation
</button> </label>
<h2 class="visually-hidden">Navigation Manu</h2>
<ul class="navbar__links"> <ul class="navbar__links">
{% block navbarLinks %} {% block navbarLinks %}
{{ navbarLinks }} {{ navbarLinks }}
@ -20,25 +21,15 @@
top: 0; top: 0;
} }
.navbar__toggle, .navbar__control {
.navbar__title {
color: var(--clr-navbar-link);
font-size: 1.3rem; font-size: 1.3rem;
font-weight: 700;
} }
.navbar__toggle { #nav-toggle:focus + .navbar__control,
display: flex;
align-items: center;
gap: 0.3em;
border: none;
padding: 0;
margin: 0;
background-color: inherit;
}
.navbar__toggle:focus,
.navbar a:focus { .navbar a:focus {
outline: 0.15em solid var(--clr-navbar-link); outline: 0.15em solid var(--clr-navbar-link);
outline-offset: 0.1em;
} }
.navbar a { .navbar a {
@ -63,7 +54,7 @@
overflow: auto; overflow: auto;
} }
.navbar__links--show { #nav-toggle:checked ~ .navbar__links {
display: grid; display: grid;
} }
@ -73,7 +64,8 @@
padding: 0 0.6em; padding: 0 0.6em;
} }
.navbar__toggle { .navbar__toggle,
.navbar__control {
display: none; display: none;
} }
@ -87,12 +79,3 @@
} }
} }
</style> </style>
<script defer>
const navbarToggle = document.querySelector('.navbar__toggle');
const navbarLinks = document.querySelector(".navbar__links");
navbarToggle.addEventListener('click', () => {
navbarLinks.classList.toggle("navbar__links--show");
});
</script>