parent
ac069b2776
commit
7128b148c2
|
@ -16,12 +16,7 @@ interface Props {
|
||||||
|
|
||||||
const isInViewport = (el: Element): boolean => {
|
const isInViewport = (el: Element): boolean => {
|
||||||
const rect = el.getBoundingClientRect()
|
const rect = el.getBoundingClientRect()
|
||||||
return (
|
return rect.top <= DEFAULT_HEADER_OFFSET
|
||||||
rect.top >= 0 &&
|
|
||||||
rect.left >= 0 &&
|
|
||||||
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
|
|
||||||
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
const scrollToHeader = (element) => {
|
const scrollToHeader = (element) => {
|
||||||
window.scrollTo({
|
window.scrollTo({
|
||||||
|
@ -57,7 +52,7 @@ export const TableOfContents = (props: Props) => {
|
||||||
const debouncedUpdateHeadings = debounce(updateHeadings, 500)
|
const debouncedUpdateHeadings = debounce(updateHeadings, 500)
|
||||||
|
|
||||||
const updateActiveHeader = throttle(() => {
|
const updateActiveHeader = throttle(() => {
|
||||||
const newActiveIndex = headings().findIndex((heading) => isInViewport(heading))
|
const newActiveIndex = headings().findLastIndex((heading) => isInViewport(heading))
|
||||||
setActiveHeaderIndex(newActiveIndex)
|
setActiveHeaderIndex(newActiveIndex)
|
||||||
}, 50)
|
}, 50)
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,6 @@
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"lib": ["ES2021", "dom"]
|
"lib": ["es2023", "dom"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user