import { useEffect, useRef, useState } from "react"; import { LearnMenu } from "../learn-menu"; import { ReferenceMenu } from "../reference-menu"; import { PlusMenu } from "../plus-menu"; import "./index.scss"; import { PLUS_IS_ENABLED } from "../../../env"; import { useGleanClick } from "../../../telemetry/glean-context"; import { MENU } from "../../../telemetry/constants"; import { useLocation } from "react-router"; import { ToolsMenu } from "../tools-menu"; export default function MainMenu({ isOpenOnMobile }) { const previousActiveElement = useRef(null); const mainMenuRef = useRef(null); const [visibleSubMenuId, setVisibleSubMenuId] = useState(null); function hideSubMenuIfVisible() { if (visibleSubMenuId) { setVisibleSubMenuId(null); } } useEffect(() => { const mainMenu = mainMenuRef.current; // by default the main menu contains a `nojs` class which // then allows users on desktop to interact with the main // menu via hover events if the JavsScript failed for whatever // reason. If all is well though, we remove the class here and // let JavaScript take over the interaction if (mainMenu) { mainMenu.classList.remove("nojs"); } const focusableSubmenuItemSelector = 'ul.show a[tabindex="0"]'; mainMenu ?.querySelector(focusableSubmenuItemSelector) ?.focus(); document.addEventListener("keyup", (event) => { if (event.key === "Escape") { hideSubMenuIfVisible(); if (previousActiveElement.current) { previousActiveElement.current.focus(); } } }); }); useEffect(() => { if (!isOpenOnMobile && visibleSubMenuId) { setVisibleSubMenuId(null); } }, [isOpenOnMobile, visibleSubMenuId]); function toggleMenu(id) { if (visibleSubMenuId === id) { setVisibleSubMenuId(null); } else { setVisibleSubMenuId(id); } } return ( ); } function TopLevelMenuLink({ to, children, }: { to: string; children: React.ReactNode; }) { const { pathname } = useLocation(); const gleanClick = useGleanClick(); const isActive = pathname.startsWith(to.split("#", 2)[0]); return (
  • gleanClick(`${MENU.CLICK_LINK}: top-level -> ${to}`)} > {children}
  • ); }