Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@evermade/menu-toolkit
Advanced tools
Menu Toolkit provides functionality to help create accessible and usable menus. Toolkit provides JavaScript functions to create the menu and it's up to you to style it and extend it.
Demos
Function menuFromHTML
adds accessible enhancements and events for WordPress navigation menus (although you can use it for different menus).
Two different interaction modes are supported:
click
: sub menus are opened only on clickhover
: sub menus open also with hoverThings to note:
<ul>
part of your menu. You may need additional logic for example toggles or displaying mobile menu in modalIf you’re using a bundler (such as Webpack or Rollup), you can install through npm:
npm install @evermade/menu-toolkit
Import the menuFromHTML
and create a menu.
import { menuFromHTML } from "@evermade/menu-toolkit";
const menuEl = document.querySelector('.js-main-menu');
if (menuEl) {
const menu = menuFromHTML(
menuEl,
{
action: 'click'
}
);
}
In general it's recommended to wrap the menu in <nav>
element but we'll show just the parts this function needs.
<ul class="js-main-menu">
<li><a href="/">Home</a></li>
<li class="menu-item-has-children">
<a href="#">Work</a>
<ul class="sub-menu">
<li><a href="/websites">Websites</a></li>
<li><a href="/e-commerce">E-commerce</a></li>
<li><a href="/apps">Apps</a></li>
</li>
<li class="menu-item-has-children">
<a href="#">Services</a>
<ul class="sub-menu">
<li><a href="/design">Design</a></li>
<li><a href="/development">Development</a></li>
<li class="menu-item-has-children">
<a href="#">Care</a>
<ul class="sub-menu">
<li><a href="/upkeep">Upkeep</a></li>
<li><a href="/monitoring">Monitoring</a></li>
</ul>
</ul>
</li>
<li><a href="/blog">Blog</a></li>
</ul>
This functions adds toggles to open/close menus.
Before:
<li class="menu-item-has-children">
<a href="/services">Services</a>
</li>
After (if shouldWrapAnchorToButton
is true):
<li class="menu-item-has-children">
<a href="/services" hidden>Services</a>
<button aria-expanded="false" data-toggle-type="cover">
<span>Services</span>
<svg></svg>
</button>
</li>
After (if shouldWrapAnchorToButton
is false):
<li class="menu-item-has-children">
<a href="/services">Services</a>
<button aria-expanded="false" data-toggle-type="icon">
<span class="screen-reader-text">Sub menu</span>
<svg></svg>
</button>
</li>
There's much more you'll actually need but here's functional things that you need so that opening and closing sub menus work:
[hidden] {
display: none !important;
}
.sub-menu {
opacity: 0;
pointer-events: none;
visibility: hidden;
}
.sub-menu.is-open {
opacity: 1;
visibility: visible;
pointer-events: auto;
}
Tips and tricks:
:hover
because hovering will already add is-open
class to sub menu[data-toggle-type="cover"]
and [data-toggle-type="icon"]
[data-toggle-type="cover"]
than for <a>
.sub-menu.animate-open
and .sub-menu.animate-close
(or whatever classes you configure)[data-toggle-type="cover"][aria-expanded="true"]
because aria-expanded is updated without any delay:focus
style in place for both <a>
and <button>
elementsThere are many settings (in object format) that can be passed as 2nd argument with following types:
const menu = menuFromHTML( menuEl, {
action: 'click',
subMenuAnchorSelector: '.menu-item-has-children > a',
subMenuListItemSelector: '.menu-item-has-children',
openSubMenuClass: 'is-open',
buttonClass: '',
visuallyHiddenClass: 'screen-reader-text',
expandChildMenuText: 'Sub menu',
hoverTimeout: 750,
hoverOpenDelay: 0,
buttonIcon:
'<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M17.5 11.6L12 16l-5.5-4.4.9-1.2L12 14l4.5-3.6 1 1.2z"></path></svg>',
shouldWrapAnchorToButton: null,
activeListItemSelector: '.current-menu-item',
openActiveSubMenuOnCreate: false,
closeSubMenuOnOutsideClick: true,
animateOpen: false,
animateClose: false,
animateOpenClass: 'animate-open',
animateCloseClass: 'animate-close',
animateOpenMaxExecutionTime: 250,
animateCloseMaxExecutionTime: 250,
onBeforeCreate: null,
onAfterCreate: null,
onBeforeOpenSubMenu: null,
onAfterOpenSubMenu: null,
onBeforeCloseSubMenu: null,
onAfterCloseSubMenu: null,
});
Either click
or hover
.
Targets main <a>
elements of menu items that have children. If using WordPress menus or WordPress-like markup, no changes needed.
If you are working with mega menu where only 1st level opens anything you can set this for example: :scope > .menu-item-has-children > a
.
Defaults to .menu-item-has-children > a
.
Targets main <li>
elements of menu items that have children. If using WordPress menus or WordPress-like markup, no changes needed.
If you are working with mega menu where only 1st level opens anything you can set this for example: :scope > .menu-item-has-children
.
Defaults to .menu-item-has-children
.
Class to be added when sub menu is open. CSS styles to show/hide sub menu should be based on this class.
Defaults to is-open
.
Class to be added to toggle buttons. Even without class you can use data-attributes as selectors.
Defaults to no added class.
Class to be added to visually hidden elements. This is used for screen readers.
Defaults to screen-reader-text
.
Text to be added to toggle buttons. This is used for screen readers.
Defaults to Sub menu
.
Time in milliseconds to wait before closing sub menu after hover moves outside of element. This is used to prevent accidental closing of sub menus.
Defaults to 750
.
Time in milliseconds to wait before opening sub menu after hover. This is used to prevent accidental opening of sub menus and let cursor quickly move over the menu without opening it.
Defaults to 0
.
HTML string to be used as toggle button icon.
Defaults to SVG icon.
Weather <a>
elements should be wrapped inside <button>
elements (that act as toggles). Alternatively toggle button can be separate element next to <a>
element.
Example:
shouldWrapAnchorToButton: true
shouldWrapAnchorToButton: ( should, a, level ) => {
return level === 1;
}
Defaults to wrapping when href is "#".
Targets active menu item. If using WordPress menus or WordPress-like markup, no changes needed.
Defaults to .current-menu-item
.
If active menu item has sub menu, should it be opened on create. This should usually only be used in mobile menus or menus that are not visible on page load.
Defaults to false
.
Should sub menus be closed when clicking outside of menu. This should usually not be used on mobile menus because it may cause accidental closing of menu levels.
Defaults to true
.
Should sub menus be animated when opening.
Defaults to false
.
Should sub menus be animated when closing.
Defaults to false
.
Class to be added when sub menu is opening. CSS styles to animate opening of sub menu should be based on this class. You can use for example animation or transitions to animate opening.
Defaults to animate-open
.
Class to be added when sub menu is closing. CSS styles to animate closing of sub menu should be based on this class. You can use for example animation or transitions to animate closing.
Defaults to animate-close
.
Maximum time in milliseconds to wait for opening animation to finish. This is fallback in case animation eventhandler is not triggered.
Defaults to 250
.
Maximum time in milliseconds to wait for closing animation to finish. This is fallback in case animation eventhandler is not triggered.
Defaults to 250
.
Callback function to be called before menu is created. This is called only once. If you need to modify markup before menu is created, this is the place to do it.
Example:
onBeforeCreate: ( menu ) => {
// wrap all anchors to span
const anchors = menu.querySelectorAll('a');
anchors.forEach((anchor) => {
const span = document.createElement('span');
anchor.parentNode.insertBefore(span, anchor);
span.appendChild(anchor);
});
}
Defaults to null
.
Callback function to be called after menu is created. This is called only once. If you need to modify markup after menu is created, this is the place to do it.
Example:
onAfterCreate: ( menu ) => {}
Defaults to null
.
Callback function to be called before sub menu is opened. This is called every time sub menu is opened. If you need to modify markup or styling in a way that could cause flickering after sub menu is opened, this is the place to do it.
Example:
onBeforeOpenSubMenu: (ul) => {
// check that sub-menu fits on right side of parent
if (ul.getBoundingClientRect().right > (window.innerWidth || document.documentElement.clientWidth)) {
ul.classList.add('is-out-of-bounds');
} else {
ul.classList.remove('is-out-of-bounds');
}
}
Defaults to null
.
Callback function to be called after sub menu is opened. This is called every time sub menu is opened.
Example:
onAfterOpenSubMenu: (ul) => {}
Defaults to null
.
Callback function to be called before sub menu is closed. This is called every time sub menu is closed.
Example:
onBeforeCloseSubMenu: (ul) => {}
Defaults to null
.
Callback function to be called after sub menu is closed. This is called every time sub menu is closed. If you need to modify markup or styling in a way that could cause flickering before sub menu is closed, this is the place to do it.
Example:
onAfterCloseSubMenu: (ul) => {
// remove out of bounds class
ul.classList.remove('is-out-of-bounds');
}
Defaults to null
.
This library has taken a lot from MEOM/navigation. Although we have a different approach to many things, it has been a great source of inspiration and ideas.
Add support for hover open delay. This allows cursors to quickly move over the menu without opening it. This is good for huge mega menus.
Prevent double initialization of menu. This could happen by human error or by some cookie consent scripts that re-run scripts after consent is given.
Fix issue of shouldWrapAnchorToButton copying links content with textContent and in case link actually has some inner HTML elements those would not be copied. This is now changed so that all inner HTML elements are moved which is the appropriate thing to do.
Install tools npm install
and build npm run build
or develop with npm run watch
.
Releasing new version:
package.json
FAQs
Toolkit for developing accessible menus.
We found that @evermade/menu-toolkit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.