From dd035088c61e48ca2f5787cb2c433b64d506c443 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Tue, 13 Jul 2021 11:00:00 +0300 Subject: [PATCH] refactor(v2): use Collapsible for mobile nav items (#5145) --- .../theme/NavbarItem/DefaultNavbarItem.tsx | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) 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} -
      + {items.map(({className: childItemClassName, ...childItemProps}, i) => (
    • ))} -
    +
  • ); }