fix: Direct Link in Docs don't open Navigation Submenu (#3561)

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
This commit is contained in:
Dishant Yadav
2024-10-29 16:25:37 +05:30
committed by GitHub
parent 33543f59f8
commit 19a3faadce

View File

@@ -289,6 +289,25 @@ interface NavigationProps extends React.ComponentPropsWithoutRef<"nav"> {
export const Navigation = ({ isMobile, ...props }: NavigationProps) => {
const [activeGroup, setActiveGroup] = useState<NavGroup | null>(navigation[0]);
const [openGroups, setOpenGroups] = useState<string[]>([]);
const pathname = usePathname();
useEffect(() => {
// Check the current pathname and set the active group
navigation.forEach((group) => {
group.links.forEach((link) => {
if (link.href && pathname.startsWith(link.href)) {
setActiveGroup(group);
} else if (link.children) {
link.children.forEach((child) => {
if (pathname.startsWith(child.href)) {
setActiveGroup(group);
setOpenGroups([`${group.title}-${link.title}`]); // Ensure parent is open
}
});
}
});
});
}, [pathname]);
return (
<nav {...props}>