Skip to content

Commit a69eac9

Browse files
MelSumnermansona
authored andcommitted
added a click event listener
1 parent 34d644a commit a69eac9

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default Component.extend({
1919
return this.isDropdownOpen ? 'true' : 'false';
2020
}),
2121

22-
navbar: service(), //TODO also do we need this too?
22+
navbar: service(),
2323

2424
actions: {
2525
triggerDropdown() {
@@ -29,11 +29,16 @@ export default Component.extend({
2929
// once it's open, let's make sure it can do some things
3030
schedule('afterRender', this, function() {
3131

32-
3332
// move focus to the first item in the dropdown
3433
let dropdownList = this.element.querySelector('.navbar-dropdown-list');
3534
this.handleFirstElementFocus();
3635

36+
dropdownList.addEventListener('click', event =>{
37+
if (this.isDropdownOpen) {
38+
this.handleBlur();
39+
}
40+
});
41+
3742
// add event listeners for keyboard events
3843
dropdownList.addEventListener('keydown', event => {
3944

@@ -42,7 +47,7 @@ export default Component.extend({
4247
this.closeDropdown();
4348
this.returnFocus();
4449

45-
// if focus leaves the open dropdown, close it (without trying to otherwise control focus)
50+
// if focus leaves the open dropdown via keypress, close it (without trying to otherwise control focus)
4651
} else if (this.isDropdownOpen) {
4752
this.handleBlur();
4853

@@ -60,8 +65,21 @@ export default Component.extend({
6065
this.set('isDropdownOpen', false);
6166
},
6267

68+
handleBlur() {
69+
next(this, function() {
70+
let subItems = Array.from(this.element.querySelectorAll('.navbar-dropdown-list li'));
71+
let focused = subItems.find(item => document.activeElement === item.querySelector('a'));
72+
console.log(focused);
73+
74+
//if the dropdown isn't focused, close it
75+
if (!focused) {
76+
this.closeDropdown();
77+
}
78+
});
79+
},
80+
6381
handleFirstElementFocus() {
64-
// Identify / set focus on the first item in the dropdown list automatically
82+
// Identify the first item in the dropdown list & set focus on it
6583
let firstFocusable = this.element.querySelector('.navbar-dropdown-list li:first-of-type a');
6684
firstFocusable.focus();
6785
},
@@ -74,19 +92,8 @@ export default Component.extend({
7492
});
7593
},
7694

77-
handleBlur() {
78-
next(this, function() {
79-
let subItems = Array.from(this.element.querySelectorAll('.navbar-dropdown-list li'));
80-
let focused = subItems.find(item => document.activeElement === item.querySelector('a'));
81-
82-
if (!focused) {
83-
this.set('isDropdownOpen', false);
84-
}
85-
});
86-
},
87-
88-
willDestroyElement() {
95+
willDestroy() {
8996
document.removeEventListener('keydown', this.triggerDropdown);
97+
document.removeEventListener('click', this.triggerDropdown);
9098
}
91-
9299
});

0 commit comments

Comments
 (0)