/
header.js
127 lines (121 loc) · 2.52 KB
/
header.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
import React from "react"
import { Link } from "gatsby"
import { css, useColorMode, Styled } from "theme-ui"
import Switch from "./switch"
import Bio from "../components/bio"
import sun from "../../assets/sun.png"
import moon from "../../assets/moon.png"
const rootPath = `${__PATH_PREFIX__}/`
const Title = ({ children, location }) => {
if (location.pathname === rootPath) {
return (
<Styled.h1
css={css({
my: 0,
fontSize: 4,
})}
>
<Styled.a
as={Link}
css={{
color: `inherit`,
boxShadow: `none`,
textDecoration: `none`,
}}
to={`/`}
>
{children}
</Styled.a>
</Styled.h1>
)
} else {
return (
<Styled.h3
as="p"
css={css({
my: 0,
})}
>
<Styled.a
as={Link}
css={css({
boxShadow: `none`,
textDecoration: `none`,
color: `primary`,
})}
to={`/`}
>
{children}
</Styled.a>
</Styled.h3>
)
}
}
const checkedIcon = (
<img
alt="moon indicating dark mode"
src={moon}
width="16"
height="16"
role="presentation"
css={{
pointerEvents: `none`,
margin: 4,
}}
/>
)
const uncheckedIcon = (
<img
alt="sun indicating light mode"
src={sun}
width="16"
height="16"
role="presentation"
css={{
pointerEvents: `none`,
margin: 4,
}}
/>
)
export default ({ children, title, ...props }) => {
const [colorMode, setColorMode] = useColorMode()
const isDark = colorMode === `dark`
const toggleColorMode = e => {
setColorMode(isDark ? `light` : `dark`)
}
return (
<header>
<div
css={css({
maxWidth: `container`,
mx: `auto`,
px: 3,
pt: 4,
})}
>
<div
css={css({
display: `flex`,
justifyContent: `space-between`,
alignItems: `baseline`,
mb: 4,
})}
>
<Title {...props}>{title}</Title>
{children}
<Switch
aria-label="Toggle dark mode"
css={css({
bg: `black`,
})}
checkedIcon={checkedIcon}
uncheckedIcon={uncheckedIcon}
checked={isDark}
onChange={toggleColorMode}
/>
</div>
{props.location.pathname === rootPath && <Bio />}
</div>
</header>
)
}