1 import React from 'react'
2 import NavItems from './nav-items'
3 import {FULL_HEADER_HEIGHT} from '../constants'
4
5 import * as styles from './sidebar.module.css'
6
7 function usePersistentScroll(id) {
8 const ref = React.useRef()
9
10 const handleScroll = React.useCallback(
11 // Save scroll position in session storage on every scroll change
12 event => window.sessionStorage.setItem(id, event.target.scrollTop),
13 [id],
14 )
15
16 React.useLayoutEffect(() => {
17 // Restore scroll position when component mounts
18 const scrollPosition = window.sessionStorage.getItem(id)
19 if (scrollPosition && ref.current) {
20 ref.current.scrollTop = scrollPosition
21 }
22 }, [id])
23
24 // Return props to spread onto the scroll container
25 return {
26 ref,
27 onScroll: handleScroll,
28 }
29 }
30
31 const Sidebar = () => (
32 <nav
33 style={{
34 top: `${FULL_HEADER_HEIGHT}px`,
35 height: `calc(100vh - ${FULL_HEADER_HEIGHT}px)`,
36 }}
37 className={styles.Box}
38 >
39 <div {...usePersistentScroll('sidebar')} className={styles.Box_1}>
40 <div className={styles.Box_2}>
41 <NavItems />
42 </div>
43 </div>
44 </nav>
45 )
46
47 export default Sidebar