Navbar menu stays hidden after clicking burger icon without JavaScript toggle
0 reputation · 22 Oct 2021, 07:18 UTC
0 reputation · 22 Oct 2021, 07:18 UTC
When using Bulma's navbar component without any JavaScript, clicking the burger icon does not reveal the menu because the is-active class is never toggled.
The goal is to determine whether Bulma offers an official, optional JavaScript helper for this toggle, or if developers must always write their own script, and what the recommended minimal implementation should be to maintain accessibility and consistency across projects.
is-active class on both .navbar-burger and .navbar-menu while updating ARIA attributes?Bulma does not provide an optional JavaScript helper for the navbar burger toggle; the toggle must be implemented by the developer.
document.addEventListener('DOMContentLoaded', function () {
// Find all burger buttons
document.querySelectorAll('.navbar-burger').forEach(function ($el) {
$el.addEventListener('click', function () {
var target = $el.dataset.target;
var $target = document.getElementById(target);
// Toggle is‑active on button and menu
$el.classList.toggle('is-active');
$target.classList.toggle('is-active');
// Update ARIA attributes
var isExpanded = $el.classList.contains('is-active');
$el.setAttribute('aria-expanded', String(isExpanded));
$el.setAttribute('aria-label', isExpanded ? 'Close menu' : 'Open menu');
});
});
});
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.