From a1ce9c20434a8d01820cd1d2fc7fe57501c64ce6 Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Thu, 23 Jun 2022 13:44:19 +0200 Subject: [PATCH] [website] Fix navigation menu close behavior (#33203) --- docs/src/components/header/HeaderNavBar.tsx | 59 ++++++++++++++------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/docs/src/components/header/HeaderNavBar.tsx b/docs/src/components/header/HeaderNavBar.tsx index 8dc1328eee840b..6bee5aa7b764aa 100644 --- a/docs/src/components/header/HeaderNavBar.tsx +++ b/docs/src/components/header/HeaderNavBar.tsx @@ -3,6 +3,7 @@ import { styled, alpha } from '@mui/material/styles'; import Box from '@mui/material/Box'; import Popper from '@mui/material/Popper'; import Paper from '@mui/material/Paper'; +import { unstable_debounce as debounce } from '@mui/utils'; import Fade from '@mui/material/Fade'; import Typography from '@mui/material/Typography'; import IconImage from 'docs/src/components/icon/IconImage'; @@ -116,9 +117,8 @@ function getNextIndex(eventKey: KeyboardEvent['key'], currentIndex: number, leng } export default function HeaderNavBar() { - const [subMenuOpen, setSubMenuOpen] = React.useState(false); + const [subMenuOpen, setSubMenuOpen] = React.useState(null); const [subMenuIndex, setSubMenuIndex] = React.useState(null); - const [docsMenuOpen, setDocsMenuOpen] = React.useState(false); const navRef = React.useRef(null); const productsMenuRef = React.useRef(null); const docsMenuRef = React.useRef(null); @@ -165,7 +165,7 @@ export default function HeaderNavBar() { if (event.key === 'ArrowDown') { event.preventDefault(); if (event.target === productsMenuRef.current) { - setSubMenuOpen(true); + setSubMenuOpen('products'); } setSubMenuIndex((prevValue) => { if (prevValue === null) { @@ -190,20 +190,40 @@ export default function HeaderNavBar() { }); } if (event.key === 'Escape') { - setSubMenuOpen(false); + setSubMenuOpen(null); setSubMenuIndex(null); } } + + const setSubMenuOpenDebounced = React.useMemo( + () => debounce(setSubMenuOpen, 40), + [setSubMenuOpen], + ); + + const setSubMenuOpenUndebounce = React.useMemo( + () => (value: typeof subMenuOpen) => { + setSubMenuOpenDebounced.clear(); + setSubMenuOpen(value); + }, + [setSubMenuOpen, setSubMenuOpenDebounced], + ); + + React.useEffect(() => { + return () => { + setSubMenuOpenDebounced.clear(); + }; + }, [setSubMenuOpenDebounced]); + return (
    {FEATURE_TOGGLE.nav_products && (
  • setSubMenuOpen(true)} - onFocus={() => setSubMenuOpen(true)} - onMouseOut={() => setSubMenuOpen(false)} - onBlur={() => setSubMenuOpen(false)} + onMouseEnter={() => setSubMenuOpenUndebounce('products')} + onFocus={() => setSubMenuOpenUndebounce('products')} + onMouseLeave={() => setSubMenuOpenDebounced(null)} + onBlur={() => setSubMenuOpenUndebounce(null)} > {({ TransitionProps }) => ( @@ -304,10 +327,10 @@ export default function HeaderNavBar() { )}
  • setDocsMenuOpen(true)} - onFocus={() => setDocsMenuOpen(true)} - onMouseOut={() => setDocsMenuOpen(false)} - onBlur={() => setDocsMenuOpen(false)} + onMouseEnter={() => setSubMenuOpenUndebounce('docs')} + onFocus={() => setSubMenuOpenUndebounce('docs')} + onMouseLeave={() => setSubMenuOpenDebounced(null)} + onBlur={() => setSubMenuOpenUndebounce(null)} > {({ TransitionProps }) => (