Skip to content

Commit 34d644a

Browse files
MelSumnermansona
authored andcommitted
added removeEventListener
1 parent 7721e2d commit 34d644a

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,24 @@ export default Component.extend({
2828
if (this.isDropdownOpen) {
2929
// once it's open, let's make sure it can do some things
3030
schedule('afterRender', this, function() {
31-
// Define startup states
32-
// Dropdown list
33-
let dropdownList = this.element.querySelector('.navbar-dropdown-list');
3431

35-
// Identify / set focus on the first item in the dropdown list automatically
36-
let firstFocusable = this.element.querySelector('.navbar-dropdown-list li:first-of-type a');
37-
firstFocusable.focus();
3832

39-
// Need some event listeners for keyboard events
33+
// move focus to the first item in the dropdown
34+
let dropdownList = this.element.querySelector('.navbar-dropdown-list');
35+
this.handleFirstElementFocus();
36+
37+
// add event listeners for keyboard events
4038
dropdownList.addEventListener('keydown', event => {
4139

42-
// Listen for ESC key to exit
40+
// ESC key should close the dropdown and return focus to the toggle
4341
if (event.keyCode === 27 && this.isDropdownOpen) {
4442
this.closeDropdown();
43+
this.returnFocus();
44+
45+
// if focus leaves the open dropdown, close it (without trying to otherwise control focus)
4546
} else if (this.isDropdownOpen) {
46-
// if focus leaves the open dropdown, close it (without trying to otherwise control focus)
4747
this.handleBlur();
48+
4849
} else {
4950
return;
5051
}
@@ -53,9 +54,19 @@ export default Component.extend({
5354
}
5455
}
5556
},
57+
5658
closeDropdown() {
5759
// set the isDropdownOpen to false, which will make the dropdown go away
5860
this.set('isDropdownOpen', false);
61+
},
62+
63+
handleFirstElementFocus() {
64+
// Identify / set focus on the first item in the dropdown list automatically
65+
let firstFocusable = this.element.querySelector('.navbar-dropdown-list li:first-of-type a');
66+
firstFocusable.focus();
67+
},
68+
69+
returnFocus() {
5970
// after that rendering bit happens, we need to return the focus to the trigger
6071
schedule('afterRender', this, function() {
6172
let dropdownTrigger = this.element.querySelector('.navbar-list-item-dropdown-toggle');
@@ -72,6 +83,10 @@ export default Component.extend({
7283
this.set('isDropdownOpen', false);
7384
}
7485
});
86+
},
87+
88+
willDestroyElement() {
89+
document.removeEventListener('keydown', this.triggerDropdown);
7590
}
7691

7792
});

0 commit comments

Comments
 (0)