diff --git a/public/app/core/components/AppChrome/AppChrome.tsx b/public/app/core/components/AppChrome/AppChrome.tsx index e85295e4660..ad875efbb3e 100644 --- a/public/app/core/components/AppChrome/AppChrome.tsx +++ b/public/app/core/components/AppChrome/AppChrome.tsx @@ -15,6 +15,7 @@ import { KioskMode } from 'app/types'; import { AppChromeMenu } from './AppChromeMenu'; import { DOCKED_LOCAL_STORAGE_KEY, DOCKED_MENU_OPEN_LOCAL_STORAGE_KEY } from './AppChromeService'; import { MegaMenu, MENU_WIDTH } from './MegaMenu/MegaMenu'; +import { useMegaMenuFocusHelper } from './MegaMenu/utils'; import { NavToolbar } from './NavToolbar/NavToolbar'; import { ReturnToPrevious } from './ReturnToPrevious/ReturnToPrevious'; import { SingleTopBar } from './TopBar/SingleTopBar'; @@ -50,6 +51,7 @@ export function AppChrome({ children }: Props) { } }, }); + useMegaMenuFocusHelper(state.megaMenuOpen, state.megaMenuDocked); const contentClass = cx({ [styles.content]: true, diff --git a/public/app/core/components/AppChrome/AppChromeMenu.tsx b/public/app/core/components/AppChrome/AppChromeMenu.tsx index cf02b6a7c48..e649fdb12b1 100644 --- a/public/app/core/components/AppChrome/AppChromeMenu.tsx +++ b/public/app/core/components/AppChrome/AppChromeMenu.tsx @@ -58,9 +58,13 @@ export function AppChromeMenu({}: Props) { classNames={animationStyles.overlay} timeout={{ enter: animationSpeed, exit: 0 }} > - - - + <> + {isOpen && ( + + + + )} + { - document.getElementById(state.megaMenuDocked ? 'mega-menu-toggle' : 'dock-menu-button')?.focus(); - }); + if (!config.featureToggles.singleTopNav) { + setTimeout(() => { + document.getElementById(state.megaMenuDocked ? MEGA_MENU_TOGGLE_ID : DOCK_MENU_BUTTON_ID)?.focus(); + }); + } }; const isPinned = useCallback( diff --git a/public/app/core/components/AppChrome/MegaMenu/MegaMenuHeader.tsx b/public/app/core/components/AppChrome/MegaMenu/MegaMenuHeader.tsx index 3fb7d247226..92062891de7 100644 --- a/public/app/core/components/AppChrome/MegaMenu/MegaMenuHeader.tsx +++ b/public/app/core/components/AppChrome/MegaMenu/MegaMenuHeader.tsx @@ -15,6 +15,9 @@ export interface Props { onClose: () => void; } +export const DOCK_MENU_BUTTON_ID = 'dock-menu-button'; +export const MEGA_MENU_HEADER_TOGGLE_ID = 'mega-menu-header-toggle'; + export function MegaMenuHeader({ handleMegaMenu, handleDockedMenu, onClose }: Props) { const theme = useTheme2(); const { chrome } = useGrafana(); @@ -24,13 +27,18 @@ export function MegaMenuHeader({ handleMegaMenu, handleDockedMenu, onClose }: Pr return (
- + { let menuItems = helpItem.children || []; @@ -146,3 +151,33 @@ export function findByUrl(nodes: NavModelItem[], url: string): NavModelItem | nu } return null; } + +/** + * helper to manage focus when opening/closing and docking/undocking the mega menu + * @param isOpen whether the mega menu is open + * @param isDocked whether mega menu is docked + */ +export function useMegaMenuFocusHelper(isOpen: boolean, isDocked: boolean) { + const isSingleTopNav = config.featureToggles.singleTopNav; + // manage focus when opening/closing + useEffect(() => { + if (isSingleTopNav) { + if (isOpen) { + document.getElementById(MEGA_MENU_HEADER_TOGGLE_ID)?.focus(); + } else { + document.getElementById(MEGA_MENU_TOGGLE_ID)?.focus(); + } + } + }, [isOpen, isSingleTopNav]); + + // manage focus when docking/undocking + useEffect(() => { + if (isSingleTopNav) { + if (isDocked) { + document.getElementById(DOCK_MENU_BUTTON_ID)?.focus(); + } else { + document.getElementById(MEGA_MENU_TOGGLE_ID)?.focus(); + } + } + }, [isDocked, isSingleTopNav]); +} diff --git a/public/app/core/components/AppChrome/TopBar/SingleTopBar.tsx b/public/app/core/components/AppChrome/TopBar/SingleTopBar.tsx index e7c3def7054..8e981e3909f 100644 --- a/public/app/core/components/AppChrome/TopBar/SingleTopBar.tsx +++ b/public/app/core/components/AppChrome/TopBar/SingleTopBar.tsx @@ -23,6 +23,8 @@ import { SignInLink } from './SignInLink'; import { TopNavBarMenu } from './TopNavBarMenu'; import { TopSearchBarCommandPaletteTrigger } from './TopSearchBarCommandPaletteTrigger'; +export const MEGA_MENU_TOGGLE_ID = 'mega-menu-toggle'; + interface Props { sectionNav: NavModelItem; pageNav?: NavModelItem; @@ -52,7 +54,12 @@ export const SingleTopBar = memo(function SingleTopBar({
{!menuDockedAndOpen && ( - +