Skip to content

Commit

Permalink
implement focus events so menu can be tabbed through
Browse files Browse the repository at this point in the history
  • Loading branch information
kshaner committed Mar 22, 2016
1 parent 1c1abc1 commit d1c81a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ It handles:
* Pointer Events
* Mouse Events
* Hover Intent
* Focus Events

If a menu has a top level link and is opened with touch, the menu is opened and the link is not followed. Then if it is touched again while the menu is open, the link followed. This behavior is similar to aria-haspopup.

Expand Down Expand Up @@ -46,3 +47,6 @@ jQuery('#node').dropdown(options);
* Android 4+ (haven't tested with 2.3)
* Edge
* IE8+

### Develop Notes
Use uglifyjs to create minified version: uglifyjs dropdown.js -o dropdown.min.js --mangle --compress
33 changes: 31 additions & 2 deletions dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ window.Dropdown = (function() {
}
};

evts.mouseenter = function(e) {
evts.mouseenter = function() {
if (typeof _this.timeout.leave === 'number') {
window.clearTimeout(_this.timeout.leave);
} else {
Expand All @@ -134,7 +134,7 @@ window.Dropdown = (function() {
}
};

evts.mouseleave = function(e) {
evts.mouseleave = function() {
if (typeof _this.timeout.enter === 'number') {
window.clearTimeout(_this.timeout.enter);
} else {
Expand Down Expand Up @@ -198,6 +198,35 @@ window.Dropdown = (function() {
document.removeEventListener('click', evts.maybeClose);
};

evts.focus = function() {
if (!_this.active) {
_this.open();
}
};

evts.blur = function() {
if (_this.menuLinks.length < 1) {
_this.close();
return;
}

// make this async so we can detect the correct document.activeElement
window.setTimeout(function() {
for(var i = 0; i<_this.menuLinks.length; i++) {
if (document.activeElement === _this.menuLinks[i]) {
return;
}
}
_this.close();
}, 0);
};

// use event capturing to detect event focus and blur because do not bubble
if (this.container.addEventListener) {
this.container.addEventListener('focus', evts.focus, true);
this.container.addEventListener('blur', evts.blur, true);
}

if (window.PointerEvent) {
this.container.addEventListener('pointerenter', evts.pointerenter);
this.container.addEventListener('pointerleave', evts.pointerleave);
Expand Down
17 changes: 1 addition & 16 deletions dropdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1c81a4

Please sign in to comment.