Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dynamic indentation for table of contents items #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions www/src/components/table-of-contents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import { HTMLAttributes, useCallback, useEffect } from "react"

interface TableOfContentsProps extends HTMLAttributes<HTMLDivElement> {}

const getIndentationClass = (level: number) => {
switch (level) {
case 2:
return "pl-4"
case 3:
return "pl-8"
case 4:
return "pl-12"
default:
return "pl-4"
}
}

export const TableOfContents = ({
className,
...props
Expand Down Expand Up @@ -38,14 +51,15 @@ export const TableOfContents = ({
return (
<div className="antialiased">
<div className={cn("text-sm/7 relative", className)} {...props}>
<p className="text-sm/7 font-semibold text-muted-light pl-4 ">
<p className="text-sm/7 font-semibold text-muted-light pl-4">
On this page
</p>
<ul>
{allHeadings.map((heading, i) => {
const isVisible = visibleSections.some(
(section) => section === slugify(heading.text),
)
const indentClass = getIndentationClass(heading.level)

return (
<li key={i} className="leading-relaxed py-1">
Expand All @@ -62,7 +76,8 @@ export const TableOfContents = ({
/>
<p
className={cn(
"text-muted-dark hover:text-muted-light transition pl-4 py-1",
"text-muted-dark hover:text-muted-light transition py-1",
indentClass,
{
"text-muted-light": isVisible,
},
Expand Down