All files / base docs-side-nav.ts

54.41% Statements 37/68
22.72% Branches 5/22
41.17% Functions 7/17
55.22% Lines 37/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174              11x     11x       11x 11x 6x 6x   6x 6x 6x   5x 5x   5x 5x 5x             9x 9x                         9x     9x         9x     9x           9x 3x 3x         9x 12x 4x   4x 4x 4x                 9x                                       2x       2x     1x       1x         1x                                       1x                                             1x            
/**
Toggles the expanded/collapsed classed on side navigation element.
 
@param {HTMLElement} sideNavigation The side navigation element.
@param {Boolean} show Whether to show or hide the drawer.
*/
export function toggleDrawer(sideNavigation: HTMLElement, show: boolean) {
  const toggleButtonOutsideDrawer = sideNavigation.querySelector(
    ".p-side-navigation__toggle"
  ) as HTMLElement | null;
  const toggleButtonInsideDrawer = sideNavigation.querySelector(
    ".p-side-navigation__toggle--in-drawer"
  ) as HTMLElement | null;
 
  Eif (sideNavigation) {
    if (show) {
      sideNavigation.classList.remove("is-drawer-collapsed");
      sideNavigation.classList.add("is-drawer-expanded");
 
      toggleButtonInsideDrawer?.focus();
      toggleButtonOutsideDrawer?.setAttribute("aria-expanded", "true");
      toggleButtonInsideDrawer?.setAttribute("aria-expanded", "true");
    } else {
      sideNavigation.classList.remove("is-drawer-expanded");
      sideNavigation.classList.add("is-drawer-collapsed");
 
      toggleButtonOutsideDrawer?.focus();
      toggleButtonOutsideDrawer?.setAttribute("aria-expanded", "false");
      toggleButtonInsideDrawer?.setAttribute("aria-expanded", "false");
    }
  }
}
 
// throttle util (for window resize event)
export function throttle(fn: (...args: unknown[]) => void, delay: number) {
  let timer: number | null = null;
  return function (this: unknown, ...args: unknown[]) {
    if (timer) clearTimeout(timer);
    timer = window.setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  };
}
 
/**
  Attaches event listeners for the side navigation toggles
  @param {HTMLElement} sideNavigation The side navigation element.
*/
export function setupSideNavigation(sideNavigation: HTMLElement) {
  const toggles = Array.from(
    sideNavigation.querySelectorAll(".js-drawer-toggle")
  ) as HTMLElement[];
  const drawerEl = sideNavigation.querySelector(
    ".p-side-navigation__drawer"
  ) as HTMLElement | null;
 
  // hide navigation drawer on small screens
  sideNavigation.classList.add("is-drawer-hidden");
 
  // setup drawer element
  drawerEl?.addEventListener("animationend", () => {
    if (!sideNavigation.classList.contains("is-drawer-expanded")) {
      sideNavigation.classList.add("is-drawer-hidden");
    }
  });
 
  window.addEventListener("keydown", (e: KeyboardEvent) => {
    Eif (e.key === "Escape") {
      toggleDrawer(sideNavigation, false);
    }
  });
 
  // setup toggle buttons
  toggles.forEach((toggle) => {
    (toggle as HTMLElement).addEventListener("click", (event: MouseEvent) => {
      event.preventDefault();
 
      Eif (sideNavigation) {
        sideNavigation.classList.remove("is-drawer-hidden");
        toggleDrawer(
          sideNavigation,
          !sideNavigation.classList.contains("is-drawer-expanded")
        );
      }
    });
  });
 
  // hide side navigation drawer when screen is resized
  window.addEventListener(
    "resize",
    throttle(() => {
      toggles.forEach((toggle: Element) => {
        return toggle.setAttribute("aria-expanded", "false");
      });
      // remove expanded/collapsed class names to avoid unexpected animations
      sideNavigation.classList.remove("is-drawer-expanded");
      sideNavigation.classList.remove("is-drawer-collapsed");
      sideNavigation.classList.add("is-drawer-hidden");
    }, 10)
  );
}
 
/**
  Attaches event listeners for all the side navigations in the document.
  @param {String} sideNavigationSelector The CSS selector matching side navigation elements.
*/
export function setupSideNavigations(sideNavigationSelector: string) {
  // Setup all side navigations on the page.
  const sideNavigations = Array.from(
    document.querySelectorAll(sideNavigationSelector)
  ) as HTMLElement[];
 
  sideNavigations.forEach(setupSideNavigation);
}
 
setupSideNavigations('.p-side-navigation, [class*="p-side-navigation--"]');
 
// Setup expandable side navigation
 
const expandToggles = document.querySelectorAll(
  ".p-side-navigation__expand"
) as NodeListOf<HTMLElement>;
 
// setup default values of aria-expanded for the toggle button, list title and list itself
const setup = (toggle: HTMLElement) => {
  const isExpanded = toggle.getAttribute("aria-expanded") === "true";
  if (!isExpanded) {
    toggle.setAttribute("aria-expanded", "false");
  }
  const item = toggle.closest(".p-side-navigation__item") as HTMLElement | null;
  const link = item?.querySelector(
    ".p-side-navigation__link"
  ) as HTMLElement | null;
  const nestedList = item?.querySelector(
    ".p-side-navigation__list"
  ) as HTMLElement | null;
  if (!link?.hasAttribute("aria-expanded")) {
    link?.setAttribute("aria-expanded", isExpanded.toString());
  }
  if (!nestedList?.hasAttribute("aria-expanded")) {
    nestedList?.setAttribute("aria-expanded", isExpanded.toString());
  }
};
 
const handleToggle = (e: Event) => {
  const item = (e.currentTarget as HTMLElement).closest(
    ".p-side-navigation__item"
  ) as HTMLElement | null;
  const button = item?.querySelector(
    ".p-side-navigation__expand"
  ) as HTMLElement | null;
  const link = item?.querySelector(
    ".p-side-navigation__link"
  ) as HTMLElement | null;
  const nestedList = item?.querySelector(
    ".p-side-navigation__list"
  ) as HTMLElement | null;
  [button, link, nestedList].forEach((el) => {
    if (el) {
      el.setAttribute(
        "aria-expanded",
        el.getAttribute("aria-expanded") === "true" ? "false" : "true"
      );
    }
  });
};
 
expandToggles.forEach((toggle) => {
  setup(toggle);
  toggle.addEventListener("click", (e) => {
    handleToggle(e);
  });
});