-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-ssr.js
49 lines (43 loc) · 1.42 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
const React = require("react");
const { RecoilRoot } = require("recoil");
const HtmlAttributes = {
lang: "kr",
};
const SetColorModeBeforeRendering = () => {
let codeToRunOnClient = `
(function() {
function getInitialColorMode() {
const isClient = typeof window !== 'undefined';
if (isClient) {
const persistedColorPreference = window.localStorage.getItem('color-mode');
if (persistedColorPreference) {
return persistedColorPreference;
}
const systemPreference = window.matchMedia('(prefers-color-scheme: dark)');
if (systemPreference.matches) {
return 'dark';
}
return 'light';
}
}
const colorMode = getInitialColorMode();
document.documentElement.style.setProperty(
'background-color',
colorMode === 'light' ? '#FFFFFF' : '#1C1C1e'
);
})()`;
// eslint-disable-next-line react/no-danger
return <script dangerouslySetInnerHTML={{ __html: codeToRunOnClient }} />;
};
exports.onRenderBody = (
{ setHtmlAttributes, setPreBodyComponents },
pluginOptions
) => {
setHtmlAttributes(HtmlAttributes);
setPreBodyComponents(
<SetColorModeBeforeRendering key="SetColorModeBeforeRendering" />
);
};
exports.wrapRootElement = ({ element }) => {
return <RecoilRoot>{element}</RecoilRoot>;
};