@@ -25,7 +25,7 @@ const MENU_ITEM_WRAPPER_SELECTOR = '[data-menu-item-wrapper="true"]';
2525 * - onClick={handleOnOpen}
2626 * - ref={menuRef}
2727 * - onKeyDown={handleKeyDown}
28- * - tabIndex={0} or {-1} depending on the kind of focusability we want
28+ * - Make sure the element is focusable
2929 * - aria-expanded={isExpanded()} (and use it wherever else needed)
3030 * - for menu items pass onKeyDown={handleKeyDownOpenMenu}
3131 * 2. For the sake of consistent code structure, it is recommended for the core menus (depth 1)
@@ -56,7 +56,6 @@ export default function useMenuNavigation ({
5656} ) {
5757 const menuRef = useRef ( null ) ;
5858 const menuContext = useContext ( MenuRefContext ) ;
59- const [ focusedItem , setFocusedItem ] = useState ( null ) ;
6059
6160 // BFS to find first children with attribute
6261 const findDirectSubitems = useCallback ( ( ) => {
@@ -70,8 +69,8 @@ export default function useMenuNavigation ({
7069 const element = children . shift ( ) ;
7170 if ( element . matches ( MENU_ITEM_SELECTOR ) ) {
7271 // Skip original starting element if we went back to the wrapper
73- if ( ! ( menuRef . current . matches ( MENU_ITEM_WRAPPER_SELECTOR ) &&
74- Array . from ( menuRef . current . children ) . includes ( element ) ) ) {
72+ if ( ! ( root . matches ( MENU_ITEM_WRAPPER_SELECTOR ) &&
73+ Array . from ( root . children ) . includes ( element ) ) ) {
7574 directSubitems . push ( [ element , element ] ) ;
7675 }
7776 continue ;
@@ -109,7 +108,6 @@ export default function useMenuNavigation ({
109108 const focusItem = useCallback ( item => {
110109 if ( item ) {
111110 item . focus ( ) ;
112- setFocusedItem ( item ) ;
113111 }
114112 } , [ ] ) ;
115113
@@ -126,7 +124,6 @@ export default function useMenuNavigation ({
126124 } , [ menuContext , menuRef , depth , defaultIndexOnOpen ] ) ;
127125
128126 const handleOnClose = useCallback ( ( ) => {
129- setFocusedItem ( null ) ;
130127 menuContext . closeMenuByRef ( menuRef ) ;
131128 menuRef ?. current ?. focus ( ) ;
132129 } , [ menuContext , menuRef ] ) ;
@@ -135,25 +132,28 @@ export default function useMenuNavigation ({
135132 const items = findDirectSubitemsFocusable ( ) ;
136133 if ( ! items . length ) return ;
137134
138- const currentIndex = items . indexOf ( focusedItem ) ;
135+ const currentIndex = items . indexOf ( document . activeElement ) ;
139136 const nextIndex = ( currentIndex + direction + items . length ) % items . length ;
140137 focusItem ( items [ nextIndex ] ) ;
141- } , [ focusedItem , menuRef , focusItem ] ) ;
138+ } , [ menuRef , focusItem ] ) ;
142139
143140 const handleKeyDownOpenMenu = useCallback ( e => {
144141 // Logic for vertical menus, will need to change when implementing for horizontal
145142 switch ( e . key ) {
146143 case KEY . ARROW_DOWN :
147144 e . preventDefault ( ) ;
145+ e . stopPropagation ( ) ;
148146 handleMove ( 1 ) ;
149147 break ;
150148 case KEY . ARROW_UP :
151149 e . preventDefault ( ) ;
150+ e . stopPropagation ( ) ;
152151 handleMove ( - 1 ) ;
153152 break ;
154153 case KEY . ESCAPE :
155154 case KEY . ARROW_LEFT :
156155 e . preventDefault ( ) ;
156+ e . stopPropagation ( ) ;
157157 handleOnClose ( ) ;
158158 break ;
159159 case KEY . ENTER :
@@ -162,8 +162,8 @@ export default function useMenuNavigation ({
162162 {
163163 const focusableItems = findDirectSubitemsFocusable ( ) ;
164164 const clickableItems = findDirectSubitemsClickable ( ) ;
165-
166- const index = focusableItems . indexOf ( focusedItem ) ;
165+
166+ const index = focusableItems . indexOf ( document . activeElement ) ;
167167 if ( index >= 0 && clickableItems [ index ] ) {
168168 clickableItems [ index ] . click ( ) ;
169169 }
@@ -199,7 +199,6 @@ export default function useMenuNavigation ({
199199
200200 return {
201201 menuRef,
202- focusedItem,
203202 isExpanded,
204203 handleKeyDown,
205204 handleKeyDownOpenMenu,
0 commit comments