Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc sidebar transition #16

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
max-height: calc(100vh - 8rem);
overflow-y: auto;
overflow-x: hidden;
scrollbar-gutter: stable;
@include custom-scrollbar;
}

Expand Down Expand Up @@ -457,12 +458,6 @@
gap: 0.5rem;
padding: 0.5rem 0.8rem 0.5rem 0rem;

&__active {
padding-left: 0.5rem;
margin-left: 1px;
border-left: 3px solid var(--mdx-link-color) !important;
}

&__heading {
font-size: 20px;
font-weight: bold;
Expand All @@ -487,6 +482,7 @@

li,
&__list {
position: relative;
list-style-type: none;
margin: 0;

Expand Down Expand Up @@ -539,17 +535,11 @@
&__active-section {
color: var(--body-text) !important;
font-weight: 600;
margin-left: -2px;
border-left: 3px solid var(--mdx-link-color) !important;
// text-decoration: underline;
}

&__active-link {
color: var(--body-text) !important;
margin-left: -2px;
border-left: 3px solid var(--mdx-link-color) !important;
font-weight: 600;
// text-decoration: underline;
}
}
}
Expand Down
61 changes: 46 additions & 15 deletions src/components/developers/DocsNavSidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo, useCallback, useEffect, useState } from "react";
import { memo, useCallback, useEffect, useRef, useState } from "react";
import ContentApi from "@/utils/contentApi";
import classNames from "classnames";
import Link from "next/link";
Expand Down Expand Up @@ -157,28 +157,58 @@ const SidebarMenu = memo(
currentPath,
nestedCount = 1,
isOpen = true,
isInFolder = false,
}: SidebarMenuProps) => {
const [height, setHeight] = useState("auto");
const [isRendered, setIsRendered] = useState(isOpen);
const contentRef = useRef<HTMLUListElement>(null);

const updateHeight = useCallback(() => {
if (contentRef.current) {
const maxHeight = contentRef.current.scrollHeight;
setHeight(isOpen ? `${maxHeight}px` : "0px");
}
}, [isOpen]);

useEffect(() => {
if (isRendered) {
updateHeight();
const resizeObserver = new ResizeObserver(updateHeight);
if (contentRef.current) resizeObserver.observe(contentRef.current);
return () => resizeObserver.disconnect();
}
}, [isRendered, updateHeight]);

useEffect(() => {
if (isOpen) {
setIsRendered(true);
}
}, [isOpen]);

// do nothing if there are no child items
if (!navItems?.length) return <></>;

return (
<ul
<div
style={{
display: !isOpen ? "none" : undefined,
borderColor: isInFolder ? "var(--mdx-details-border)" : undefined,
maxHeight: height,
opacity: isOpen ? 1 : 0,
transition: "max-height 300ms ease-in-out, opacity 300ms ease-in-out",
}}
>
{navItems.map((section, key) => (
<SidebarMenuChildren
isExpandedDefault={isExpandedDefault}
key={key}
section={section}
currentPath={currentPath}
nestedCount={nestedCount}
/>
))}
</ul>
{isRendered && (
<ul ref={contentRef}>
{navItems.map((section, key) => (
<SidebarMenuChildren
isExpandedDefault={isExpandedDefault}
key={key}
section={section}
currentPath={currentPath}
nestedCount={nestedCount}
/>
))}
</ul>
)}
</div>
);
},
);
Expand Down Expand Up @@ -213,6 +243,7 @@ const SidebarMenuChildren = memo(
e.target?.tagName.toLowerCase(),
);
if (clickedToggleIcon) e.preventDefault();

setIsOpen(!isOpen);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
Loading