Skip to content

Commit 3c8d438

Browse files
committed
add toc
1 parent f8b560d commit 3c8d438

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

components/toc.tsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"use client";
2+
3+
import React, { useState } from 'react';
4+
5+
export const TableOfContents = ({ headings }) => {
6+
const [isOpen, setIsOpen] = useState(false);
7+
8+
const toggleTOC = () => {
9+
setIsOpen(!isOpen);
10+
};
11+
12+
return (
13+
<div className="toc-container" style={{
14+
margin: '1.5rem 0',
15+
border: '1px solid #e2e8f0',
16+
borderRadius: '0.375rem',
17+
overflow: 'hidden'
18+
}}>
19+
<button
20+
onClick={toggleTOC}
21+
style={{
22+
width: '100%',
23+
padding: '0.75rem 1rem',
24+
backgroundColor: '#f8fafc',
25+
border: 'none',
26+
textAlign: 'left',
27+
fontWeight: 'bold',
28+
cursor: 'pointer',
29+
display: 'flex',
30+
justifyContent: 'space-between',
31+
alignItems: 'center'
32+
}}
33+
>
34+
<span>Table of Contents</span>
35+
<span>{isOpen ? '▲' : '▼'}</span>
36+
</button>
37+
38+
{isOpen && (
39+
<div style={{ padding: '1rem' }}>
40+
<ul style={{ listStyleType: 'none', margin: 0, padding: 0 }}>
41+
{headings.map((heading) => (
42+
<li
43+
key={heading.id}
44+
style={{
45+
marginBottom: '0.5rem',
46+
paddingLeft: `${(heading.depth - 1) * 1.5}rem`
47+
}}
48+
>
49+
<a
50+
href={`#${heading.id}`}
51+
style={{
52+
color: '#3182ce',
53+
textDecoration: 'none'
54+
}}
55+
>
56+
{heading.text}
57+
</a>
58+
</li>
59+
))}
60+
</ul>
61+
</div>
62+
)}
63+
</div>
64+
);
65+
};

0 commit comments

Comments
 (0)