@@ -78,17 +78,25 @@ import Headroom from 'headroom.js';
7878 } ) ;
7979
8080 const menuTitles = document . querySelectorAll ( '.fl-menu-title' ) ;
81+ let focusedMenu = null ;
8182
8283 // keyboard is being used
8384 menuTitles . forEach ( function ( title ) {
85+ // when focusing a menu title, close all menus
86+ title . addEventListener ( 'focus' , function ( ) {
87+ menuCategories . forEach ( function ( category ) {
88+ if ( category . classList . contains ( 'is-active' ) ) {
89+ focusedMenu = category ;
90+ }
91+ category . classList . remove ( 'is-active' ) ;
92+ } ) ;
93+ } ) ;
94+
95+ // when leaving the last link of a menu, close all menus
8496 const menuLinks = title
8597 . closest ( '.fl-menu-category' )
8698 . querySelectorAll ( 'a' ) ;
8799
88- // focus events are no good here as we mix in a click handler below
89- // which is also triggered after our focus would run
90-
91- // when leaving the last link of a menu, close all menus
92100 menuLinks [ menuLinks . length - 1 ] . addEventListener (
93101 'keydown' ,
94102 function ( event ) {
@@ -100,21 +108,19 @@ import Headroom from 'headroom.js';
100108 }
101109 ) ;
102110
103- // when leaving the first link of a menu, close all menus
104- menuLinks [ 0 ] . addEventListener ( 'keydown' , function ( event ) {
105- if ( event . key === 'Tab' && event . shiftKey ) {
106- menuCategories . forEach ( function ( category ) {
107- category . classList . remove ( 'is-active' ) ;
108- } ) ;
109- }
110- } ) ;
111-
112111 // when clicking or pressing enter, toggle the menu
113112 title . addEventListener ( 'click' , function ( event ) {
114113 event . preventDefault ( ) ;
115114
116115 const menuPanel = event . target . closest ( '.fl-menu-category' ) ;
117116
117+ // focus runs first then click. if we click a menu that was closed
118+ // by focus don't open it again immediately
119+ if ( focusedMenu === menuPanel ) {
120+ focusedMenu = null ;
121+ return ;
122+ }
123+
118124 if ( menuPanel . classList . contains ( 'is-active' ) ) {
119125 menuPanel . classList . remove ( 'is-active' ) ;
120126 } else {
0 commit comments