|
| 1 | +export const setActiveDropdown = ( |
| 2 | + dropdownToggleButton: HTMLElement, |
| 3 | + isActive = true, |
| 4 | +) => { |
| 5 | + // set active state of the dropdown toggle (to slide the panel into view) |
| 6 | + const dropdownToggleEl = dropdownToggleButton.closest( |
| 7 | + ".p-navigation__item--dropdown-toggle", |
| 8 | + ); |
| 9 | + if (dropdownToggleEl) { |
| 10 | + dropdownToggleEl.classList.toggle("is-active", isActive); |
| 11 | + dropdownToggleEl.classList.toggle("is-selected", isActive); |
| 12 | + const globalNavButton = dropdownToggleEl.querySelector( |
| 13 | + ":scope > .p-navigation__link", |
| 14 | + ); |
| 15 | + // fix some states from global-nav elements in mobile |
| 16 | + if (globalNavButton) { |
| 17 | + globalNavButton.setAttribute("aria-expanded", isActive.toString()); |
| 18 | + globalNavButton.classList.toggle("is-selected", isActive); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + // set active state of the parent dropdown panel (to fade it out of view) |
| 23 | + const parentLevelDropdown = dropdownToggleEl?.closest( |
| 24 | + ".p-navigation__dropdown", |
| 25 | + ); |
| 26 | + if (parentLevelDropdown) { |
| 27 | + parentLevelDropdown.classList.toggle("is-active", isActive); |
| 28 | + } |
| 29 | + |
| 30 | + // set active state of the top navigation list under p-navigation__nav |
| 31 | + // to set the position of the sliding panel properly |
| 32 | + const topLevelNavigation = dropdownToggleButton.closest(".p-navigation__nav"); |
| 33 | + if (topLevelNavigation) { |
| 34 | + const topLevelItems = topLevelNavigation.querySelectorAll( |
| 35 | + ":scope > .p-navigation__items", |
| 36 | + ); |
| 37 | + |
| 38 | + for (const item of topLevelItems) { |
| 39 | + // in case there are more than one top level navigation lists, we need to |
| 40 | + // mark as active the one that contains the clicked button and hide the rest |
| 41 | + if (item.contains(dropdownToggleButton)) { |
| 42 | + item.classList.toggle("is-active", isActive); |
| 43 | + } else { |
| 44 | + item.classList.toggle("u-hide", isActive); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +export const setListFocusable = (list: Element) => { |
| 51 | + // turn on focusability for all direct children in the target dropdown |
| 52 | + if (list) { |
| 53 | + for (const item of list.children) { |
| 54 | + item.children[0].setAttribute("tabindex", "0"); |
| 55 | + } |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +export const setFocusable = (target: Element) => { |
| 60 | + // if target dropdown is not a list, find the list in it |
| 61 | + const isList = |
| 62 | + target.classList.contains("p-navigation__dropdown") || |
| 63 | + target.classList.contains("p-navigation__items"); |
| 64 | + |
| 65 | + if (!isList) { |
| 66 | + // find all lists in the target dropdown and make them focusable |
| 67 | + target.querySelectorAll(".p-navigation__dropdown").forEach((element) => { |
| 68 | + setListFocusable(element); |
| 69 | + }); |
| 70 | + } else { |
| 71 | + setListFocusable(target); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +export const collapseDropdown = ( |
| 76 | + dropdownToggleButton: HTMLElement, |
| 77 | + targetDropdown: HTMLElement, |
| 78 | +) => { |
| 79 | + targetDropdown.setAttribute("aria-hidden", "true"); |
| 80 | + setActiveDropdown(dropdownToggleButton, false); |
| 81 | +}; |
| 82 | + |
| 83 | +export const expandDropdown = ( |
| 84 | + dropdownToggleButton: HTMLElement, |
| 85 | + targetDropdown: HTMLElement, |
| 86 | +) => { |
| 87 | + setActiveDropdown(dropdownToggleButton); |
| 88 | + targetDropdown.setAttribute("aria-hidden", "false"); |
| 89 | + setFocusable(targetDropdown); |
| 90 | +}; |
| 91 | + |
| 92 | +const toggleAnimationPlaying = ( |
| 93 | + element: Element, |
| 94 | + animationDuration?: number, |
| 95 | +) => { |
| 96 | + const endAnimation = () => { |
| 97 | + element.classList.toggle("js-animation-playing", false); |
| 98 | + }; |
| 99 | + element.classList.toggle("js-animation-playing", true); |
| 100 | + // force browser to flush all pending style and layout calculations immediately |
| 101 | + void (element as HTMLElement).offsetWidth; |
| 102 | + setTimeout(endAnimation, animationDuration); |
| 103 | +}; |
| 104 | + |
| 105 | +export const setupAnimationStart = ( |
| 106 | + elements: Element[], |
| 107 | + animationDuration?: number, |
| 108 | +) => { |
| 109 | + elements.forEach((toggle: Element) => { |
| 110 | + toggleAnimationPlaying(toggle.parentElement!, animationDuration); |
| 111 | + }); |
| 112 | +}; |
0 commit comments