-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-ssr.js
41 lines (34 loc) · 1.08 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
import React from 'react';
export const onRenderBody = ({ setPreBodyComponents }) => {
setPreBodyComponents([
React.createElement('script', {
key: 0,
dangerouslySetInnerHTML: {
__html: `
(() => {
let preferredTheme;
try {
preferredTheme = localStorage.getItem('color-mode');
} catch (err) { }
const setTheme = (newTheme) => {
window.__theme = newTheme;
preferredTheme = newTheme;
document.body.className = newTheme;
}
window.__setPreferredTheme = (newTheme) => {
setTheme(newTheme);
try {
localStorage.setItem('color-mode', newTheme);
} catch (err) {}
}
let darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
darkQuery.addListener(function (e) {
window.__setPreferredTheme(e.matches ? 'dark' : 'light')
})
setTheme(preferredTheme || (darkQuery.matches ? 'dark' : 'light'));
})();
`,
},
}),
]);
};