-
Notifications
You must be signed in to change notification settings - Fork 13
/
menu.js
168 lines (121 loc) · 5.22 KB
/
menu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
An accessible menu for WordPress
https://github.com/argenteum/accessible-nav-wp
Licensed GPL v.2 (http://www.gnu.org/licenses/gpl-2.0.html)
This work derived from:
https://github.com/WordPress/twentysixteen (GPL v.2)
https://github.com/wpaccessibility/a11ythemepatterns/tree/master/menu-keyboard-arrow-nav (GPL v.2)
*/
(function($) {
var menuContainer = $('.menu-container');
var menuToggle = menuContainer.find( '.menu-button' );
var siteHeaderMenu = menuContainer.find( '#site-header-menu' );
var siteNavigation = menuContainer.find( '#site-navigation' );
// If you are using WordPress, and do not need to localise your script, or if you are not using WordPress, then uncomment the next line
// var screenReaderText = {"expand":"Expand child menu","collapse":"Collapse child menu"};
var dropdownToggle = $('<button />', {'class': 'dropdown-toggle','aria-expanded': false})
.append($('<span />', {'class': 'screen-readers',text: screenReaderText.expand}));
// Toggles the menu button
(function() {
if (!menuToggle.length) {
return;
}
menuToggle.add(siteNavigation).attr('aria-expanded', 'false');
menuToggle.on('click', function() {
$(this).add(siteHeaderMenu).toggleClass('toggled-on');
// jscs:disable
$(this).add( siteNavigation )
.attr('aria-expanded', $( this )
.add( siteNavigation ).attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
// jscs:enable
} );
} )();
// Adds the dropdown toggle button
$('.menu-item-has-children > a').after(dropdownToggle);
// Adds aria attribute
siteHeaderMenu.find( '.menu-item-has-children' ).attr( 'aria-haspopup', 'true' );
// Toggles the sub-menu when dropdown toggle button clicked
siteHeaderMenu.find( '.dropdown-toggle' ).click( function(e) {
screenReaderSpan = $(this).find( '.screen-readers' );
e.preventDefault();
$(this).toggleClass( 'toggled-on' );
$(this).nextAll( '.sub-menu' ).toggleClass( 'toggled-on' );
// jscs:disable
$(this).attr( 'aria-expanded', $(this).attr( 'aria-expanded' ) === 'false'
? 'true' : 'false' );
// jscs:enable
screenReaderSpan.text( screenReaderSpan.text() ===
screenReaderText.expand ? screenReaderText.collapse :
screenReaderText.expand );
});
// Adds a class to sub-menus for styling
$('.sub-menu .menu-item-has-children').parent('.sub-menu').addClass('has-sub-menu');
// Keyboard navigation
$('.menu-item a, button.dropdown-toggle').on('keydown', function(e) {
if ([37,38,39,40].indexOf(e.keyCode) == -1) {
return;
}
switch(e.keyCode) {
case 37: // left key
e.preventDefault();
e.stopPropagation();
if ($(this).hasClass('dropdown-toggle')){
$(this).prev('a').focus();
}
else {
if ($(this).parent().prev().children('button.dropdown-toggle').length) {
$(this).parent().prev().children('button.dropdown-toggle').focus();
}
else {
$(this).parent().prev().children('a').focus();
}
}
if ($(this).is('ul ul ul.sub-menu.toggled-on li:first-child a')) {
$(this).parents('ul.sub-menu.toggled-on li').children('button.dropdown-toggle').focus();
}
break;
case 39: // right key
e.preventDefault();
e.stopPropagation();
if($(this).next('button.dropdown-toggle').length) {
$(this).next('button.dropdown-toggle').focus();
}
else {
$(this).parent().next().children('a').focus();
}
if ($(this).is('ul.sub-menu .dropdown-toggle.toggled-on')){
$(this).parent().find('ul.sub-menu li:first-child a').focus();
}
break;
case 40: // down key
e.preventDefault();
e.stopPropagation();
if($(this).next().length){
$(this).next().find('li:first-child a').first().focus();
}
else {
$(this).parent().next().children('a').focus();
}
if (($(this).is('ul.sub-menu a')) && ($(this).next('button.dropdown-toggle').length)) {
$(this).parent().next().children('a').focus();
}
if (($(this).is('ul.sub-menu .dropdown-toggle')) && ($(this).parent().next().children('.dropdown-toggle').length)) {
$(this).parent().next().children('.dropdown-toggle').focus();
}
break;
case 38: // up key
e.preventDefault();
e.stopPropagation();
if($(this).parent().prev().length){
$(this).parent().prev().children('a').focus();
}
else {
$(this).parents('ul').first().prev('.dropdown-toggle.toggled-on').focus();
}
if (($(this).is('ul.sub-menu .dropdown-toggle')) && ($(this).parent().prev().children('.dropdown-toggle').length)) {
$(this).parent().prev().children('.dropdown-toggle').focus();
}
break;
}
});
})(jQuery);