1 import {Box} from '@primer/react'
2 import React from 'react'
3 import NavItems from './nav-items'
4 import {FULL_HEADER_HEIGHT} from '../constants'
5
6 function usePersistentScroll(id) {
7 const ref = React.useRef()
8
9 const handleScroll = React.useCallback(
10 // Save scroll position in session storage on every scroll change
11 event => window.sessionStorage.setItem(id, event.target.scrollTop),
12 [id],
13 )
14
15 React.useLayoutEffect(() => {
16 // Restore scroll position when component mounts
17 const scrollPosition = window.sessionStorage.getItem(id)
18 if (scrollPosition && ref.current) {
19 ref.current.scrollTop = scrollPosition
20 }
21 }, [id])
22
23 // Return props to spread onto the scroll container
24 return {
25 ref,
26 onScroll: handleScroll,
27 }
28 }
29
30 const Sidebar = () => (
31 <Box
32 role="navigation"
33 sx={{
34 position: 'sticky',
35 top: `${FULL_HEADER_HEIGHT}px`,
36 height: `calc(100vh - ${FULL_HEADER_HEIGHT}px)`,
37 width: 270,
38 }}
39 >
40 <Box
41 {...usePersistentScroll('sidebar')}
42 sx={{
43 overflow: 'auto',
44 borderWidth: 0,
45 borderRightWidth: 1,
46 height: '100%',
47 borderStyle: 'solid',
48 borderColor: 'border.subtle',
49 }}
50 >
51 <Box sx={{display: 'flex', flexDirection: 'column'}} role="list">
52 <NavItems />
53 </Box>
54 </Box>
55 </Box>
56 )
57
58 export default Sidebar