From ce06e10e248dd49b0fe2e89253fdd8d6c41884da Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Wed, 21 Oct 2020 17:19:16 +0300 Subject: [PATCH] fix(v2): animate dropdown properly (#3603) * fix(v2): animate dropdown properly * Simplify logic --- .../src/theme/DocSidebar/index.tsx | 31 +++++++++++++++++-- .../src/theme/DocSidebar/styles.module.css | 12 +++++++ .../theme/NavbarItem/DefaultNavbarItem.tsx | 10 +++++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/DocSidebar/index.tsx b/packages/docusaurus-theme-classic/src/theme/DocSidebar/index.tsx index 96a98a7028..0fb5307b8d 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocSidebar/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/DocSidebar/index.tsx @@ -64,6 +64,16 @@ function DocSidebarItemCategory({ return isActive ? false : item.collapsed; }); + const menuListRef = useRef(null); + const [menuListHeight, setMenuListHeight] = useState( + undefined, + ); + const handleMenuListHeight = (calc = true) => { + setMenuListHeight( + calc ? `${menuListRef.current?.scrollHeight}px` : undefined, + ); + }; + // If we navigate to a category, it should automatically expand itself useEffect(() => { const justBecameActive = isActive && !wasActive; @@ -75,9 +85,14 @@ function DocSidebarItemCategory({ const handleItemClick = useCallback( (e) => { e.preventDefault(); - setCollapsed((state) => !state); + + if (!menuListHeight) { + handleMenuListHeight(); + } + + setTimeout(() => setCollapsed((state) => !state), 100); }, - [setCollapsed], + [menuListHeight], ); if (items.length === 0) { @@ -101,7 +116,17 @@ function DocSidebarItemCategory({ {...props}> {label} -
    +
      { + if (!collapsed) { + handleMenuListHeight(false); + } + }}> {items.map((childItem) => ( (null); const {pathname} = useLocation(); const [collapsed, setCollapsed] = useState( () => !items?.some((item) => isSamePath(item.to, pathname)) ?? true, @@ -192,7 +193,14 @@ function NavItemMobile({ }}> {props.label} -
        +
          {items.map(({className: childItemClassName, ...childItemProps}, i) => (