diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx index 5a2698388c..88805feef2 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx @@ -10,7 +10,11 @@ import clsx from 'clsx'; import Link from '@docusaurus/Link'; import useBaseUrl from '@docusaurus/useBaseUrl'; import {useLocation} from '@docusaurus/router'; -import {isSamePath} from '@docusaurus/theme-common'; +import { + isSamePath, + useCollapsible, + Collapsible, +} from '@docusaurus/theme-common'; import type { NavLinkProps, DesktopOrMobileNavBarItemProps, @@ -167,11 +171,11 @@ function NavItemMobile({ position: _position, // Need to destructure position from props so that it doesn't get passed on. ...props }: DesktopOrMobileNavBarItemProps) { - const menuListRef = useRef(null); const {pathname} = useLocation(); - const [collapsed, setCollapsed] = useState( - () => !items?.some((item) => isSamePath(item.to, pathname)) ?? true, - ); + const {collapsed, toggleCollapsed} = useCollapsible({ + initialState: () => + !items?.some((item) => isSamePath(item.to, pathname)) ?? true, + }); const navLinkClassNames = (extraClassName?: string, isSubList = false) => clsx( @@ -190,10 +194,6 @@ function NavItemMobile({ ); } - const menuListHeight = menuListRef.current?.scrollHeight - ? `${menuListRef.current?.scrollHeight}px` - : undefined; - return (
  • { e.preventDefault(); - setCollapsed((state) => !state); + toggleCollapsed(); }}> {props.children ?? props.label} - +
  • ); }