Skip to content

Commit b9cc123

Browse files
authored
feat: add table of content (#115)
1 parent 20f9991 commit b9cc123

13 files changed

Lines changed: 431 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"dependencies": {
1616
"@mdx-js/react": "3.1.0",
17+
"@radix-ui/react-scroll-area": "1.2.9",
1718
"clsx": "2.1.1",
1819
"modern-normalize": "3.0.1",
1920
"posthog-js": "1.248.0",

pnpm-lock.yaml

Lines changed: 179 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Mdx/Title/Title.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ export function Heading({
8181
order,
8282
...props
8383
}: TitleProps & { order: Order }): JSX.Element {
84-
props.id = getIdFrom(props.children)
84+
const headingId = getIdFrom(props.children)
85+
props.id = headingId
8586
props.className = cx(`title-${Math.min(order, 4)}`, styles.title)
87+
// @ts-expect-error -- custom data attribute
88+
props['data-heading'] = order > 0 ? headingId : undefined
89+
// @ts-expect-error -- custom data attribute
90+
props['data-order'] = order > 0 ? order : undefined
8691

8792
switch (order) {
8893
case 1:

src/components/ResponsiveLayout/ResponsiveLayout.module.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@
4545
.tableOfContents {
4646
@include sidecontent;
4747

48-
& {
49-
background-color: lightgray;
50-
}
51-
5248
@include up-to-xl {
5349
display: none;
5450
}

src/components/ResponsiveLayout/ResponsiveLayout.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { SideNavigation } from '@/components/SideNavigation'
44

55
import { Breadcrumbs } from '../Breadcrumbs'
66

7+
import { TableOfContents } from '../TableOfContents'
8+
import { ScrollArea } from '../ScrollArea'
9+
710
import styles from './ResponsiveLayout.module.scss'
811

912
interface ResponsiveLayoutProps {
@@ -28,11 +31,15 @@ export function ResponsiveLayout({
2831
<SideNavigation />
2932
</aside>
3033
)}
31-
<article className={styles.body}>
34+
<article className={styles.body} id="mdx-root">
3235
{withBreadcrumbs && <Breadcrumbs />}
3336
{children}
3437
</article>
35-
<div className={styles.tableOfContents} />
38+
<div className={styles.tableOfContents}>
39+
<ScrollArea>
40+
<TableOfContents />
41+
</ScrollArea>
42+
</div>
3643
</div>
3744
)
3845
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.scrollArea {
2+
width: 100%;
3+
height: calc(100vh - 90px);
4+
overflow: hidden;
5+
--scrollbar-size: 10px;
6+
7+
.viewport {
8+
width: 100%;
9+
height: 100%;
10+
border-radius: inherit;
11+
}
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { JSX, ReactNode } from 'react'
2+
import {
3+
ScrollArea as ScrollAreaRoot,
4+
ScrollAreaCorner,
5+
ScrollAreaScrollbar,
6+
ScrollAreaThumb,
7+
ScrollAreaViewport,
8+
} from '@radix-ui/react-scroll-area'
9+
10+
import styles from './ScrollArea.module.scss'
11+
12+
export function ScrollArea({ children }: { children: ReactNode }): JSX.Element {
13+
return (
14+
<ScrollAreaRoot className={styles.scrollArea}>
15+
<ScrollAreaViewport className={styles.viewport}>
16+
{children}
17+
</ScrollAreaViewport>
18+
<ScrollAreaScrollbar orientation="vertical">
19+
<ScrollAreaThumb />
20+
</ScrollAreaScrollbar>
21+
<ScrollAreaCorner />
22+
</ScrollAreaRoot>
23+
)
24+
}

src/components/ScrollArea/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { ScrollArea } from './ScrollArea'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.title {
2+
margin: 0 0 12px;
3+
}
4+
5+
.list {
6+
border-left: 1px solid var(--border-primary);
7+
display: flex;
8+
flex-direction: column;
9+
list-style: none;
10+
gap: 12px;
11+
margin: 0;
12+
padding: 0;
13+
14+
.link {
15+
color: var(--content-secondary);
16+
text-decoration: none;
17+
}
18+
19+
.activeLink {
20+
color: var(--content-accent) !important;
21+
}
22+
}

0 commit comments

Comments
 (0)