Skip to content

Commit a5fdd77

Browse files
committed
Fix navigation
1 parent 66e716d commit a5fdd77

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

components/Docs/NavGroup.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export const NavGroup: React.FunctionComponent<INavGroupProps> = ({
1515
item
1616
}: React.PropsWithChildren<INavGroupProps>) => {
1717
const router = useRouter();
18-
const [hasActiveLink, setHasActiveLink] = React.useState<boolean>(false);
1918

2019
const getLinks = React.useMemo(() => {
2120
const { content } = item;
@@ -28,13 +27,6 @@ export const NavGroup: React.FunctionComponent<INavGroupProps> = ({
2827
return null;
2928
}
3029

31-
const crntPath = router.asPath.replace(/\/#/, '#');
32-
let activeLink = !!links.find(link => crntPath === link[1].replace(/\/#/, '#'));
33-
if (!activeLink && subItems && subItems.length > 0) {
34-
activeLink = !!subItems.find(link => crntPath === `/docs/${link.slug}`.replace(/\/#/, '#'));
35-
}
36-
setHasActiveLink(!!activeLink);
37-
3830
return (
3931
<ul className={`mt-2 space-y-2`}>
4032
{links.map((link, index) => {
@@ -64,8 +56,7 @@ export const NavGroup: React.FunctionComponent<INavGroupProps> = ({
6456
return (
6557
<Section
6658
title={item.title}
67-
link={`/docs/${item.slug !== "index" ? item.slug : ''}`}
68-
hasActiveLink={hasActiveLink}>
59+
link={`/docs/${item.slug !== "index" ? item.slug : ''}`}>
6960
{getLinks}
7061
</Section>
7162
);

components/Link/Section.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,29 @@ import { useEffect } from 'react';
77
export interface ISectionProps {
88
title: string;
99
link: string;
10-
hasActiveLink: boolean;
1110
}
1211

1312
export const Section: React.FunctionComponent<React.PropsWithChildren<ISectionProps>> = ({
1413
title,
1514
link,
16-
hasActiveLink,
1715
children
1816
}: React.PropsWithChildren<ISectionProps>) => {
1917
const router = useRouter();
2018
const [isActive, setIsActive] = React.useState(false);
2119
const [showChildren, setShowChildren] = React.useState<boolean | undefined>(undefined);
20+
const [hasActiveLink, setHasActiveLink] = React.useState<boolean | undefined>(undefined);
2221

2322
useEffect(() => {
2423
const page = router.asPath;
25-
setIsActive(page === link || link === `${page}/` || page.includes(`${link}#`) || page.includes(`${link}index`));
24+
console.log(page, link);
25+
// Remove the last slash
26+
const crntLink = link.endsWith('/') ? link.slice(0, -1) : link;
27+
console.log(page, crntLink);
28+
setIsActive(page === crntLink || crntLink === `${page}/` || page.includes(`${crntLink}#`) || page.includes(`${crntLink}index`));
29+
30+
if (crntLink.split('/').length > 2) {
31+
setHasActiveLink(page.includes(crntLink))
32+
}
2633
}, [router.asPath, link]);
2734

2835
return (
@@ -40,7 +47,11 @@ export const Section: React.FunctionComponent<React.PropsWithChildren<ISectionPr
4047
title={`Show children of ${title.toLowerCase()}`}
4148
onClick={() => setShowChildren(prev => !prev)} >
4249
{
43-
((isActive && typeof showChildren === "undefined") || (hasActiveLink && typeof showChildren === "undefined") || showChildren) ? (
50+
(
51+
(isActive && typeof showChildren === "undefined") ||
52+
(hasActiveLink && typeof showChildren === "undefined") ||
53+
showChildren
54+
) ? (
4455
<ChevronDownIcon className='h-4' />
4556
) : (
4657
<ChevronRightIcon className='h-4' />
@@ -52,8 +63,8 @@ export const Section: React.FunctionComponent<React.PropsWithChildren<ISectionPr
5263
{
5364
(
5465
(isActive && typeof showChildren === "undefined") ||
55-
(showChildren && typeof showChildren !== "undefined") ||
56-
(hasActiveLink && typeof showChildren === "undefined")
66+
(hasActiveLink && typeof showChildren === "undefined") ||
67+
(showChildren && typeof showChildren !== "undefined")
5768
) && children
5869
}
5970
</>

0 commit comments

Comments
 (0)