/
layout.js
219 lines (204 loc) · 5.61 KB
/
layout.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/** @jsx jsx */
import { css, jsx, Themed, useColorMode } from 'theme-ui'
import { useState, useRef, useEffect } from 'react'
import { Flex, Box } from '@theme-ui/components'
import { AccordionNav } from '@theme-ui/sidenav'
import { Link } from 'gatsby'
import SkipLink from './skip-link'
import Pagination from './pagination'
import EditLink from './edit-link'
import Head from './head'
import MenuButton from './menu-button'
import NavLink from './nav-link'
import Button from './button'
import SearchInput from './search-input'
import Sidebar from '../sidebar.mdx'
const modes = ['default', 'dark', 'deep', 'swiss']
const sidebar = {
wrapper: AccordionNav,
a: NavLink,
}
const getModeName = (mode) => {
switch (mode) {
case 'dark':
return 'Dark'
case 'deep':
return 'Deep'
case 'swiss':
return 'Swiss'
case 'light':
case 'default':
return 'Light'
case undefined:
return ' '
default:
return mode
}
}
export default function DocsLayout(props) {
const [menuOpen, setMenuOpen] = useState(false)
const nav = useRef(null)
const [mode, setMode] = useColorMode()
const { pathname } = props.location
const isLanding = pathname === '/'
const fullwidth =
isLanding ||
(props.pageContext.frontmatter && props.pageContext.frontmatter.fullwidth)
const showNav = !props.pageContext?.frontmatter?.hidenav
const nextColorMode = modes[(modes.indexOf(mode) + 1) % modes.length]
return (
<Themed.root>
<Head {...props} />
<SkipLink>Skip to content</SkipLink>
<Flex
sx={{
flexDirection: 'column',
minHeight: '100vh',
}}
>
{showNav && (
<Flex
as="header"
sx={{
zIndex: 1,
height: 64,
px: 3,
alignItems: 'center',
justifyContent: 'space-between',
position: isLanding ? 'initial' : 'sticky',
top: 0,
background: 'background',
}}
>
<Flex sx={{ alignItems: 'center' }}>
<MenuButton
onClick={(e) => {
setMenuOpen(!menuOpen)
if (!nav.current) return
const navLink = nav.current.querySelector('a')
if (navLink) navLink.focus()
}}
/>
<Link to="/" sx={{ variant: 'links.nav' }}>
Theme UI
</Link>
</Flex>
<Flex sx={{ gap: [0, 2] }}>
<SearchInput />
<Flex sx={{ alignItems: 'center' }}>
<NavLink href="https://github.com/system-ui/theme-ui">
GitHub
</NavLink>
<Button
// color mode can be read from localStorage
suppressHydrationWarning
aria-label={`Change color mode to ${nextColorMode}`}
sx={{
ml: 2,
whiteSpace: 'pre',
}}
onClick={() => setMode(nextColorMode)}
>
{getModeName(mode)}
</Button>
</Flex>
</Flex>
</Flex>
)}
<Box
sx={{
flex: '1 1 auto',
alignItems: 'flex-start',
display: ['block', 'flex'],
height: '100%',
}}
>
<Sidebar
ref={nav}
role="navigation"
onFocus={(e) => {
setMenuOpen(true)
}}
onBlur={(e) => {
setMenuOpen(false)
}}
onClick={(e) => {
setMenuOpen(false)
}}
onKeyPress={(e) => {
setMenuOpen(false)
}}
open={menuOpen}
components={sidebar}
pathname={pathname}
sx={{
background: 'background',
display: [null, fullwidth ? 'none' : 'block'],
width: 256,
flex: 'none',
maxHeight: ['100%', 'calc(100vh - 64px)'],
overflowY: 'auto',
px: 3,
pt: 3,
pb: 4,
mt: [64, 0],
position: [null, 'sticky'],
top: [null, '64px'],
}}
/>
<div
sx={{
width: '100%',
minWidth: 0,
position: 'relative',
}}
>
{!isLanding && <HeaderScrollShadow />}
<main
id="content"
sx={{
maxWidth: fullwidth ? 'none' : 768,
mx: 'auto',
px: fullwidth ? 0 : 3,
}}
>
{props.children}
<EditLink />
{!fullwidth && <Pagination />}
</main>
</div>
</Box>
</Flex>
</Themed.root>
)
}
function HeaderScrollShadow() {
const ref = useRef()
useEffect(() => {
const onScroll = () => {
const { current } = ref
if (current) {
current.style.opacity = window.scrollY > 0 ? 1 : 0
}
}
window.addEventListener('scroll', onScroll)
return () => window.removeEventListener('scroll', onScroll)
}, [])
return (
<div
ref={ref}
sx={{
content: "''",
top: '64px',
transform: 'translateY(-64px)',
left: 0,
right: 0,
height: '64px',
position: 'sticky',
boxShadow: '0 12px 18px -3px rgb(0 0 0 / 0.03)',
transition: 'opacity 250ms linear',
opacity: 0,
}}
/>
)
}