Skip to content

Commit 2ec7b2e

Browse files
author
katiegoines
committed
toggle close on link click
1 parent 6487e5c commit 2ec7b2e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/components/TableOfContents/TableOfContents.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Flex, View, Link, Heading } from '@aws-amplify/ui-react';
22
import { IconTOC } from '@/components/Icons';
3+
import { useContext } from 'react';
4+
import { LayoutContext } from '../Layout';
35
export interface HeadingInterface {
46
linkText: string;
57
hash: string;
@@ -10,6 +12,15 @@ interface TableOfContents {
1012
}
1113

1214
export const TableOfContents = ({ headers }) => {
15+
const { tocOpen, toggleTocOpen } = useContext(LayoutContext);
16+
17+
const onLinkClick = () => {
18+
if (tocOpen) {
19+
// Close the menu after clicking a link (applies to the mobile menu)
20+
toggleTocOpen(false);
21+
}
22+
};
23+
1324
return (
1425
<Flex as="nav" className="toc" aria-labelledby="tocHeader">
1526
{headers ? (
@@ -30,6 +41,7 @@ export const TableOfContents = ({ headers }) => {
3041
<Link
3142
href={`#${hash}`}
3243
className={`toc-item__link toc-item__link--${level}`}
44+
onClick={onLinkClick}
3345
>
3446
{linkText}
3547
</Link>

0 commit comments

Comments
 (0)