|
1 | 1 | /** |
2 | | - * Clicky Menus v1.2.0 |
| 2 | + * Clicky Menus v1.3.0 |
3 | 3 | */ |
4 | 4 |
|
5 | 5 | ( function() { |
|
9 | 9 | // DOM element(s) |
10 | 10 | const container = menu.parentElement; |
11 | 11 | let currentMenuItem, |
12 | | - i, |
| 12 | + i = 0, |
13 | 13 | len; |
14 | 14 |
|
15 | 15 | this.init = function() { |
|
62 | 62 | } |
63 | 63 |
|
64 | 64 | function closeOnEscKey( e ) { |
65 | | - if ( 27 === e.keyCode ) { |
| 65 | + if ( 27 === e.keyCode ) { |
66 | 66 | // we're in a submenu item |
67 | 67 | if ( null !== e.target.closest( 'ul[aria-hidden="false"]' ) ) { |
68 | 68 | currentMenuItem.focus(); |
69 | 69 | toggleSubmenu( currentMenuItem ); |
70 | | - |
| 70 | + e.stopPropagation(); |
71 | 71 | // we're on a parent item |
72 | 72 | } else if ( 'true' === e.target.getAttribute( 'aria-expanded' ) ) { |
73 | 73 | toggleSubmenu( currentMenuItem ); |
| 74 | + e.stopPropagation(); |
74 | 75 | } |
75 | 76 | } |
76 | 77 | } |
|
116 | 117 | */ |
117 | 118 | function convertLinkToButton( menuItem ) { |
118 | 119 | const link = menuItem.getElementsByTagName( 'a' )[ 0 ], |
119 | | - linkHTML = link.innerHTML, |
120 | | - linkAtts = link.attributes, |
121 | | - button = document.createElement( 'button' ); |
| 120 | + linkHTML = link.innerHTML, |
| 121 | + linkAtts = link.attributes, |
| 122 | + button = document.createElement( 'button' ); |
122 | 123 |
|
123 | 124 | if ( null !== link ) { |
124 | 125 | // copy button attributes and content from link |
125 | 126 | button.innerHTML = linkHTML.trim(); |
126 | | - for ( i = 0, len = linkAtts.length; i < len; i++ ) { |
| 127 | + for ( len = linkAtts.length; i < len; i++ ) { |
127 | 128 | const attr = linkAtts[ i ]; |
128 | 129 | if ( 'href' !== attr.name ) { |
129 | 130 | button.setAttribute( attr.name, attr.value ); |
|
141 | 142 |
|
142 | 143 | let id; |
143 | 144 | if ( null === submenuId ) { |
144 | | - id = button.textContent.trim().replace( /\s+/g, '-' ).toLowerCase() + '-submenu'; |
| 145 | + id = button.textContent.trim().replace( /\s+/g, '-' ).replace(/^[^a-zA-Z]+|[^\w:.-]+/g, "").toLowerCase() + `-submenu-${i}`; |
| 146 | + i++; |
145 | 147 | } else { |
146 | | - id = submenuId + '-submenu'; |
| 148 | + id = `${submenuId}-submenu-${i}`; |
| 149 | + i++; |
147 | 150 | } |
148 | 151 |
|
149 | 152 | // set button ARIA |
|
165 | 168 | clickyMenu.init(); |
166 | 169 | } ); |
167 | 170 | } ); |
| 171 | + |
| 172 | + function dispatchMenuClose(e) { |
| 173 | + const menuId = e.currentTarget.getAttribute('data-clicky-menus-close'); |
| 174 | + const menu = document.getElementById( menuId ); |
| 175 | + if( menu ) { |
| 176 | + menu.dispatchEvent( new Event( 'clickyMenusClose' ) ); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + const menuClosers = document.querySelectorAll( '[data-clicky-menus-close]' ); |
| 181 | + if( menuClosers ) { |
| 182 | + menuClosers.forEach( ( menuCloser ) => { |
| 183 | + menuCloser.addEventListener( 'click', dispatchMenuClose ); |
| 184 | + } ); |
| 185 | + } |
168 | 186 | }() ); |
0 commit comments