Skip to content

Commit 9c6a753

Browse files
MelSumnermansona
authored andcommitted
extracted keypress to its own function
1 parent b1cea46 commit 9c6a753

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

addon/components/es-navbar/link/component.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,16 @@ export default Component.extend({
2323
actions: {
2424
toggleDropdown() {
2525
this.toggleProperty('isDropdownOpen');
26-
26+
2727
if (this.isDropdownOpen) {
2828
// if it's open, let's make sure it can do some things
2929
schedule('afterRender', this, function() {
3030

3131
// move focus to the first item in the dropdown
32-
this.handleFirstElementFocus();
33-
34-
// add event listeners
35-
let dropdownList = this.element.querySelector('.navbar-dropdown-list');
36-
37-
//TODO ...for click events outside this dropdown
38-
39-
//...for certain keypress events
40-
dropdownList.addEventListener('keydown', event => {
41-
42-
// ESC key should close the dropdown and return focus to the toggle
43-
if (event.keyCode === 27 && this.isDropdownOpen) {
44-
this.closeDropdown();
45-
this.returnFocus();
32+
this.processFirstElementFocus();
33+
this.processKeyPress();
4634

47-
// if focus leaves the open dropdown via keypress, close it (without trying to otherwise control focus)
48-
} else if (this.isDropdownOpen) {
49-
this.handleBlur();
5035

51-
} else {
52-
return;
53-
}
54-
});
5536
});
5637
}
5738
}
@@ -65,10 +46,10 @@ export default Component.extend({
6546
openDropdown() { //might not need this
6647
// open the dropdown and set the focus to the first item inside
6748
this.set('isDropdownOpen', true);
68-
this.handleFirstElementFocus();
49+
this.processFirstElementFocus();
6950
},
7051

71-
handleBlur() { //TODO is this working?
52+
processBlur() { //TODO is this working?
7253
next(this, function() {
7354
let subItems = Array.from(this.element.querySelectorAll('.navbar-dropdown-list li'));
7455
let focused = subItems.find(item => document.activeElement === item.querySelector('a'));
@@ -80,12 +61,38 @@ export default Component.extend({
8061
});
8162
},
8263

83-
handleFirstElementFocus() {
64+
processClick() {
65+
66+
},
67+
68+
processFirstElementFocus() {
8469
// Identify the first item in the dropdown list & set focus on it
8570
let firstFocusable = this.element.querySelector('.navbar-dropdown-list li:first-of-type a');
8671
firstFocusable.focus();
8772
},
8873

74+
processKeyPress() {
75+
// add event listeners
76+
let dropdownList = this.element.querySelector('.navbar-dropdown-list');
77+
78+
//...for certain keypress events
79+
dropdownList.addEventListener('keydown', event => {
80+
81+
// ESC key should close the dropdown and return focus to the toggle
82+
if (event.keyCode === 27 && this.isDropdownOpen) {
83+
this.closeDropdown();
84+
this.returnFocus();
85+
86+
// if focus leaves the open dropdown via keypress, close it (without trying to otherwise control focus)
87+
} else if (this.isDropdownOpen) {
88+
this.processBlur();
89+
90+
} else {
91+
return;
92+
}
93+
});
94+
},
95+
8996
returnFocus() {
9097
// after that rendering bit happens, we need to return the focus to the trigger
9198
schedule('afterRender', this, function() {

0 commit comments

Comments
 (0)