1.4.0 • Published 1 year ago

a11y-nav v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

A11Y Nav

Library for accessible navigations. https://mmahandev.github.io/a11y-nav/

Currently a work in progress, but it has bare minimum functionality needed for production use. Will be adding features as I need them.

Install

Using npm:

npm install a11y-nav

Using browser:

<!-- In the <head> -->
<link rel="stylesheet" href="//unpkg.com/a11y-nav@latest/dist/a11y-nav.css" />

<!-- End of <body> -->
<script src="//unpkg.com/a11y-nav@latest/dist/a11y-nav.umd.js"></script>

Usage

Build your standard UL/LI navigation and give all menus a <button> with an aria-expanded and aria-controls attribute. Point the ID to the menu element. Example:

<ul class="a11y-nav">
  <li>
    <button aria-expanded="false" aria-controls="id_here">First Level Item</button>
    <ul id="id_here">
      <li>
        <button aria-expanded="false" aria-controls="id_here_2">Second Level Item</button>
        <ul id="id_here_2">
            <li><a href="#">Example Inner Link 1</a></li>
            <li><a href="#">Example Inner Link 2</a></li>
        </ul>
      </li>
      <li><a href="#">Example Link</a></li>
    </ul>
  </li>
</ul>

Then initialize A11YNav. The options is an optional parameter:

const nav = new A11YNav(document.querySelector(".a11y-nav"), options);

Options

// Default options:
{
  // adds delay for toggling menu open/close animation classes
  animate: true,
  // amount of time in ms for menu open/close animation
  duration: 300,
  // Enables use of arrow keys to navigate menus
  useArrowKeys: true,
  // Enables closing of menus when focus leaves the nav
  closeOnBlur: true,
  // Class to add to body when a menu is open. If false, no class is added.
  bodyClass: "a11y-nav-menu-open",
  // Focus menu that just opened
  focusOnOpen: true,
}

Methods

// Example use of the 'destroy' method.
nav.destroy();
MethodDescription
destroyRemoves everything that the A11YNav created in the DOM
closeAllMenusCloses all currently opened menus

Events

// Example use of the 'afterOpen' event
const navEl = document.querySelector(".a11y-nav");

navEl.addEventListener("afterOpen", function (e) {
  console.log(e.detail.menu);
});

const a11yNav = new A11YNav(navEl);
EventDetailDescription
inita11yNavFires after nav initialization
beforeOpena11yNav, menuFires before menu open
afterOpena11yNav, menuFires after menu open
beforeClosea11yNav, menuFires before menu close
afterClosea11yNav, menuFires after menu close
destroya11yNavFires after the nav is destroyed

Events listeners should be added before initializing the nav if possible. For example init will require it.

Browser support

Currently works in all browsers except IE 11 until I figure out how to correctly polyfill the UMD build.

1.4.0

1 year ago

1.3.0

1 year ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago