A JavaScript library to help you generate WCAG accessible menus in the DOM.
The supported menu types are:
Browser Support
Chrome | Firefox | Safari | Edge | Chromium | Webkit |
---|
last 2 versions | last 2 versions | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
Found something that doesn't work the way it should in one of the listed browsers above? Open an issue!
Installation
NPM
NPM is recommended for large-scale development, since it works well with bundlers like Webpack or Rollup.
# latest stable
npm install accessible-menu
CDN
For learning/prototyping purposes you can use the latest version with:
<script src="https://cdn.jsdelivr.net/npm/accessible-menu/dist/accessible-menu.js"></script>
For production environments, it is recommend to use a specific version to avoid unforseen breaking changes:
<script src="https://cdn.jsdelivr.net/npm/accessible-menu@3.0.7/dist/accessible-menu.min.js"></script>
Upgrading from version 2 to version 3?
Check out the upgrade guide!
Usage
To use accessible-menu, you first need to make sure your menu matches the following structure:
<ul id="example-menu">
<li><a href="/about">About</a></li>
<li class="dropdown">
<a href="#">Projects ▼</a>
<ul>
<li><a href="/projects/awesome">Awesome project</a></li>
<li><a href="/projects/not-so-awesome">Not-so-awesome project</a></li>
</ul>
</li>
<li><a href="/contact">Contact me</a></li>
</ul>
Include accessible-menu through import or bundled library in your project:
import AccessibleMenu from "accessible-menu";
or
<script src="https://cdn.jsdelivr.net/npm/accessible-menu@3.0.7/dist/accessible-menu.min.js"></script>
Once you have accessible-menu loaded, declare a new menu object.
menuElement
is required for all menus, while submenuItemSelector
is only required if you have submenus/dropdowns.
const menu = new AccessibleMenu.DisclosureMenu({
menuElement: document.querySelector("#example-menu"),
submenuItemSelector: "li.dropdown",
});
Bundled versions of each menu are provided in the dist and individual exports are provided in the index.
There are also compiled ES Module versions if you don't want to use an iife!
import { DisclosureMenu } from "accessible-menu";
or
<script src="https://cdn.jsdelivr.net/npm/accessible-menu@3.0.7/dist/disclosure-menu.min.js"></script>
then
const menu = new DisclosureMenu({
menuElement: document.querySelector("#example-menu"),
submenuItemSelector: "li.dropdown",
});
import { Menubar } from "accessible-menu";
or
<script src="https://cdn.jsdelivr.net/npm/accessible-menu@3.0.7/dist/menubar.min.js"></script>
then
const menu = new Menubar({
menuElement: document.querySelector("#example-menu"),
submenuItemSelector: "li.dropdown",
});
Treeview usage
import { Treeview } from "accessible-menu";
or
<script src="https://cdn.jsdelivr.net/npm/accessible-menu@3.0.7/dist/treeview.min.js"></script>
then
const menu = new Treeview({
menuElement: document.querySelector("#example-menu"),
submenuItemSelector: "li.dropdown",
});
Documentation
Examples
Looking for a working example of accessible-menu? Check out these codepens:
Bootstrap support
Looking to use this with Bootstrap? Because Bootstrap adds classes to the menu's containing element to show/hide the menu, you'll need custom open/close functions for your menu. Check out the accessible-menu-bootstrap-4 or accessible-menu-bootstrap-5 projects where all that is done for you!
Versioning
This project uses Semantic Versioning 2.0.0 to keep track of releases.
For more detailed information about SemVer, please see the official documentation.
Contributing
If you're interested in contributing to the project, please read the Contribution Guidelines. Any and all contributions must follow these guidelines or they will not be accepted.