@meom/navigation
Advanced tools
Comparing version 1.0.3 to 1.0.4
# Changelog | ||
## Version 1.0.4 released December 31, 2021 | ||
### Added | ||
- New demo about using buttons as dropdowns. | ||
## Version 1.0.3 released November 3, 2021 | ||
@@ -3,0 +6,0 @@ ### Added |
@@ -1,2 +0,2 @@ | ||
/*! navigation 1.0.3 — © MEOM */ | ||
/*! navigation 1.0.4 — © MEOM */ | ||
function t(t,e,s){t&&e&&(t.classList.add(e),t.addEventListener("animationend",(function n(){t.classList.remove(e),s&&t.classList.remove(s),t.removeEventListener("animationend",n,!1)}),!1))}function e(t,e){if(void 0===t||0>=e.length)return;const s="true"===t.getAttribute(`aria-${e}`)?"false":"true";t.setAttribute(`aria-${e}`,s)}function s(t,e,s={}){this._handleNav=this.handleNav.bind(this),this._handleSubNav=this.handleSubNav.bind(this),this._handleCloseNav=this.handleCloseNav.bind(this),this._handleCloseSubNav=this.handleCloseSubNav.bind(this),this._closeAllSubMenus=this.closeAllSubMenus.bind(this),this._setSubMenu=this.setSubMenu.bind(this),this._closeAllSubMenuToggles=this.closeAllSubMenuToggles.bind(this),this._handleDocClick=this.handleDocClick.bind(this),this._handleFocus=this.handleFocus.bind(this);const n={action:"click",subNavAnchors:".js-site-nav-items > .menu-item-has-children > a",subSubNavAnchors:".js-site-nav-items .sub-menu > .menu-item-has-children > a",toggleNavClass:!0,toggleNavClassValue:"is-opened",toggleSubNavClassValue:"is-opened",closeNavOnEscKey:!0,closeNavOnLastTab:!1,subNavClass:".sub-menu",subToggleButtonClasses:"",subSubToggleButtonClasses:"",animateSubNav:!1,animateSubNavClass:"",visuallyHiddenClass:"screen-reader-text",expandChildNavText:"Sub menu",dropDownIcon:'<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>',onCreate:null,onOpenNav:null,onCloseNav:null,onOpenSubNav:null,onCloseSubNav:null,...s};this.$element=t,this.$toggle=e,this.settings=n,this.navOpened=!1,this.$subNavs=this.$element.querySelectorAll(this.settings.subNavAnchors),this.$subSubNavs=this.$element.querySelectorAll(this.settings.subSubNavAnchors),this.create()}s.prototype.create=function(){return this.$toggle.setAttribute("aria-expanded","false"),this.$element.setAttribute("data-meom-nav","navigation"),this.$subNavs.forEach((function(t,e){"click"===this.settings.action&&t.setAttribute("hidden","");const s=document.createElement("button");s.setAttribute("data-meom-nav","sub-toggle"),s.setAttribute("aria-expanded","false"),s.className=`${this.settings.subToggleButtonClasses}`,s.type="button","click"===this.settings.action&&(s.innerHTML=`${t.textContent}${this.settings.dropDownIcon}`),"hover"===this.settings.action&&(s.innerHTML=`<span class="${this.settings.visuallyHiddenClass}">${this.settings.expandChildNavText}</span>${this.settings.dropDownIcon}`),t.after(s)}),this),this.$subSubNavs.forEach((function(t,e){const s=document.createElement("button");s.setAttribute("data-meom-nav","sub-sub-toggle"),s.setAttribute("aria-expanded","false"),s.setAttribute("aria-controls",`sub-sub-menu-${e}`),t.nextElementSibling&&(t.nextElementSibling.id=`sub-sub-menu-${e}`),s.className=`${this.settings.subSubToggleButtonClasses}`,s.type="button",s.innerHTML=`<span class="${this.settings.visuallyHiddenClass}">${this.settings.expandChildNavText}</span>${this.settings.dropDownIcon}`,t.after(s)}),this),this.$toggle.addEventListener("click",this._handleNav,!1),this.$element.addEventListener("click",this._handleSubNav,!1),document.addEventListener("keydown",this._handleCloseNav,!1),this.$element.addEventListener("keydown",this._handleCloseSubNav,!1),this.$element.addEventListener("keydown",this._handleFocus,!1),document.addEventListener("click",this._handleDocClick,!1),this.settings.onCreate&&"function"==typeof this.settings.onCreate&&this.settings.onCreate(this.$element,this.$toggle),this},s.prototype.handleNav=function(t){return this.navOpened?(e(this.$toggle,"expanded"),this.settings.toggleNavClass&&this.$element.classList.remove(this.settings.toggleNavClassValue),this.$toggle&&this.$toggle.focus(),this.navOpened=!1,this._closeAllSubMenus(),this._closeAllSubMenuToggles(),this.settings.onCloseNav&&"function"==typeof this.settings.onCloseNav&&this.settings.onCloseNav(this.$element,this.$toggle,t)):(e(this.$toggle,"expanded"),this.settings.toggleNavClass&&this.$element.classList.add(this.settings.toggleNavClassValue),this.navOpened=!0,this.settings.onOpenNav&&"function"==typeof this.settings.onOpenNav&&this.settings.onOpenNav(this.$element,this.$toggle,t)),this},s.prototype.handleSubNav=function(t){const s=t.target,n=s.closest('[data-meom-nav="sub-toggle"]'),i=s.closest('[data-meom-nav="sub-sub-toggle"]');return n||i?(s.nextElementSibling.classList.contains(this.settings.toggleSubNavClassValue)||s.matches('[data-meom-nav="sub-sub-toggle"]')||(this._closeAllSubMenus(),this._closeAllSubMenuToggles()),e(s,"expanded"),s.nextElementSibling&&this._setSubMenu(s.nextElementSibling,t),this):this},s.prototype.handleCloseNav=function(t){return this.navOpened&&this.settings.closeNavOnEscKey&&27===t.keyCode&&this._handleNav(t),this},s.prototype.handleCloseSubNav=function(t){const e=document.querySelector(`${this.settings.subNavClass}.${this.settings.toggleSubNavClassValue}`);if(e){const s=e.querySelectorAll(["a[href]","area[href]","input:not([disabled])","select:not([disabled])","textarea:not([disabled])","button:not([disabled])"]),n=s[s.length-1];9!==t.keyCode||t.shiftKey||t.target!==n||(this._closeAllSubMenus(),this._closeAllSubMenuToggles());const i=e.previousElementSibling;i&&9===t.keyCode&&t.shiftKey&&t.target===i&&(this._closeAllSubMenus(),this._closeAllSubMenuToggles())}if(27===t.keyCode){if(t.target.matches('[data-meom-nav="sub-toggle"][aria-expanded="true"]'))return this._handleSubNav(t),this._closeAllSubMenus(),this._closeAllSubMenuToggles(),this;const e=t.target.closest(`${this.settings.subNavClass}.${this.settings.toggleSubNavClassValue}`);if(e){const t=e.previousElementSibling;t&&t.focus()}this._closeAllSubMenus(),this._closeAllSubMenuToggles()}return this},s.prototype.handleFocus=function(t){if(!this.navOpened)return this;if(!this.settings.closeNavOnLastTab)return this;const e=this.$element.querySelectorAll(["a[href]","area[href]","input:not([disabled])","select:not([disabled])","textarea:not([disabled])","button:not([disabled])"]),s=e[e.length-1];return 9!==t.keyCode||t.shiftKey||t.target!==s||(t.preventDefault(),this._handleNav(t)),this},s.prototype.handleDocClick=function(t){return t.target.closest('[data-meom-nav="navigation"]')||(this._closeAllSubMenus(),this._closeAllSubMenuToggles()),this},s.prototype.closeAllSubMenus=function(){return document.querySelectorAll(`${this.settings.subNavClass}.${this.settings.toggleSubNavClassValue}`).forEach((function(t){this._setSubMenu(t)}),this),this},s.prototype.setSubMenu=function(e,s){return e?(e.classList.contains(this.settings.toggleSubNavClassValue)?(e.classList.remove(this.settings.toggleSubNavClassValue),this.settings.onCloseSubNav&&"function"==typeof this.settings.onCloseSubNav&&this.settings.onCloseSubNav(this.$element,this.$toggle,e,s)):(e.classList.add(this.settings.toggleSubNavClassValue),this.settings.animateSubNav&&t(e,this.settings.animateSubNavClass),this.settings.onOpenSubNav&&"function"==typeof this.settings.onOpenSubNav&&this.settings.onOpenSubNav(this.$element,this.$toggle,e,s)),this):this},s.prototype.closeAllSubMenuToggles=function(){document.querySelectorAll('[data-meom-nav="sub-toggle"][aria-expanded="true"]').forEach((function(t){e(t,"expanded")}));return document.querySelectorAll('[data-meom-nav="sub-sub-toggle"][aria-expanded="true"]').forEach((function(t){e(t,"expanded")})),this};export{s as Navigation,t as animate,e as updateAria}; |
@@ -1,2 +0,2 @@ | ||
/*! navigation 1.0.3 — © MEOM */ | ||
/*! navigation 1.0.4 — © MEOM */ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Navigation={})}(this,(function(t){"use strict";function e(t,e,s){t&&e&&(t.classList.add(e),t.addEventListener("animationend",(function n(){t.classList.remove(e),s&&t.classList.remove(s),t.removeEventListener("animationend",n,!1)}),!1))}function s(t,e){if(void 0===t||0>=e.length)return;const s="true"===t.getAttribute(`aria-${e}`)?"false":"true";t.setAttribute(`aria-${e}`,s)}function n(t,e,s={}){this._handleNav=this.handleNav.bind(this),this._handleSubNav=this.handleSubNav.bind(this),this._handleCloseNav=this.handleCloseNav.bind(this),this._handleCloseSubNav=this.handleCloseSubNav.bind(this),this._closeAllSubMenus=this.closeAllSubMenus.bind(this),this._setSubMenu=this.setSubMenu.bind(this),this._closeAllSubMenuToggles=this.closeAllSubMenuToggles.bind(this),this._handleDocClick=this.handleDocClick.bind(this),this._handleFocus=this.handleFocus.bind(this);const n={action:"click",subNavAnchors:".js-site-nav-items > .menu-item-has-children > a",subSubNavAnchors:".js-site-nav-items .sub-menu > .menu-item-has-children > a",toggleNavClass:!0,toggleNavClassValue:"is-opened",toggleSubNavClassValue:"is-opened",closeNavOnEscKey:!0,closeNavOnLastTab:!1,subNavClass:".sub-menu",subToggleButtonClasses:"",subSubToggleButtonClasses:"",animateSubNav:!1,animateSubNavClass:"",visuallyHiddenClass:"screen-reader-text",expandChildNavText:"Sub menu",dropDownIcon:'<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>',onCreate:null,onOpenNav:null,onCloseNav:null,onOpenSubNav:null,onCloseSubNav:null,...s};this.$element=t,this.$toggle=e,this.settings=n,this.navOpened=!1,this.$subNavs=this.$element.querySelectorAll(this.settings.subNavAnchors),this.$subSubNavs=this.$element.querySelectorAll(this.settings.subSubNavAnchors),this.create()}n.prototype.create=function(){return this.$toggle.setAttribute("aria-expanded","false"),this.$element.setAttribute("data-meom-nav","navigation"),this.$subNavs.forEach((function(t,e){"click"===this.settings.action&&t.setAttribute("hidden","");const s=document.createElement("button");s.setAttribute("data-meom-nav","sub-toggle"),s.setAttribute("aria-expanded","false"),s.className=`${this.settings.subToggleButtonClasses}`,s.type="button","click"===this.settings.action&&(s.innerHTML=`${t.textContent}${this.settings.dropDownIcon}`),"hover"===this.settings.action&&(s.innerHTML=`<span class="${this.settings.visuallyHiddenClass}">${this.settings.expandChildNavText}</span>${this.settings.dropDownIcon}`),t.after(s)}),this),this.$subSubNavs.forEach((function(t,e){const s=document.createElement("button");s.setAttribute("data-meom-nav","sub-sub-toggle"),s.setAttribute("aria-expanded","false"),s.setAttribute("aria-controls",`sub-sub-menu-${e}`),t.nextElementSibling&&(t.nextElementSibling.id=`sub-sub-menu-${e}`),s.className=`${this.settings.subSubToggleButtonClasses}`,s.type="button",s.innerHTML=`<span class="${this.settings.visuallyHiddenClass}">${this.settings.expandChildNavText}</span>${this.settings.dropDownIcon}`,t.after(s)}),this),this.$toggle.addEventListener("click",this._handleNav,!1),this.$element.addEventListener("click",this._handleSubNav,!1),document.addEventListener("keydown",this._handleCloseNav,!1),this.$element.addEventListener("keydown",this._handleCloseSubNav,!1),this.$element.addEventListener("keydown",this._handleFocus,!1),document.addEventListener("click",this._handleDocClick,!1),this.settings.onCreate&&"function"==typeof this.settings.onCreate&&this.settings.onCreate(this.$element,this.$toggle),this},n.prototype.handleNav=function(t){return this.navOpened?(s(this.$toggle,"expanded"),this.settings.toggleNavClass&&this.$element.classList.remove(this.settings.toggleNavClassValue),this.$toggle&&this.$toggle.focus(),this.navOpened=!1,this._closeAllSubMenus(),this._closeAllSubMenuToggles(),this.settings.onCloseNav&&"function"==typeof this.settings.onCloseNav&&this.settings.onCloseNav(this.$element,this.$toggle,t)):(s(this.$toggle,"expanded"),this.settings.toggleNavClass&&this.$element.classList.add(this.settings.toggleNavClassValue),this.navOpened=!0,this.settings.onOpenNav&&"function"==typeof this.settings.onOpenNav&&this.settings.onOpenNav(this.$element,this.$toggle,t)),this},n.prototype.handleSubNav=function(t){const e=t.target,n=e.closest('[data-meom-nav="sub-toggle"]'),i=e.closest('[data-meom-nav="sub-sub-toggle"]');return n||i?(e.nextElementSibling.classList.contains(this.settings.toggleSubNavClassValue)||e.matches('[data-meom-nav="sub-sub-toggle"]')||(this._closeAllSubMenus(),this._closeAllSubMenuToggles()),s(e,"expanded"),e.nextElementSibling&&this._setSubMenu(e.nextElementSibling,t),this):this},n.prototype.handleCloseNav=function(t){return this.navOpened&&this.settings.closeNavOnEscKey&&27===t.keyCode&&this._handleNav(t),this},n.prototype.handleCloseSubNav=function(t){const e=document.querySelector(`${this.settings.subNavClass}.${this.settings.toggleSubNavClassValue}`);if(e){const s=e.querySelectorAll(["a[href]","area[href]","input:not([disabled])","select:not([disabled])","textarea:not([disabled])","button:not([disabled])"]),n=s[s.length-1];9!==t.keyCode||t.shiftKey||t.target!==n||(this._closeAllSubMenus(),this._closeAllSubMenuToggles());const i=e.previousElementSibling;i&&9===t.keyCode&&t.shiftKey&&t.target===i&&(this._closeAllSubMenus(),this._closeAllSubMenuToggles())}if(27===t.keyCode){if(t.target.matches('[data-meom-nav="sub-toggle"][aria-expanded="true"]'))return this._handleSubNav(t),this._closeAllSubMenus(),this._closeAllSubMenuToggles(),this;const e=t.target.closest(`${this.settings.subNavClass}.${this.settings.toggleSubNavClassValue}`);if(e){const t=e.previousElementSibling;t&&t.focus()}this._closeAllSubMenus(),this._closeAllSubMenuToggles()}return this},n.prototype.handleFocus=function(t){if(!this.navOpened)return this;if(!this.settings.closeNavOnLastTab)return this;const e=this.$element.querySelectorAll(["a[href]","area[href]","input:not([disabled])","select:not([disabled])","textarea:not([disabled])","button:not([disabled])"]),s=e[e.length-1];return 9!==t.keyCode||t.shiftKey||t.target!==s||(t.preventDefault(),this._handleNav(t)),this},n.prototype.handleDocClick=function(t){return t.target.closest('[data-meom-nav="navigation"]')||(this._closeAllSubMenus(),this._closeAllSubMenuToggles()),this},n.prototype.closeAllSubMenus=function(){return document.querySelectorAll(`${this.settings.subNavClass}.${this.settings.toggleSubNavClassValue}`).forEach((function(t){this._setSubMenu(t)}),this),this},n.prototype.setSubMenu=function(t,s){return t?(t.classList.contains(this.settings.toggleSubNavClassValue)?(t.classList.remove(this.settings.toggleSubNavClassValue),this.settings.onCloseSubNav&&"function"==typeof this.settings.onCloseSubNav&&this.settings.onCloseSubNav(this.$element,this.$toggle,t,s)):(t.classList.add(this.settings.toggleSubNavClassValue),this.settings.animateSubNav&&e(t,this.settings.animateSubNavClass),this.settings.onOpenSubNav&&"function"==typeof this.settings.onOpenSubNav&&this.settings.onOpenSubNav(this.$element,this.$toggle,t,s)),this):this},n.prototype.closeAllSubMenuToggles=function(){document.querySelectorAll('[data-meom-nav="sub-toggle"][aria-expanded="true"]').forEach((function(t){s(t,"expanded")}));return document.querySelectorAll('[data-meom-nav="sub-sub-toggle"][aria-expanded="true"]').forEach((function(t){s(t,"expanded")})),this},t.Navigation=n,t.animate=e,t.updateAria=s,Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "@meom/navigation", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "MEOM navigation", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/MEOM/navigation", |
@@ -18,2 +18,3 @@ # MEOM navigation | ||
- [Language nav as dropdown](https://meom.github.io/navigation/demo/lang-button.html). | ||
- [buttons as dropdown](https://meom.github.io/navigation/demo/buttons.html). Like on previous example we can write `<button data-meom-nav="sub-toggle" aria-expanded="false" type="button">` element directly in the markup. | ||
@@ -75,6 +76,6 @@ ## Usage | ||
<ul class="sub-menu"> | ||
<li><a href="#">Contact us</a></li> | ||
<li><a href="#">Company values</a></li> | ||
<li><a href="#">Staff</a></li> | ||
<li><a href="#">More info</a></li> | ||
<li><a href="#">Contact us</a></li> | ||
<li><a href="#">Company values</a></li> | ||
<li><a href="#">Staff</a></li> | ||
<li><a href="#">More info</a></li> | ||
</ul> | ||
@@ -85,12 +86,12 @@ </li> | ||
<ul class="sub-menu"> | ||
<li><a href="#">Design</a></li> | ||
<li><a href="#">WordPress</a></li> | ||
<li class="menu-item-has-children"><a href="#">Hosting</a> | ||
<ul class="sub-menu"> | ||
<li><a href="#">Premium</a></li> | ||
<li><a href="#">Even better</a></li> | ||
<li><a href="#">The fastest</a> | ||
</ul> | ||
</li> | ||
<li><a href="#">Helping</a></li> | ||
<li><a href="#">Design</a></li> | ||
<li><a href="#">WordPress</a></li> | ||
<li class="menu-item-has-children"><a href="#">Hosting</a> | ||
<ul class="sub-menu"> | ||
<li><a href="#">Premium</a></li> | ||
<li><a href="#">Even better</a></li> | ||
<li><a href="#">The fastest</a> | ||
</ul> | ||
</li> | ||
<li><a href="#">Helping</a></li> | ||
</ul> | ||
@@ -120,2 +121,51 @@ </li> | ||
### Markup with your own `<button>` elements | ||
You can use `<button data-meom-nav="sub-toggle" aria-expanded="false" type="button">` for dropdowns elements directly in the markup without top level anchors. Note the mandatory attributes: | ||
- `data-meom-nav="sub-toggle"` | ||
- `aria-expanded="false"` | ||
- `type="button"` | ||
Markup would look like this: | ||
```html | ||
<nav class="site-nav js-site-nav"> | ||
<button | ||
class="site-nav__toggle js-site-nav-toggle" aria-controls="site-nav__items" | ||
aria-expanded="false" | ||
> | ||
Menu | ||
</button> | ||
<ul class="site-nav__items js-site-nav-items" id="site-nav__items"> | ||
<li><a href="#">Home</a></li> | ||
<li class="menu-item-has-children"> | ||
<button class="site-nav__sub-toggle" data-meom-nav="sub-toggle" aria-expanded="false" type="button">About</button> | ||
<ul class="sub-menu"> | ||
<li><a href="#">Contact us</a></li> | ||
<li><a href="#">Company values</a></li> | ||
<li><a href="#">Staff</a></li> | ||
<li><a href="#">More info</a></li> | ||
</ul> | ||
</li> | ||
<li class="menu-item-has-children"> | ||
<button class="site-nav__sub-toggle" data-meom-nav="sub-toggle" aria-expanded="false" type="button">Services</button> | ||
<ul class="sub-menu"> | ||
<li><a href="#">Design</a></li> | ||
<li><a href="#">WordPress</a></li> | ||
<li class="menu-item-has-children"><a href="#">Hosting</a> | ||
<ul class="sub-menu"> | ||
<li><a href="#">Premium</a></li> | ||
<li><a href="#">Even better</a></li> | ||
<li><a href="#">The fastest</a> | ||
</ul> | ||
</li> | ||
<li><a href="#">Helping</a></li> | ||
</ul> | ||
</li> | ||
<li><a href="#">Blog</a></li> | ||
</ul> | ||
</nav> | ||
``` | ||
## Issues opening and closing the sub menus? | ||
@@ -122,0 +172,0 @@ First try adding `pointer-events: none;` CSS to SVG icon inside the sub menu toggles. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
407689
30
384