-
Notifications
You must be signed in to change notification settings - Fork 13
/
gatsby-ssr.js
38 lines (30 loc) · 1.25 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
import { DataProvider, ThemeProvider } from 'gatsby-plugin-substrate';
import React from 'react';
const MagicScriptTag = () => {
const codeToRunOnClient = `
(function() {
function getInitialColorMode() {
const persistedColorPreference = localStorage.theme;
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 'light';
}
const colorMode = getInitialColorMode();
document.documentElement.classList.add(colorMode);
})()
`;
// eslint-disable-next-line react/no-danger
return <script dangerouslySetInnerHTML={{ __html: codeToRunOnClient }} />;
};
export const onRenderBody = ({ setPreBodyComponents }) => {
setPreBodyComponents(<MagicScriptTag key={1} />);
};
export const wrapRootElement = ({ element }) => <ThemeProvider>{element}</ThemeProvider>;
export const wrapPageElement = ({ element, props }) => <DataProvider props={props}>{element}</DataProvider>;