Skip to content

Commit f920ec9

Browse files
committed
Fix navigation
1 parent 4cc733e commit f920ec9

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

components/Docs/CodeHighlighting.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ export const CodeHighlighting: React.FunctionComponent<ICodeHighlightingProps> =
8383

8484
shiki.setCDN(`../../`);
8585

86-
console.log(language);
87-
8886
shiki.getHighlighter({
8987
langs: [language as any],
9088
theme: `the-unnamed`

components/Docs/NavGroup.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Section } from '../Link/Section';
33
import { PageFrontMatter } from '../../models';
44
import { Link } from '../Link/Link';
55
import { ParentLink } from '../Link/ParentLink';
6+
import { useRouter } from 'next/router';
67

78
export interface INavGroupProps {
89
items: PageFrontMatter[];
@@ -13,6 +14,9 @@ export const NavGroup: React.FunctionComponent<INavGroupProps> = ({
1314
items,
1415
item
1516
}: React.PropsWithChildren<INavGroupProps>) => {
17+
const router = useRouter();
18+
const [hasActiveLink, setHasActiveLink] = React.useState<boolean>(false);
19+
1620
const getLinks = React.useMemo(() => {
1721
const { content } = item;
1822
const links = Array.from(content.matchAll(/^## (.*$)/gim));
@@ -24,16 +28,25 @@ export const NavGroup: React.FunctionComponent<INavGroupProps> = ({
2428
return null;
2529
}
2630

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+
2738
return (
2839
<ul className={`mt-2 space-y-2`}>
29-
{links.map((link, index) => (
30-
<li key={index}>
31-
<Link
32-
title={link[1]}
33-
link={`/docs/${item.slug !== "index" ? item.slug : ''}#${link[1].toLowerCase().replace(/\s/g, '-')}`}
34-
/>
35-
</li>
36-
))}
40+
{links.map((link, index) => {
41+
return (
42+
<li key={index}>
43+
<Link
44+
title={link[1]}
45+
link={`/docs/${item.slug !== "index" ? item.slug : ''}#${link[1].toLowerCase().replace(/\s/g, '-')}`}
46+
/>
47+
</li>
48+
);
49+
})}
3750

3851
{subItems.map((subItem) => (
3952
<li key={subItem.slug} className={`group`}>
@@ -51,7 +64,8 @@ export const NavGroup: React.FunctionComponent<INavGroupProps> = ({
5164
return (
5265
<Section
5366
title={item.title}
54-
link={`/docs/${item.slug !== "index" ? item.slug : ''}`}>
67+
link={`/docs/${item.slug !== "index" ? item.slug : ''}`}
68+
hasActiveLink={hasActiveLink}>
5569
{getLinks}
5670
</Section>
5771
);

components/Link/Section.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import { useEffect } from 'react';
77
export interface ISectionProps {
88
title: string;
99
link: string;
10+
hasActiveLink: boolean;
1011
}
1112

12-
export const Section: React.FunctionComponent<React.PropsWithChildren<ISectionProps>> = ({ title, link, children }: React.PropsWithChildren<ISectionProps>) => {
13+
export const Section: React.FunctionComponent<React.PropsWithChildren<ISectionProps>> = ({
14+
title,
15+
link,
16+
hasActiveLink,
17+
children
18+
}: React.PropsWithChildren<ISectionProps>) => {
1319
const router = useRouter();
1420
const [isActive, setIsActive] = React.useState(false);
1521
const [showChildren, setShowChildren] = React.useState<boolean | undefined>(undefined);
@@ -34,7 +40,7 @@ export const Section: React.FunctionComponent<React.PropsWithChildren<ISectionPr
3440
title={`Show children of ${title.toLowerCase()}`}
3541
onClick={() => setShowChildren(prev => !prev)} >
3642
{
37-
((isActive && typeof showChildren === "undefined") || showChildren) ? (
43+
((isActive && typeof showChildren === "undefined") || (hasActiveLink && typeof showChildren === "undefined") || showChildren) ? (
3844
<ChevronDownIcon className='h-4' />
3945
) : (
4046
<ChevronRightIcon className='h-4' />
@@ -46,7 +52,8 @@ export const Section: React.FunctionComponent<React.PropsWithChildren<ISectionPr
4652
{
4753
(
4854
(isActive && typeof showChildren === "undefined") ||
49-
(showChildren && typeof showChildren !== "undefined")
55+
(showChildren && typeof showChildren !== "undefined") ||
56+
(hasActiveLink && typeof showChildren === "undefined")
5057
) && children
5158
}
5259
</>

0 commit comments

Comments
 (0)