fix(v2): animate dropdown properly (#3603)

* fix(v2): animate dropdown properly

* Simplify logic
This commit is contained in:
Alexey Pyltsyn 2020-10-21 17:19:16 +03:00 committed by GitHub
parent 91256d445e
commit ce06e10e24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 4 deletions

View file

@ -64,6 +64,16 @@ function DocSidebarItemCategory({
return isActive ? false : item.collapsed;
});
const menuListRef = useRef<HTMLUListElement>(null);
const [menuListHeight, setMenuListHeight] = useState<string | undefined>(
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}
</a>
<ul className="menu__list">
<ul
className="menu__list"
ref={menuListRef}
style={{
height: menuListHeight,
}}
onTransitionEnd={() => {
if (!collapsed) {
handleMenuListHeight(false);
}
}}>
{items.map((childItem) => (
<DocSidebarItem
tabIndex={collapsed ? '-1' : '0'}

View file

@ -89,3 +89,15 @@
line-height: 0.9;
width: 24px;
}
/* TODO: Move to Infima */
:global(.menu__list) :global(.menu__list) {
overflow-y: hidden;
will-change: height;
/* Same as "arrow" transition */
transition: height var(--ifm-transition-fast) linear;
}
:global(.menu__list-item--collapsed) :global(.menu__list) {
height: 0px !important;
}

View file

@ -156,6 +156,7 @@ function NavItemMobile({
position: _position, // Need to destructure position from props so that it doesn't get passed on.
...props
}: DesktopOrMobileNavBarItemProps) {
const menuListRef = useRef<HTMLUListElement>(null);
const {pathname} = useLocation();
const [collapsed, setCollapsed] = useState(
() => !items?.some((item) => isSamePath(item.to, pathname)) ?? true,
@ -192,7 +193,14 @@ function NavItemMobile({
}}>
{props.label}
</NavLink>
<ul className="menu__list">
<ul
className="menu__list"
ref={menuListRef}
style={{
height: !collapsed
? `${menuListRef.current?.scrollHeight}px`
: undefined,
}}>
{items.map(({className: childItemClassName, ...childItemProps}, i) => (
<li className="menu__list-item" key={i}>
<NavLink