-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathgatsby-ssr.js
More file actions
79 lines (67 loc) · 2.16 KB
/
Copy pathgatsby-ssr.js
File metadata and controls
79 lines (67 loc) · 2.16 KB
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
import { wrapPageElement } from './gatsby-shared'
const themeInitScript = `
void function() {
var STORAGE_KEY = 'theme-mode';
var EVENT_NAME = 'theme-modechange';
function isExplicit(v) { return v === 'light' || v === 'dark'; }
function getSystemTheme() {
try { return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; }
catch(e) { return 'light'; }
}
function resolve(mode) { return isExplicit(mode) ? mode : getSystemTheme(); }
function setTheme(mode) {
var resolved = resolve(mode);
document.body.classList.remove('light-mode', 'dark-mode');
document.body.classList.add(resolved + '-mode');
document.body.dataset.themeMode = mode;
window.__themeMode = mode;
window.__resolvedTheme = resolved;
window.dispatchEvent(new CustomEvent(EVENT_NAME, { detail: { mode: mode, resolved: resolved } }));
}
function persist(mode) {
try {
if (isExplicit(mode)) localStorage.setItem(STORAGE_KEY, mode);
else localStorage.removeItem(STORAGE_KEY);
} catch(e) {}
}
window.__setThemeMode = function(mode) { persist(mode); setTheme(mode); };
var stored = null;
try { stored = localStorage.getItem(STORAGE_KEY); } catch(e) {}
var mode = (stored === 'light' || stored === 'dark' || stored === 'system') ? stored : 'system';
setTheme(mode);
try {
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function() {
if (!isExplicit(window.__themeMode)) setTheme(window.__themeMode);
});
} catch(e) {}
}();
`
export const onRenderBody = ({ setHeadComponents, setPreBodyComponents }) => {
const preloadFonts = [
'brandon_reg',
'brandon_reg_it',
'brandon_med',
'brandon_bld'
]
setHeadComponents(
preloadFonts.map(name => (
<link
key={`preload-${name}`}
rel="preload"
href={`/fonts/${name}.woff2`}
as="font"
type="font/woff2"
crossOrigin="anonymous"
/>
))
)
setPreBodyComponents([
<script
key="theme-mode-init"
id="theme-mode-init"
data-uc-allowed="true"
dangerouslySetInnerHTML={{ __html: themeInitScript }}
/>
])
}
export { wrapPageElement }