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