Change rect top (#270)

Change rect top
This commit is contained in:
Ilya Y 2023-10-17 16:37:54 +03:00 committed by GitHub
parent ac069b2776
commit 7128b148c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 8 deletions

View File

@ -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)

View File

@ -13,6 +13,6 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"skipLibCheck": true, "skipLibCheck": true,
"isolatedModules": true, "isolatedModules": true,
"lib": ["ES2021", "dom"] "lib": ["es2023", "dom"]
} }
} }