-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathnav-drawer.js
128 lines (123 loc) · 3.58 KB
/
nav-drawer.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React from 'react'
import {Button, Box} from '@primer/react'
import {XIcon, ThreeBarsIcon} from '@primer/octicons-react'
import NavItems from './nav-items'
import {useIsMobile} from '../hooks/use-breakpoint'
import {DarkTheme, LightTheme} from '../theme'
import {AnimatePresence, motion} from 'framer-motion'
import {FocusOn} from 'react-focus-on'
import {HEADER_BAR, HEADER_HEIGHT} from '../constants'
import SiteTitle from './site-title'
const Drawer = ({isOpen, onDismiss, children}) => (
<AnimatePresence>
{isOpen ? (
// These event handlers fix a bug that caused links below the fold
// to be unclickable in macOS Safari.
// Reference: https://github.com/theKashey/react-focus-lock/issues/79
<Box
onMouseDown={event => event.preventDefault()}
onKeyDown={event => event.target.focus()}
onClick={event => event.target.focus()}
role="button"
tabIndex="0"
>
<FocusOn returnFocus={true} onEscapeKey={onDismiss}>
<Box
sx={{
position: 'fixed',
top: 0,
right: 0,
bottom: 0,
left: 0,
bg: 'overlay.backdrop',
}}
key="overlay"
as={motion.div}
initial={{opacity: 0}}
animate={{opacity: 1}}
exit={{opacity: 0}}
transition={{type: 'tween'}}
onClick={onDismiss}
/>
<Box
sx={{
position: 'fixed',
top: `${HEADER_BAR}px`,
right: 0,
bottom: 0,
width: 300,
zIndex: 1,
}}
key="drawer"
as={motion.div}
initial={{x: '100%'}}
animate={{x: 0}}
exit={{x: '100%'}}
transition={{type: 'tween', duration: 0.2}}
>
{children}
</Box>
</FocusOn>
</Box>
) : null}
</AnimatePresence>
)
function NavDrawer() {
const isMobile = useIsMobile()
const [open, setOpen] = React.useState(false)
React.useEffect(() => {
if (!isMobile && open) {
setOpen(false)
}
}, [isMobile, open])
return (
<>
<Button aria-label="Menu" aria-expanded={open} onClick={() => setOpen(true)} sx={{ml: 3}}>
<ThreeBarsIcon />
</Button>
<LightTheme as={Drawer} isOpen={open} onDismiss={() => setOpen(false)}>
<Box
sx={{
display: 'flex',
flexDirection: 'column',
height: '100%',
bg: 'canvas.backdrop',
overflow: 'auto',
}}
style={{WebkitOverflowScrolling: 'touch'}}
>
<Box
sx={{
display: 'flex',
flexDirection: 'column',
flex: '1 0 auto',
color: 'fg.default',
bg: 'canvas.default',
}}
>
<DarkTheme
sx={{
color: 'fg.default',
bg: 'canvas.default',
height: `${HEADER_HEIGHT}px`,
px: 3,
alignItems: 'center',
justifyContent: 'space-between',
display: 'flex',
}}
>
<SiteTitle />
<Button aria-label="Close" onClick={() => setOpen(false)}>
<XIcon />
</Button>
</DarkTheme>
<Box sx={{display: 'flex', flexDirection: 'column'}}>
<NavItems />
</Box>
</Box>
</Box>
</LightTheme>
</>
)
}
export default NavDrawer