@@ -28,23 +28,24 @@ export default Component.extend({
28
28
if ( this . isDropdownOpen ) {
29
29
// once it's open, let's make sure it can do some things
30
30
schedule ( 'afterRender' , this , function ( ) {
31
- // Define startup states
32
- // Dropdown list
33
- let dropdownList = this . element . querySelector ( '.navbar-dropdown-list' ) ;
34
31
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 ( ) ;
38
32
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
40
38
dropdownList . addEventListener ( 'keydown' , event => {
41
39
42
- // Listen for ESC key to exit
40
+ // ESC key should close the dropdown and return focus to the toggle
43
41
if ( event . keyCode === 27 && this . isDropdownOpen ) {
44
42
this . closeDropdown ( ) ;
43
+ this . returnFocus ( ) ;
44
+
45
+ // if focus leaves the open dropdown, close it (without trying to otherwise control focus)
45
46
} else if ( this . isDropdownOpen ) {
46
- // if focus leaves the open dropdown, close it (without trying to otherwise control focus)
47
47
this . handleBlur ( ) ;
48
+
48
49
} else {
49
50
return ;
50
51
}
@@ -53,9 +54,19 @@ export default Component.extend({
53
54
}
54
55
}
55
56
} ,
57
+
56
58
closeDropdown ( ) {
57
59
// set the isDropdownOpen to false, which will make the dropdown go away
58
60
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 ( ) {
59
70
// after that rendering bit happens, we need to return the focus to the trigger
60
71
schedule ( 'afterRender' , this , function ( ) {
61
72
let dropdownTrigger = this . element . querySelector ( '.navbar-list-item-dropdown-toggle' ) ;
@@ -72,6 +83,10 @@ export default Component.extend({
72
83
this . set ( 'isDropdownOpen' , false ) ;
73
84
}
74
85
} ) ;
86
+ } ,
87
+
88
+ willDestroyElement ( ) {
89
+ document . removeEventListener ( 'keydown' , this . triggerDropdown ) ;
75
90
}
76
91
77
92
} ) ;
0 commit comments