/
layout.js
163 lines (155 loc) · 4.24 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
/** @jsx jsx */
import { jsx, Themed, useColorMode } from 'theme-ui'
import { useState, useRef } 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 fullwidth =
(props.pageContext.frontmatter &&
props.pageContext.frontmatter.fullwidth) ||
props.location.pathname === '/'
const showNav = !props.pageContext?.frontmatter?.hidenav
const cycleMode = (e) => {
const i = modes.indexOf(mode)
const next = modes[(i + 1) % modes.length]
setMode(next)
}
return (
<Themed.root>
<Head {...props} />
<SkipLink>Skip to content</SkipLink>
<Flex
sx={{
flexDirection: 'column',
minHeight: '100vh',
}}>
{showNav && (
<Flex
as="header"
sx={{
height: 64,
px: 3,
alignItems: 'center',
justifyContent: 'space-between',
}}>
<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
sx={{
ml: 2,
whiteSpace: 'pre',
}}
onClick={cycleMode}>
{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={props.location.pathname}
sx={{
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],
}}
/>
<main
id="content"
sx={{
width: '100%',
minWidth: 0,
maxWidth: fullwidth ? 'none' : 768,
mx: 'auto',
px: fullwidth ? 0 : 3,
}}>
{props.children}
<EditLink />
{!fullwidth && <Pagination />}
</main>
</Box>
</Flex>
</Themed.root>
)
}