Skip to content

Commit

Permalink
Fix nav scroll behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jkicillof committed Nov 15, 2023
1 parent ae8bdaa commit 82daee7
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions components/Nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ function Nav({ currentSection, contents }) {
}

useEffect(() => {
if(currentSection && currentSection.nav) {
if(currentSection.nav.offsetTop < container.scrollTop + header.clientHeight) {
container.scrollTo({
behavior: 'smooth',
top: currentSection.nav.offsetTop - header.clientHeight
})
} else if(container.scrollTop + window.innerHeight < currentSection.nav.offsetTop + currentSection.nav.clientHeight ) {
container.scrollTo({
behavior: 'smooth',
top: currentSection.nav.offsetTop + currentSection.nav.clientHeight - window.innerHeight
})
const timeout = setTimeout(() => {
if(currentSection && currentSection.nav) {
if(currentSection.nav.offsetTop < container.scrollTop + header.clientHeight) {
container.scrollTo({
behavior: 'smooth',
top: currentSection.nav.offsetTop - header.clientHeight
})
} else if(container.scrollTop + window.innerHeight < currentSection.nav.offsetTop + currentSection.nav.clientHeight ) {
container.scrollTo({
behavior: 'smooth',
top: currentSection.nav.offsetTop + currentSection.nav.clientHeight - window.innerHeight
})
}
}
}
}, 100)
return () => clearTimeout(timeout)
}, [currentSection])

return (
Expand Down

0 comments on commit 82daee7

Please sign in to comment.