diff --git a/network-api/networkapi/templates/fragments/blocks/nav/dropdown.html b/network-api/networkapi/templates/fragments/blocks/nav/dropdown.html index 011dbf20133..9f1dd95c18f 100644 --- a/network-api/networkapi/templates/fragments/blocks/nav/dropdown.html +++ b/network-api/networkapi/templates/fragments/blocks/nav/dropdown.html @@ -1,9 +1,9 @@ {% load static wagtailcore_tags wagtailadmin_tags nav_tags %} -{% fragment as content_base_styles %}tw-py-0 tw-px-0 tw-gap-8 tw-overflow-y-auto{% endfragment %} +{% fragment as content_base_styles %}tw-py-0 tw-px-0 tw-gap-8 tw-overflow-y-auto tw-transition-all tw-duration-500{% endfragment %} {% fragment as dropdown_selector_base %}tw-flex tw-flex-row tw-items-center tw-justify-between tw-w-full tw-py-12 tw-px-8 tw-gap-4{% endfragment %} -{% fragment as content_desktop %}large:tw-max-w-[1250px] xlarge:tw-max-w-[1350px] 2xl:tw-max-w-[1600px] large:tw-w-full large:tw-px-24 large:tw-grid large:tw-transition-all large:tw-duration-200 large:tw-invisible large:tw-opacity-0{% endfragment %} +{% fragment as content_desktop %}large:tw-max-w-[1250px] xlarge:tw-max-w-[1350px] 2xl:tw-max-w-[1600px] large:tw-w-full large:tw-px-24 large:tw-grid large:tw-duration-100 large:tw-invisible large:tw-opacity-0{% endfragment %} {% fragment as content_desktop_border %}large:tw-border large:tw-border-gray-20{% endfragment %} {% comment %} 40 (5rem) is the height of the navbar {% endcomment %} {% fragment as content_desktop_positioning %}large:tw-fixed large:tw-top-40 large:tw-left-0 large:tw-right-0 large:tw-mx-auto{% endfragment %} diff --git a/source/js/components/accordion/accordion.js b/source/js/components/accordion/accordion.js index e69cc82e52f..d2bdd7b1ace 100644 --- a/source/js/components/accordion/accordion.js +++ b/source/js/components/accordion/accordion.js @@ -16,7 +16,7 @@ class Accordion { bindEvents() { this.title.addEventListener("click", (e) => { e.preventDefault(); - let open = !this.content.classList.contains("tw-hidden"); + let open = this.title.getAttribute("aria-expanded") === "true"; if (open) { this.close(); @@ -37,14 +37,12 @@ class Accordion { } open() { - this.content.classList.remove("tw-hidden"); this.chevron.classList.remove("tw-rotate-180"); this.title.setAttribute("aria-expanded", "true"); this.content.setAttribute("aria-hidden", "false"); } close() { - this.content.classList.add("tw-hidden"); this.chevron.classList.add("tw-rotate-180"); this.title.setAttribute("aria-expanded", "false"); this.content.setAttribute("aria-hidden", "true"); diff --git a/source/js/components/nav/mobile-dropdown.js b/source/js/components/nav/mobile-dropdown.js index f5d4dba51f3..b484be41abf 100644 --- a/source/js/components/nav/mobile-dropdown.js +++ b/source/js/components/nav/mobile-dropdown.js @@ -27,9 +27,10 @@ class NavMobileDropdown extends Accordion { this.title.classList.add("tw-border-s-4"); } - getSiblings() { - let siblings = document.querySelectorAll(NavMobileDropdown.selector()); - return Array.from(siblings).filter((sibling) => sibling !== this.accordion); + getOpenSibling() { + return document + .querySelector(`${NavMobileDropdown.selector()} [aria-expanded="true"]`) + ?.closest(NavMobileDropdown.selector()); } bindEvents() { @@ -63,23 +64,29 @@ class NavMobileDropdown extends Accordion { } open() { - super.open(); if (this.isDropdownWayfindingActive === "true") { this.handleWayfindingOpenStyles(); } - if (!this.siblings) { - this.siblings = this.getSiblings(); - } - this.siblings.forEach((sibling) => { - const title = sibling.querySelector("[data-accordion-title]"); - const chevron = sibling.querySelector("[data-accordion-title] img"); - const content = sibling.querySelector("[data-accordion-content]"); + + let openAccordion = this.getOpenSibling(); + if (openAccordion) { + const title = openAccordion.querySelector("[data-accordion-title]"); + const chevron = openAccordion.querySelector("[data-accordion-title] img"); + const content = openAccordion.querySelector("[data-accordion-content]"); title.setAttribute("aria-expanded", "false"); content.setAttribute("aria-hidden", "true"); - content.classList.add("tw-hidden"); chevron.classList.add("tw-rotate-180"); - }); - this.title.scrollIntoView({ behavior: "smooth", block: "start" }); + content.style.height = "0"; + } + + super.open(); + let transitionEndHandler = () => { + this.title.scrollIntoView({ behavior: "smooth", block: "start" }); + this.content.removeEventListener("transitionend", transitionEndHandler); + }; + + this.content.addEventListener("transitionend", transitionEndHandler); + this.content.style.height = `${this.content.scrollHeight}px`; } close() { @@ -87,6 +94,7 @@ class NavMobileDropdown extends Accordion { if (this.isDropdownWayfindingActive === "true") { this.handleWayfindingClosedStyles(); } + this.content.style.height = "0"; } }