-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharia-menuitem.html
382 lines (371 loc) · 8.88 KB
/
aria-menuitem.html
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<template id="ariamenuitem">
<div role="menuitem" tabindex="0" aria-owns="nextmenu">
<content select="label"></content>
</div>
<span id="nextmenu">
<content select="aria-menu,aria-menuitem"></content>
</span>
<style>
* {
box-sizing: border-box;
}
*:before, *:after {
box-sizing: inherit;
}
div {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
line-height:3.236em;
float: left;
width:100%;
background: #999999;
text-align: center;
color:#fff;
display:inline-block;
}
span {
display: none;
width: 100%;
}
div.open {
padding-bottom:0;
}
div:focus {
outline: none;
border: 2px dotted black;
}
div:hover,
div:focus {
background: #CCCCCC;
color: #000000;
}
div.open + span {
display:block;
position:absolute;
}
</style>
</template>
<script>
(function() {
function getElementCoordinates(node) {
var coords = {
"top": 0, "right": 0, "bottom": 0, "left": 0, "width": 0, "height": 0
},
xOffset, yOffset, rect;
if (node) {
xOffset = window.scrollX;
yOffset = window.scrollY;
rect = node.getBoundingClientRect();
coords = {
"top": rect.top + yOffset,
"right": rect.right + xOffset,
"bottom": rect.bottom + yOffset,
"left": rect.left + xOffset,
"width": rect.right - rect.left,
"height": rect.bottom - rect.top
};
}
return coords;
}
var importDoc = document.currentScript.ownerDocument; // importee
var AriaMenuitem = Object.create(HTMLElement.prototype);
/**
* AriaMenuitem Component
*
* createdCallback initializes the shadow DOM and sets up the internal members:
* - div - the DOM component with the role="menuitem"
* - nodes - the distributed nodes containing potential sub-menu
* - span - the DOM component that will contain the sub-menu
* - hasPopup - boolean indicating whether there is a sub-menu
* - isOpen - boolean tracking the open state
*
* It also registers the event handlers for:
* - keydown - manage the opening of sub-menu by keyboard, tabbing off, and navigation within siblings if in a menubar (todo: move to AriaMenubar?)
* - click - if clicked and there is a sub-menu, toggle it, otherwise trigger a click on the parentNode
* - aria-menuclose - the sub-menu was closed
*
* @class aria-menuitem
* @constructor
*/
AriaMenuitem.createdCallback = function() {
this.hasPopup = false;
var shadowRoot = this.createShadowRoot();
var template = importDoc.querySelector('#ariamenuitem');
var clone = document.importNode(template.content, true);
shadowRoot.appendChild(clone);
};
function handleClick(e) {
if (this.hasPopup) {
if (this.isOpen) {
this.close(true);
} else {
this.open()
}
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
} else {
this.div.tabIndex = 0;
this.div.focus();
this.parentNode.dispatchEvent(new Event('click'));
}
this._closeOthers();
}
function handleMenuClose(e) {
// listen for the close of a child
this.close(true);
e.stopPropagation();
}
function handleKeydown(e) {
var which = e.which || e.keyCode;
var handled = false;
if (e.ctrlKey || e.altKey || e.metaKey) {
return;
}
switch(which) {
case 9: // tab
this.parentNode.dispatchEvent(new Event(this.ARIA_LOST_FOCUS));
return;
case 37: // left
if (!e.shiftKey && this.horizontal) {
this._focusPrev();
handled = true;
}
break;
case 39: // right
if (!e.shiftKey && this.horizontal) {
this._focusNext();
handled = true;
}
break;
case 40: // down
case 13: // ENTER
if (!e.shiftKey) {
if (this.horizontal && this.hasPopup) {
this.dispatchEvent(new Event('click'));
handled = true;
} else if (which === 13) {
// emulate click
this.dispatchEvent(new Event('click'));
handled = true;
}
break;
}
}
if (handled) {
e.stopPropagation();
e.preventDefault();
}
}
AriaMenuitem.attachedCallback = function () {
var shadowRoot = this.shadowRoot;
this.div = shadowRoot.querySelector('div');
this.span = shadowRoot.querySelector('span');
this.div.style.width = this.getAttribute('width');
// Set the span to the same width as ourselves
// because the absolute positioning picks
// up on the width of the whole document and we want to be
// responsive
var coords = getElementCoordinates(this.div);
this.span.style.width = coords.width + 'px';
if (!this.previousElementSibling &&
(this.parentNode.nodeName !== 'ARIA-MENU' || this.parentNode.hasAttribute('popup'))) {
// only make this element focusable if it is the topmost first element in the tree
this.div.tabIndex = 0;
} else {
this.div.tabIndex = -1;
}
if (this.parentNode.nodeName !== 'ARIA-MENU') {
this.horizontal = true;
}
// look for sub-menus
var content = shadowRoot.querySelector('#nextmenu').querySelector('content');
this.nodes = content.getDistributedNodes();
for (var i = 0; i < this.nodes.length; i++) {
if (this.nodes[i].nodeName === 'ARIA-MENU') {
this.hasPopup = true;
break;
}
}
if (this.hasPopup) {
this.div.setAttribute('aria-haspopup', true);
this.div.setAttribute('aria-expanded', false);
}
// handle click
this.isOpen = false;
this.addEventListener('click', handleClick, false);
this.addEventListener('aria-menuclose', handleMenuClose, false);
this.addEventListener('keydown', handleKeydown, false);
};
AriaMenuitem.detachedCallback = function () {
this.removeEventListener('click', handleClick, false);
this.removeEventListener('aria-menuclose', handleMenuClose, false);
this.removeEventListener('keydown', handleKeydown, false);
};
/**
* Fired when focus will be lost
*
* @event aria-lostfocus
*/
AriaMenuitem.ARIA_LOST_FOCUS = 'aria-lostfocus';
/**
* Closes peer menuitems
*
* @method _closeOthers
* @private
* @return undefined
*/
AriaMenuitem._closeOthers = function () {
var prev = this.previousElementSibling;
while (prev) {
prev.close && prev.close();
prev = prev.previousElementSibling;
}
var next = this.nextElementSibling;
while (next) {
next.close && next.close();
next = next.nextElementSibling;
}
};
/**
* Opens the menuitem if it has a sub-menu, otherwise does nothing
*
* @method open
* @return undefined
*/
AriaMenuitem.open = function () {
var coords;
if (this.hasPopup) {
this.isOpen = true;
this.div.classList.add('open');
this.div.tabIndex = -1;
this.nodes[0].open();
coords = getElementCoordinates(this.div);
this.span.style.left = coords.left + 'px';
this.span.style.top = coords.top + coords.height + 'px';
}
};
/**
* focuses the menu item and closes peer items
*
* @method _takeFocus
* @private
* @return undefined
*/
AriaMenuitem._takeFocus = function () {
this.div.tabIndex = 0;
this.div.focus();
this._closeOthers();
};
/**
* focuses the menu item
*
* @method focus
* @return undefined
*/
AriaMenuitem.focus = AriaMenuitem._takeFocus;
/**
* closes the menu item
*
* @method close
* @return undefined
*/
AriaMenuitem.close = function (focus) {
if (this.hasPopup) {
this.isOpen = false;
this.div.classList.remove('open');
if (focus) {
this.div.tabIndex = 0;
this.div.focus();
} else {
this.div.tabIndex = -1;
}
}
};
/**
* focuses the next menu item and wraps around at the limit
*
* @method _focusNext
* @private
* @return undefined
*/
AriaMenuitem._focusNext = function () {
var next = this.nextElementSibling;
while (next) {
if (next._takeFocus) {
next._takeFocus();
this.div.tabIndex = -1;
return;
}
next = next.nextElementSibling;
}
this._focusFirst();
};
/**
* sets the tabindex to -1 so that the item is no longer naturally focusable
*
* @method removeFocus
* @return undefined
*/
AriaMenuitem.removeFocus = function () {
this.div.tabIndex = -1;
};
/**
* focuses the first menu item
*
* @method _focusFirst
* @private
* @return undefined
*/
AriaMenuitem._focusFirst = function () {
var prev = this;
var first;
while (prev) {
if (prev._takeFocus) {
first = prev;
}
prev = prev.previousElementSibling;
}
first._takeFocus();
};
/**
* focuses the previous menu item and wraps around at the limit
*
* @method _focusPrev
* @private
* @return undefined
*/
AriaMenuitem._focusPrev = function () {
var prev = this.previousElementSibling;
while (prev) {
if (prev._takeFocus) {
prev._takeFocus();
this.div.tabIndex = -1;
return;
}
prev = prev.previousElementSibling;
}
this._focusLast();
};
/**
* focuses the last menu item
*
* @method _focusLast
* @private
* @return undefined
*/
AriaMenuitem._focusLast = function () {
var next = this;
var last;
while (next) {
if (next._takeFocus) {
last = next;
}
next = next.nextElementSibling;
}
last._takeFocus();
};
window.AriaMenuItem = document.registerElement('aria-menuitem', {
prototype: AriaMenuitem
});
}());
</script>