1
0
mirror of https://github.com/picocms/pico-theme.git synced 2025-01-17 04:18:13 +01:00

JavaScript: Don't setup nav toggle when there's no menu

This commit is contained in:
Daniel Rudolf 2020-08-29 17:07:41 +02:00
parent e385f2ba0e
commit 6821662127
No known key found for this signature in database
GPG Key ID: A061F02CD8DE4538

View File

@ -28,10 +28,12 @@ function main()
// responsive menu // responsive menu
var menu = document.getElementById('nav'), var menu = document.getElementById('nav'),
menuToggle = document.getElementById('nav-toggle'), menuToggle = document.getElementById('nav-toggle');
toggleMenuEvent = function (event) {
if (menu && menuToggle) {
function toggleMenuEvent(event) {
if (event.type === 'keydown') { if (event.type === 'keydown') {
if ((event.keyCode != 13) && (event.keyCode != 32)) { if ((event.keyCode !== 13) && (event.keyCode !== 32)) {
return; return;
} }
} }
@ -49,8 +51,9 @@ function main()
menuToggle.setAttribute('aria-expanded', 'false'); menuToggle.setAttribute('aria-expanded', 'false');
utils.slideUp(menu); utils.slideUp(menu);
} }
}, }
onResizeEvent = function () {
function onResizeEvent() {
if (utils.isElementVisible(menuToggle)) { if (utils.isElementVisible(menuToggle)) {
menu.className = 'hidden'; menu.className = 'hidden';
menuToggle.addEventListener('click', toggleMenuEvent); menuToggle.addEventListener('click', toggleMenuEvent);
@ -61,10 +64,11 @@ function main()
menuToggle.removeEventListener('click', toggleMenuEvent); menuToggle.removeEventListener('click', toggleMenuEvent);
menuToggle.removeEventListener('keydown', toggleMenuEvent); menuToggle.removeEventListener('keydown', toggleMenuEvent);
} }
}; }
window.addEventListener('resize', onResizeEvent); window.addEventListener('resize', onResizeEvent);
onResizeEvent(); onResizeEvent();
} }
}
main(); main();