adamelliotfields commited on
Commit
e667f80
1 Parent(s): 4c2b2fd

Fix menu scrolling

Browse files
Files changed (1) hide show
  1. demo.js +15 -5
demo.js CHANGED
@@ -1,11 +1,21 @@
1
  () =>{
 
2
  const menu = document.querySelector("#menu");
3
  const menuButton = menu.querySelector("button");
 
4
 
5
- // scroll on accordion click
6
- menuButton.addEventListener("click", () => {
7
- requestAnimationFrame(() => {
 
 
8
  menu.scrollIntoView({ behavior: "instant" });
9
- });
10
- });
 
 
 
 
 
 
11
  }
 
1
  () =>{
2
+ const header = document.querySelector("header");
3
  const menu = document.querySelector("#menu");
4
  const menuButton = menu.querySelector("button");
5
+ const menuSpan = menuButton.querySelector("span:first-child");
6
 
7
+ const updateMenuText = () => {
8
+ const isOpen = menuButton.classList.contains("open");
9
+ menuSpan.textContent = isOpen ? "Close menu" : "Open menu";
10
+
11
+ if (isOpen) {
12
  menu.scrollIntoView({ behavior: "instant" });
13
+ } else {
14
+ header.scrollIntoView({ behavior: "instant" });
15
+ }
16
+ };
17
+
18
+ const observer = new MutationObserver(updateMenuText);
19
+ observer.observe(menuButton, { attributes: true, attributeFilter: ["class"] });
20
+ updateMenuText();
21
  }