-
-
Notifications
You must be signed in to change notification settings - Fork 535
Expand file tree
/
Copy pathMiniNav.tsx
More file actions
82 lines (78 loc) · 2.3 KB
/
Copy pathMiniNav.tsx
File metadata and controls
82 lines (78 loc) · 2.3 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { useEffect, useState } from 'react';
import { Box, Link, Typography, useMediaQuery } from '@mui/material';
import { useRouter } from 'next/router';
import { EthicalAd } from '../mdx/EthicalAd';
export const MiniNav = () => {
const { pathname } = useRouter();
const isXLDesktop = useMediaQuery('(min-width: 1800px)');
const [headings, setHeadings] = useState<NodeListOf<HTMLElement>>();
useEffect(() => {
setHeadings(
document.querySelectorAll(isXLDesktop ? 'h2, h3, h4, h5' : 'h3'),
);
}, [isXLDesktop, pathname]);
return (
<Box
sx={{
position: isXLDesktop ? 'fixed' : undefined,
top: '60px',
right: '2rem',
minWidth: '100px',
maxWidth: isXLDesktop ? '250px' : '500px',
}}
>
<Typography sx={{ mt: '1rem' }} component="div" variant="h6">
On This Page
</Typography>
<ul
style={{
padding: 0,
maxHeight: isXLDesktop ? 'calc(100vh - 9rem)' : undefined,
overflowY: isXLDesktop ? 'auto' : undefined,
}}
>
{Array.from(headings ?? []).map((heading, index) => {
if (
!isXLDesktop &&
['demo', 'source code'].includes(
heading.innerText.toLowerCase().trim(),
)
) {
return;
}
return (
<li
key={index}
style={{
listStyle: 'none',
paddingLeft:
heading.localName === 'h3'
? '1rem'
: heading.localName === 'h4'
? '2rem'
: heading.localName === 'h5'
? '3rem'
: 0,
}}
>
<Link
href={`#${heading.id}`}
sx={(theme) => ({
color:
theme.palette.grey[
theme.palette.mode === 'dark' ? 400 : 700
],
})}
>
<Typography component="span" variant="subtitle2">
{heading.innerText}
</Typography>
</Link>
</li>
);
})}
</ul>
{isXLDesktop && <EthicalAd id="mini-nav" vertical />}
</Box>
);
};