-
-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathTocMobile.tsx
More file actions
54 lines (49 loc) · 1.72 KB
/
TocMobile.tsx
File metadata and controls
54 lines (49 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React from 'react'
import { Link } from '@tanstack/react-router'
import type { HeadingData } from 'marked-gfm-heading-id'
import { FaCaretRight, FaCaretDown } from 'react-icons/fa6'
interface TocMobileProps {
headings: Array<HeadingData>
}
export function TocMobile({ headings }: TocMobileProps) {
const [isOpen, setIsOpen] = React.useState(false)
const handleToggle = (event: React.SyntheticEvent<HTMLDetailsElement>) => {
setIsOpen(event.currentTarget.open)
}
if (headings.length === 0) {
return null
}
return (
<div className="lg:hidden flex -mx-2 -mt-2 md:-mx-6 md:-mt-6 pb-3 md:pb-5">
<details className="w-full" onToggle={handleToggle}>
<summary
className="px-4 py-3 text-sm font-medium w-full flex content-start items-center gap-2 bg-white/50 dark:bg-black/60 backdrop-blur-lg border-b border-gray-500 border-opacity-20"
aria-expanded={isOpen}
>
<span>{isOpen ? <FaCaretDown /> : <FaCaretRight />}</span>
<span>On this page</span>
</summary>
<div className="px-2 py-2">
<ul className="list-none grid gap-2">
{headings.map((heading) => (
<li
key={`mobile-toc-${heading.id}`}
style={{
paddingLeft: `${(heading.level - 1) * 0.7}rem`,
}}
>
<Link
to="."
className="text-sm"
hash={heading.id}
dangerouslySetInnerHTML={{ __html: heading.text }}
aria-label={heading.text.replace(/<\/?[^>]+(>|$)/g, '')}
/>
</li>
))}
</ul>
</div>
</details>
</div>
)
}