You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

4 years ago
  1. const container = document.querySelector(".container");
  2. const menu = document.querySelector(".menu");
  3. const mobileMenuTrigger = document.querySelector(".menu-trigger");
  4. const desktopMenu = document.querySelector(".menu__inner--desktop");
  5. const desktopMenuTrigger = document.querySelector(".menu__sub-inner-more-trigger");
  6. const menuMore = document.querySelector(".menu__sub-inner-more");
  7. const mobileQuery = getComputedStyle(document.body).getPropertyValue("--phoneWidth");
  8. const isMobile = () => window.matchMedia(mobileQuery).matches;
  9. const handleMenuClasses = () => {
  10. mobileMenuTrigger && mobileMenuTrigger.classList.toggle("hidden", !isMobile());
  11. menu && menu.classList.toggle("hidden", isMobile());
  12. menuMore && menuMore.classList.toggle("hidden", !isMobile());
  13. };
  14. // Common
  15. menu && menu.addEventListener("click", e => e.stopPropagation());
  16. menuMore && menuMore.addEventListener("click", e => e.stopPropagation());
  17. handleMenuClasses();
  18. document.body.addEventListener("click", () => {
  19. if (!isMobile() && menuMore && !menuMore.classList.contains("hidden")) {
  20. menuMore.classList.add("hidden");
  21. } else if (isMobile() && !menu.classList.contains("hidden")) {
  22. menu.classList.add("hidden");
  23. }
  24. });
  25. window.addEventListener("resize", handleMenuClasses);
  26. // Mobile menu
  27. mobileMenuTrigger &&
  28. mobileMenuTrigger.addEventListener("click", e => {
  29. e.stopPropagation();
  30. menu && menu.classList.toggle("hidden");
  31. });
  32. // Desktop menu
  33. desktopMenuTrigger &&
  34. desktopMenuTrigger.addEventListener("click", e => {
  35. e.stopPropagation();
  36. menuMore && menuMore.classList.toggle("hidden");
  37. if (menuMore.getBoundingClientRect().right > container.getBoundingClientRect().right) {
  38. menuMore.style.left = "auto";
  39. menuMore.style.right = 0;
  40. }
  41. });