Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropdown doesn't hide after tabbing to next menu element #925

Open
craigs100 opened this issue Oct 26, 2024 · 1 comment
Open

Dropdown doesn't hide after tabbing to next menu element #925

craigs100 opened this issue Oct 26, 2024 · 1 comment

Comments

@craigs100
Copy link

Resource URL

https://www.w3.org/WAI/tutorials/menus/flyout/

Description

Approach 2:Use button as toggle

The code examples you provide do not show how the dropdown is hidden once you've tabbed away from it. I can get it to show and hide when clicking the arrow icon, but once you tab down the submenu times and onto the next menu item, yours hides, mine doesn't. I'm using the same code as you've shown.

Please either explain or modify the code.

Thanks.

Resource Shortname

wai-tutorials

@heybran
Copy link
Contributor

heybran commented Nov 18, 2024

Hi @craigs100,

It seems like there is more codes being applied to the menu then the ones shown in the tutorial. To close the dropdown when you tab out of the dropdown links, you would need to add a blur event listener to the links, something like this.

var menuItems = document.querySelectorAll("li.has-submenu");
Array.prototype.forEach.call(menuItems, function (el, i) {
  var activatingA = el.querySelector("a");
  var btn =
    '<button><span><span class="visuallyhidden">show submenu for “' +
    activatingA.text +
    "”</span></span></button>";
  activatingA.insertAdjacentHTML("afterend", btn);

  el.querySelector("button").addEventListener("click", function (event) {
    if (this.parentNode.className == "has-submenu") {
      this.parentNode.className = "has-submenu open";
      this.parentNode.querySelector("a").setAttribute("aria-expanded", "true");
      this.parentNode
        .querySelector("button")
        .setAttribute("aria-expanded", "true");
    } else {
      this.parentNode.className = "has-submenu";
      this.parentNode.querySelector("a").setAttribute("aria-expanded", "false");
      this.parentNode
        .querySelector("button")
        .setAttribute("aria-expanded", "false");
    }
    event.preventDefault();
  });

  const submenuLinks = el.querySelectorAll("ul li a");
  if (submenuLinks.length === 0) {
    return;
  }

  submenuLinks.forEach((a) => {
    // Add a blur event listener on the sub menu link.
    a.addEventListener("blur", function (event) {
      const timeout = setTimeout(function () {
        if (
          el.classList.contains("open") &&
          a.closest("ul").matches(":not(:focus-within)")
        ) {
          el.classList.remove("open");
          el.querySelector("a").setAttribute("aria-expanded", "false");
          el.querySelector("button").setAttribute("aria-expanded", "false");
        }
      }, 10);
    });
  });
});

I also created a CodePen to show it works. Free free to check it out: https://codepen.io/brandonzhang/pen/JjgVqgW

And @remibetin I think we should maybe update the original English tutorial: https://www.w3.org/WAI/tutorials/menus/flyout/?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants