-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
71 lines (57 loc) · 1.64 KB
/
index.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
(function ( w, doc, undefined ) {
/**
* ARIA Buttons
*
* Author: Scott O'Hara
* Version: 2.0.0
* License: https://github.com/scottaohara/aria_buttons/blob/master/LICENSE
*/
const ariaButton = function ( inst, options ) {
const el = inst;
let isToggle;
/**
* Initialize the button instance
*/
const init = function () {
setupButton();
if ( isToggle ) {
el.addEventListener('click', toggleEvent);
}
el.addEventListener('keydown', keyEvents);
};
const setupButton = function () {
if ( !el.hasAttribute('tabindex') && el.getAttribute('aria-disabled') !== 'true' ) {
el.tabIndex = 0;
}
if ( el.hasAttribute('tabindex') && el.getAttribute('aria-disabled') === 'true' ) {
el.removeAttribute('tabindex');
}
if ( el.hasAttribute('aria-pressed') ) isToggle = true;
/**
* If an element started off as a <a href...> remove the href attribute.
* Buttons should not return a context menu for links, if right clicked.
*/
el.removeAttribute('href');
};
const toggleEvent = function ( e ) {
if ( isToggle ) {
el.setAttribute('aria-pressed', el.getAttribute('aria-pressed') === 'true' ? 'false' : 'true');
}
};
const keyEvents = function ( e ) {
const keyCode = e.keyCode || e.which;
switch ( keyCode ) {
case 32:
case 13:
e.preventDefault();
this.click();
break;
default:
break;
}
}
init.call( this );
return this;
}; // ariaButton()
w.ariaButton = ariaButton;
})( window, document );