Skip to content

Commit

Permalink
docs: save sidebar scrollbar position
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Dec 1, 2024
1 parent 54dfdd9 commit 277977b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/components/Sidebar/Sidebar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ let { isMobileOnly = false } = Astro.props
initMenu()
</script>

<script is:inline>
const SIDEBAR_SCROLL_POSITION = 'SIDEBAR_SCROLL_POSITION'

let initSidebarScroll = () => {
let savedScrollPosition = sessionStorage.getItem(SIDEBAR_SCROLL_POSITION)
if (savedScrollPosition) {
let menu = document.getElementById('menu')
menu.scrollTop = Number(savedScrollPosition)
}
let menu = document.getElementById('menu')
menu.addEventListener('scrollend', event => {
let scrollPosition = event.target.scrollTop
sessionStorage.setItem(SIDEBAR_SCROLL_POSITION, scrollPosition)
})
}

document.addEventListener('astro:after-swap', initSidebarScroll)
initSidebarScroll()
</script>

<style>
.sidebar {
position: fixed;
Expand Down

0 comments on commit 277977b

Please sign in to comment.