|
| 1 | +(function () { |
| 2 | + var toggle = document.querySelector("[data-mobile-nav-toggle]"); |
| 3 | + var panel = document.querySelector("[data-mobile-nav-panel]"); |
| 4 | + var lastFocused = null; |
| 5 | + var focusableSelectors = [ |
| 6 | + "a[href]", |
| 7 | + "button:not([disabled])", |
| 8 | + "textarea:not([disabled])", |
| 9 | + "input:not([type=\"hidden\"]):not([disabled])", |
| 10 | + "select:not([disabled])", |
| 11 | + "[tabindex]:not([tabindex=\"-1\"])", |
| 12 | + ]; |
| 13 | + |
| 14 | + if (!toggle || !panel) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + function getFocusableElements() { |
| 19 | + return Array.prototype.slice.call( |
| 20 | + panel.querySelectorAll(focusableSelectors.join(",")) |
| 21 | + ).filter(function (el) { |
| 22 | + return !el.hasAttribute("disabled") && el.offsetParent !== null; |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + var content = panel.querySelector("[data-mobile-nav-content]"); |
| 27 | + |
| 28 | + function setOpen(open) { |
| 29 | + toggle.setAttribute("aria-expanded", open ? "true" : "false"); |
| 30 | + toggle.setAttribute("aria-label", open ? "Sluit navigatie" : "Open navigatie"); |
| 31 | + panel.setAttribute("aria-hidden", open ? "false" : "true"); |
| 32 | + panel.classList.toggle("hidden", !open); |
| 33 | + |
| 34 | + if (open) { |
| 35 | + lastFocused = document.activeElement; |
| 36 | + var focusables = getFocusableElements(); |
| 37 | + if (focusables.length) { |
| 38 | + focusables[0].focus(); |
| 39 | + } |
| 40 | + if (content) { |
| 41 | + content.classList.remove("opacity-0", "-translate-y-2"); |
| 42 | + content.classList.add("opacity-100", "translate-y-0"); |
| 43 | + } |
| 44 | + } else if (lastFocused && typeof lastFocused.focus === "function") { |
| 45 | + lastFocused.focus(); |
| 46 | + lastFocused = null; |
| 47 | + if (content) { |
| 48 | + content.classList.add("opacity-0", "-translate-y-2"); |
| 49 | + content.classList.remove("opacity-100", "translate-y-0"); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + setOpen(false); |
| 55 | + |
| 56 | + toggle.addEventListener("click", function () { |
| 57 | + var isOpen = toggle.getAttribute("aria-expanded") === "true"; |
| 58 | + setOpen(!isOpen); |
| 59 | + }); |
| 60 | + |
| 61 | + panel.addEventListener("keydown", function (event) { |
| 62 | + if (event.key !== "Tab") { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + var focusables = getFocusableElements(); |
| 67 | + if (!focusables.length) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + var first = focusables[0]; |
| 72 | + var last = focusables[focusables.length - 1]; |
| 73 | + var active = document.activeElement; |
| 74 | + |
| 75 | + if (event.shiftKey && active === first) { |
| 76 | + event.preventDefault(); |
| 77 | + last.focus(); |
| 78 | + } else if (!event.shiftKey && active === last) { |
| 79 | + event.preventDefault(); |
| 80 | + first.focus(); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + panel.addEventListener("click", function (event) { |
| 85 | + var target = event.target; |
| 86 | + if (target && target.tagName && target.tagName.toLowerCase() === "a") { |
| 87 | + setOpen(false); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + document.addEventListener("keydown", function (event) { |
| 92 | + if (event.key === "Escape") { |
| 93 | + setOpen(false); |
| 94 | + } |
| 95 | + }); |
| 96 | +})(); |
0 commit comments