Jump to content

ADA compliance on Hamburger menu

Recommended Posts

How might one add keyboard navigation to the Hamburger menu open and close button? I have been able to implement keyboard navigation everywhere else except on the mobile header navigation 'Menu' and 'Close' 

Screen Shot 2021-06-11 at 9.49.41 AM.png

mobile-nav-expanded.png

Link to comment

Hi @Kurt12345678,

Can you post or DM me a link to your site?

We've got some free code in the bonus section of this article for desktop dropdown navigation accessibility, but the fix for mobile depends on which template you're using.

Edited by tcp13

The above post may be outdated. I’m no longer active in the Squarespace ecosystem, so I won’t see your direct messages. For better resources about web accessibility, I’ve documented some of my thoughts on my forum profile.

Link to comment

@Kurt12345678 Try adding the following within Settings > Advanced > Code Injection:

<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>

<!-- Accessible Mobile Navigation - Five Template -->
<script>
  // assign role=button so screen readers announce the label as a button 
  $("#mobile-navigation-label").attr("role", "button");
  
  // assign an aria-label as an invisible screen reader label so the button isn't empty
  $("#mobile-navigation-label").attr("aria-label", "toggle mobile navigation menu");
  
  // add tabindex value so button is focusable by keyboard navigation
  $("#mobile-navigation-label").attr("tabindex", "0");
  
  // add event listener so button can be triggered by pressing enter or space keys
  $("#mobile-navigation-label").keyup(function(event) {
    event.preventDefault();
    if (event.keyCode == 13 || event.keyCode == 32) {
      document.activeElement.click();
    }
  });
</script>

 

With this expected result using exclusively keyboard navigation:

765457289_ezgif.com-gif-maker(8).thumb.gif.d2a0520e4f7e4af90e56e6c110d39bf7.gif

 

This code is based on the standardized WAI-ARIA design pattern for a button: https://www.w3.org/TR/wai-aria-practices/#button

You cited WCAG Success Criterion 3.2.1, but I don't think that's applicable here (it prohibits a change of context, such as if focusing the button caused the menu to automatically open without pressing enter). More pertinent are 2.1.1 and 4.1.2 😎 Regardless though, good on ya for being proactive about accessibility!

Hope this helps!
-Tyler

 

Edited by tcp13

The above post may be outdated. I’m no longer active in the Squarespace ecosystem, so I won’t see your direct messages. For better resources about web accessibility, I’ve documented some of my thoughts on my forum profile.

Link to comment
17 hours ago, tcp13 said:

<!-- jQuery --> <script src="https://code.jquery.com/jquery-3.5.0.min.js"></script> <!-- Accessible Mobile Navigation - Five Template --> <script> // assign role=button so screen readers announce the label as a button $("#mobile-navigation-label").attr("role", "button"); // assign an aria-label as an invisible screen reader label so the button isn't empty $("#mobile-navigation-label").attr("aria-label", "toggle mobile navigation menu"); // add tabindex value so button is focusable by keyboard navigation $("#mobile-navigation-label").attr("tabindex", "0"); // add event listener so button can be triggered by pressing enter or space keys $("#mobile-navigation-label").keyup(function(event) { event.preventDefault(); if (event.keyCode == 13 || event.keyCode == 32) { document.activeElement.click(); } }); </script>

Confirming the code worked as needed when added to the Footer block of Code Injection. Thanks @tcp13

Link to comment
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.