menu.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. document.addEventListener("DOMContentLoaded", function () {
  2. // Make tables responsive by wrapping them in a div and making them scrollable
  3. const tables = document.querySelectorAll("table.docutils")
  4. tables.forEach(function(table){
  5. table.outerHTML = '<div class="responsive-table__container">' + table.outerHTML + "</div>"
  6. })
  7. const togglerInput = document.querySelector(".toggler__input")
  8. const togglerLabel = document.querySelector(".toggler__label")
  9. const sideMenu = document.querySelector(".menu-wrapper")
  10. const menuItems = document.querySelectorAll(".menu")
  11. const doc = document.querySelector(".document")
  12. const body = document.querySelector("body")
  13. function closeMenu() {
  14. togglerInput.checked = false
  15. sideMenu.setAttribute("aria-expanded", "false")
  16. sideMenu.setAttribute("aria-hidden", "true")
  17. togglerLabel.setAttribute("aria-pressed", "false")
  18. body.style.overflow = "visible"
  19. }
  20. function openMenu() {
  21. togglerInput.checked = true
  22. sideMenu.setAttribute("aria-expanded", "true")
  23. sideMenu.setAttribute("aria-hidden", "false")
  24. togglerLabel.setAttribute("aria-pressed", "true")
  25. body.style.overflow = "hidden"
  26. }
  27. // Close menu when link on the sideMenu is clicked
  28. sideMenu.addEventListener("click", function (event) {
  29. let target = event.target
  30. if (target.tagName.toLowerCase() !== "a") {
  31. return
  32. }
  33. closeMenu()
  34. })
  35. // Add accessibility data when sideMenu is opened/closed
  36. togglerInput.addEventListener("change", function (_event) {
  37. togglerInput.checked ? openMenu() : closeMenu()
  38. })
  39. // Make sideMenu links tabbable only when visible
  40. for(let menuItem of menuItems) {
  41. if(togglerInput.checked) {
  42. menuItem.setAttribute("tabindex", "0")
  43. } else {
  44. menuItem.setAttribute("tabindex", "-1")
  45. }
  46. }
  47. // Close sideMenu when document body is clicked
  48. doc.addEventListener("click", function () {
  49. if (togglerInput.checked) {
  50. closeMenu()
  51. }
  52. })
  53. })