-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-ssr.js
51 lines (46 loc) · 1.84 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
import React from 'react'
import { COLORS } from './src/components/utils'
const MagicScriptTag = () => {
const codeToRunOnClient = `
(function() {
function getInitialColorMode() {
const persistedColorPreference = window.localStorage.getItem('color-mode')
const hasPersistedPreference = typeof persistedColorPreference === 'string'
if (hasPersistedPreference) {
return persistedColorPreference
}
const mql = window.matchMedia('(prefers-color-scheme: dark)')
const hasMediaQueryPreference = typeof mql.matches === 'boolean'
if (hasMediaQueryPreference) {
return mql.matches ? 'dark' : 'light'
}
return 'dark'
}
const colorMode = getInitialColorMode();
const root = document.documentElement;
root.style.setProperty(
'--background',
colorMode === 'light'
? '${COLORS.light.background}'
: '${COLORS.dark.background}'
);
root.style.setProperty(
'--font-color',
colorMode === 'light'
? '${COLORS.light.font}'
: '${COLORS.dark.font}'
);
root.style.setProperty(
'--hover-color',
colorMode === 'light'
? '${COLORS.light.hover}'
: '${COLORS.dark.hover}'
);
root.style.setProperty('--initial-color-mode', colorMode);
})()`
// eslint-disable-next-line react/no-danger
return <script dangerouslySetInnerHTML={{ __html: codeToRunOnClient }} />
}
export const onRenderBody = ({ setPreBodyComponents }) => {
setPreBodyComponents(<MagicScriptTag />)
}