@@ -198,8 +198,14 @@ export function useMenu<ToggleEl extends HTMLElement>(
198198 disableFocusOnMount = false ,
199199 disableFocusOnUnmount = false ,
200200 } = options ;
201- const { menubar, activeId, setActiveId, hoverTimeout, setAnimatedOnce } =
202- useMenuBarContext ( ) ;
201+ const {
202+ root,
203+ menubar,
204+ activeId,
205+ setActiveId,
206+ hoverTimeout,
207+ setAnimatedOnce,
208+ } = useMenuBarContext ( ) ;
203209 const touch = useIsUserInteractionMode ( "touch" ) ;
204210
205211 const timeout = useRef < number | undefined > ( ) ;
@@ -290,13 +296,22 @@ export function useMenu<ToggleEl extends HTMLElement>(
290296 return ;
291297 }
292298
299+ // this is to fix keyboard movement behavior when navigating between
300+ // different root-level menuitems with the `ArrowLeft` and `ArrowRight` keys
301+ // while menus are visible. If the exit focus behavior is not cancelled, the
302+ // next menu's menu will be visible, but the current menu's menuitem would
303+ // be the current focus which breaks everything
304+ cancelExitFocus . current =
305+ cancelExitFocus . current ||
306+ ! menuNodeRef . current ?. contains ( document . activeElement ) ;
307+
293308 setActiveId ( ( prevActiveId ) =>
294309 baseId === prevActiveId ? "" : prevActiveId
295310 ) ;
296- } , [ baseId , setActiveId , visible ] ) ;
311+ } , [ baseId , root , setActiveId , visible ] ) ;
297312 useEffect ( ( ) => {
298313 setVisible ( baseId === activeId ) ;
299- } , [ activeId , baseId , setVisible ] ) ;
314+ } , [ activeId , baseId , root , setVisible ] ) ;
300315
301316 return {
302317 menuRef : nodeRef ,
0 commit comments