11import React from 'react'
22import { Button , Box } from '@primer/react'
33import { XIcon , ThreeBarsIcon } from '@primer/octicons-react'
4- import Link from './link'
5- import Drawer from './drawer'
64import NavItems from './nav-items'
7- import useSiteMetadata from '../hooks/use-site-metadata'
85import { useIsMobile } from '../hooks/use-breakpoint'
96import { DarkTheme , LightTheme } from '../theme'
7+ import { AnimatePresence , motion } from 'framer-motion'
8+ import { FocusOn } from 'react-focus-on'
9+ import { HEADER_BAR , HEADER_HEIGHT } from '../constants'
10+ import SiteTitle from './site-title'
1011
11- const useDrawerIsOpen = ( ) => {
12+ const Drawer = ( { isOpen, onDismiss, children} ) => (
13+ < AnimatePresence >
14+ { isOpen ? (
15+ // These event handlers fix a bug that caused links below the fold
16+ // to be unclickable in macOS Safari.
17+ // Reference: https://github.com/theKashey/react-focus-lock/issues/79
18+ < Box
19+ onMouseDown = { event => event . preventDefault ( ) }
20+ onKeyDown = { event => event . target . focus ( ) }
21+ onClick = { event => event . target . focus ( ) }
22+ role = "button"
23+ tabIndex = "0"
24+ >
25+ < FocusOn returnFocus = { true } onEscapeKey = { onDismiss } >
26+ < Box
27+ sx = { {
28+ position : 'fixed' ,
29+ top : 0 ,
30+ right : 0 ,
31+ bottom : 0 ,
32+ left : 0 ,
33+ bg : 'overlay.backdrop' ,
34+ } }
35+ key = "overlay"
36+ as = { motion . div }
37+ initial = { { opacity : 0 } }
38+ animate = { { opacity : 1 } }
39+ exit = { { opacity : 0 } }
40+ transition = { { type : 'tween' } }
41+ onClick = { onDismiss }
42+ />
43+ < Box
44+ sx = { {
45+ position : 'fixed' ,
46+ top : `${ HEADER_BAR } px` ,
47+ right : 0 ,
48+ bottom : 0 ,
49+ width : 300 ,
50+ zIndex : 1 ,
51+ } }
52+ key = "drawer"
53+ as = { motion . div }
54+ initial = { { x : '100%' } }
55+ animate = { { x : 0 } }
56+ exit = { { x : '100%' } }
57+ transition = { { type : 'tween' , duration : 0.2 } }
58+ >
59+ { children }
60+ </ Box >
61+ </ FocusOn >
62+ </ Box >
63+ ) : null }
64+ </ AnimatePresence >
65+ )
66+
67+ function NavDrawer ( ) {
1268 const isMobile = useIsMobile ( )
13- const [ isOpen , setIsOpen ] = React . useState ( false )
14- const setOpen = React . useCallback ( ( ) => setIsOpen ( true ) , [ ] )
15- const setClose = React . useCallback ( ( ) => setIsOpen ( false ) , [ ] )
69+ const [ open , setOpen ] = React . useState ( false )
1670
1771 React . useEffect ( ( ) => {
18- if ( ! isMobile && isOpen ) {
19- setIsOpen ( false )
72+ if ( ! isMobile && open ) {
73+ setOpen ( false )
2074 }
21- } , [ isMobile , isOpen ] )
22-
23- return [ isOpen , { setOpen, setClose} ]
24- }
25-
26- function NavDrawer ( ) {
27- const siteMetadata = useSiteMetadata ( )
28- const [ isOpen , { setOpen, setClose} ] = useDrawerIsOpen ( )
75+ } , [ isMobile , open ] )
2976
3077 return (
3178 < >
32- < Button aria-label = "Menu" aria-expanded = { isOpen } onClick = { setOpen } sx = { { ml : 3 } } >
79+ < Button aria-label = "Menu" aria-expanded = { open } onClick = { ( ) => setOpen ( true ) } sx = { { ml : 3 } } >
3380 < ThreeBarsIcon />
3481 </ Button >
35- < LightTheme as = { Drawer } isOpen = { isOpen } onDismiss = { setClose } >
82+ < LightTheme as = { Drawer } isOpen = { open } onDismiss = { ( ) => setOpen ( false ) } >
3683 < Box
3784 sx = { {
3885 display : 'flex' ,
@@ -54,32 +101,19 @@ function NavDrawer() {
54101 >
55102 < DarkTheme
56103 sx = { {
57- borderWidth : 0 ,
58- borderRadius : 0 ,
59- borderBottomWidth : 1 ,
60- borderColor : 'border.muted' ,
61- borderStyle : 'solid' ,
62104 color : 'fg.default' ,
63105 bg : 'canvas.default' ,
106+ height : `${ HEADER_HEIGHT } px` ,
107+ px : 3 ,
108+ alignItems : 'center' ,
109+ justifyContent : 'space-between' ,
110+ display : 'flex' ,
64111 } }
65112 >
66- < Box
67- sx = { {
68- py : 3 ,
69- pl : 4 ,
70- pr : 3 ,
71- alignItems : 'center' ,
72- justifyContent : 'space-between' ,
73- display : 'flex' ,
74- } }
75- >
76- < Link to = "/" sx = { { fontSize : 2 , color : 'fg.default' } } >
77- { siteMetadata . title }
78- </ Link >
79- < Button aria-label = "Close" onClick = { setClose } >
80- < XIcon />
81- </ Button >
82- </ Box >
113+ < SiteTitle />
114+ < Button aria-label = "Close" onClick = { ( ) => setOpen ( false ) } >
115+ < XIcon />
116+ </ Button >
83117 </ DarkTheme >
84118 < Box sx = { { display : 'flex' , flexDirection : 'column' } } >
85119 < NavItems />
0 commit comments