forked from IMY772-2022/via-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-ssr.js
191 lines (149 loc) · 5.03 KB
/
gatsby-ssr.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
import React from "react"
import {
COLORS,
COLOR_MODE_KEY,
FONT_SIZES,
FONT_SIZE_MODE_KEY,
FONT_WEIGHTS,
FONT_WEIGHT_MODE_KEY,
INITIAL_COLOR_MODE_CSS_PROP,
INITIAL_FONT_SIZE_MODE_CSS_PROP,
INITIAL_FONT_WEIGHT_MODE_CSS_PROP,
} from "./src/theme"
const setColorsByTheme = () => {
const colors = "🌈"
const colorModeKey = "🔑"
const colorModeCssProp = "⚡️"
const mql = window.matchMedia("(prefers-color-scheme: dark)")
const prefersDarkFromMQ = mql.matches
const persistedPreference = localStorage.getItem(colorModeKey)
let colorMode = "light"
const hasUsedToggle = typeof persistedPreference === "string"
if (hasUsedToggle) {
colorMode = persistedPreference
} else {
colorMode = prefersDarkFromMQ ? "dark" : "light"
}
let root = document.documentElement
root.style.setProperty(colorModeCssProp, colorMode)
Object.entries(colors).forEach(([name, colorByTheme]) => {
const cssVarName = `--color-${name}`
root.style.setProperty(cssVarName, colorByTheme[colorMode])
})
}
const setFontWeightModeByTheme = () => {
const fontWeights = "🌈"
const fontWeightModeKey = "🔑"
const fontWeightModeCssProp = "⚡️"
const persistedPreference = localStorage.getItem(fontWeightModeKey)
const hasUsedToggle = typeof persistedPreference === "string"
const fontWeightMode = hasUsedToggle ? persistedPreference : "normal"
let root = document.documentElement
root.style.setProperty(fontWeightModeCssProp, fontWeightMode)
Object.entries(fontWeights).forEach(([name, weightByTheme]) => {
const cssVarName = `--weight-${name}`
root.style.setProperty(cssVarName, weightByTheme[fontWeightMode])
})
}
const setFontSizeModeByTheme = () => {
const fontSizes = "🌈"
const fontSizeModeKey = "🔑"
const fontSizeModeCssProp = "⚡️"
const persistedPreference = localStorage.getItem(fontSizeModeKey)
const hasUsedToggle = typeof persistedPreference === "string"
const fontSizeMode = hasUsedToggle ? persistedPreference : "normal"
let root = document.documentElement
root.style.setProperty(fontSizeModeCssProp, fontSizeMode)
Object.entries(fontSizes).forEach(([name, sizeByTheme]) => {
const cssVarName = `--size-${name}`
root.style.setProperty(cssVarName, sizeByTheme[fontSizeMode])
})
}
const ColorsScript = () => {
const colorsBoundFn = String(setColorsByTheme)
.replace("🌈", JSON.stringify(COLORS))
.replace("🔑", COLOR_MODE_KEY)
.replace("⚡️", INITIAL_COLOR_MODE_CSS_PROP)
const calledColorsFunction = `(${colorsBoundFn})()`
// eslint-disable-next-line react/no-danger
return (
<script
dangerouslySetInnerHTML={{
__html: calledColorsFunction,
}}
/>
)
}
const FontWeightsScript = () => {
const weightsBoundFn = String(setFontWeightModeByTheme)
.replace("'🌈'", JSON.stringify(FONT_WEIGHTS))
.replace("🔑", FONT_WEIGHT_MODE_KEY)
.replace("⚡️", INITIAL_FONT_WEIGHT_MODE_CSS_PROP)
const calledWeightsFunction = `(${weightsBoundFn})()`
// eslint-disable-next-line react/no-danger
return (
<script
dangerouslySetInnerHTML={{
__html: calledWeightsFunction,
}}
/>
)
}
const FontSizesScript = () => {
const sizesBoundFn = String(setFontSizeModeByTheme)
.replace("'🌈'", JSON.stringify(FONT_SIZES))
.replace("🔑", FONT_SIZE_MODE_KEY)
.replace("⚡️", INITIAL_FONT_SIZE_MODE_CSS_PROP)
const calledSizesFunction = `(${sizesBoundFn})()`
// eslint-disable-next-line react/no-danger
return (
<script
dangerouslySetInnerHTML={{
__html: calledSizesFunction,
}}
/>
)
}
/**
* If the user has JS disabled, the injected script will never fire!
* This means that they won't have any colors set, everything will be default
* black and white.
* We can solve for this by injecting a `<style>` tag into the head of the
* document, which sets default values for all of our colors.
* Only light mode will be available for users with JS disabled.
*/
const FallbackStyles = () => {
// Create a string holding each CSS variable:
/*
`--color-text: black;
--color-background: white;`
*/
const colorsCssVariableString = Object.entries(COLORS).reduce(
(acc, [name, colorByTheme]) => {
return `${acc}\n--color-${name}: ${colorByTheme.light};`
},
""
)
const weightsCssVariableString = Object.entries(FONT_WEIGHTS).reduce(
(acc, [name, weightByTheme]) => {
return `${acc}\n--weight-${name}: ${weightByTheme.normal};`
},
""
)
const sizesCssVariableString = Object.entries(FONT_SIZES).reduce(
(acc, [name, sizeByTheme]) => {
return `${acc}\n--size-${name}: ${sizeByTheme.normal};`
},
""
)
const wrappedInSelector = `html { ${colorsCssVariableString} ${weightsCssVariableString} ${sizesCssVariableString} }`
return <style>{wrappedInSelector}</style>
}
export const onRenderBody = ({ setPreBodyComponents, setHeadComponents }) => {
setHeadComponents(<FallbackStyles />)
setPreBodyComponents(
<ColorsScript />,
<FontWeightsScript />,
<FontSizesScript />
)
}