Repository navigation
Expand file tree
/
Copy pathTableOfContent.js
More file actions
104 lines (94 loc) · 3.35 KB
/
Copy pathTableOfContent.js
File metadata and controls
104 lines (94 loc) · 3.35 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Comes from https://janosh.dev/blog/sticky-active-smooth-responsive-toc
import './tableOfContent.css'
import React, { useState, useEffect } from 'react'
import PropTypes from 'prop-types';
import { chartTypesInfo } from '../util/sectionDescriptions';
import { fullUrlToInternalLink } from '../util/utils';
import { Link } from 'gatsby';
import SectionLogo from './SectionLogo';
const accumulateOffsetTop = (el, totalOffset = 0) => {
while (el) {
totalOffset += el.offsetTop - el.scrollTop + el.clientTop
el = el.offsetParent
}
return totalOffset
}
const ChartTypeLink = ({ chartType }) => {
const chartDescription = chartTypesInfo.filter(chart => chart.id === chartType)[0]
const url = fullUrlToInternalLink(chartDescription.pythonURL)
return (
<div style={{ width: "50px", marginLeft: "16px", marginTop: "25px" }}>
<Link to={url}>
<SectionLogo chartType={chartDescription.logo} />
</Link>
</div>
)
}
ChartTypeLink.propTypes = {
chartType: PropTypes.string
};
export default function TableOfContent({ chartType }) {
const [headings, setHeadings] = useState({
titles: [],
nodes: [],
minDepth: 0,
})
// Controls which heading is currently highlighted as active.
const [active, setActive] = useState(0)
// Read heading titles, depths and nodes from the DOM.
useEffect(() => {
const selector = ["main h2", "main h3"]
const nodes = Array.from(document.querySelectorAll(selector))
const titles = nodes.map(node => ({
title: node.id || node.innerHTML,
depth: Number(node.nodeName[1])
}))
// Compute the minimum heading depth. Will be subtracted from each heading's
// depth to determine the indentation of that heading in the ToC.
const minDepth = Math.min(...titles.map(h => h.depth))
setHeadings({ titles, nodes, minDepth })
}, [])
// Add scroll event listener to update currently active heading.
useEffect(() => {
// Throttling the scrollHandler saves computation and hence battery life.
const scrollHandler = () => {
const { nodes } = headings
const offsets = nodes.map(el => accumulateOffsetTop(el))
const currentVisualizedLocation = window.scrollY + 150
const closest = offsets.reduce(function (prev, curr) {
return (Math.abs(curr - currentVisualizedLocation) < Math.abs(prev - currentVisualizedLocation) ? curr : prev);
}, 0);
const activeIndex = offsets.findIndex(
offset => offset === closest
)
setActive(activeIndex === -1 ? 0 : activeIndex)
}
window.addEventListener(`scroll`, scrollHandler)
return () => window.removeEventListener(`scroll`, scrollHandler)
}, [headings])
return (
<div className="table-of-content">
{headings.titles.map(({ title, depth }, index) => (
<p
key={title}
depth={depth - headings.minDepth}
className={active === index ? "toc-item active-toc-item" : "toc-item"}
onClick={event => {
event.preventDefault()
headings.nodes[index].scrollIntoView({
behavior: `smooth`,
block: `start`,
alignToTop: true
})
}}
dangerouslySetInnerHTML={{ __html: title }}
/>
))
}
{chartType && <ChartTypeLink chartType={chartType} />}
</div >
)
}
TableOfContent.propTypes = {
chartType: PropTypes.string
};