mirror of
https://github.com/mastermindzh/rickvanlieshout.com
synced 2024-12-26 14:59:14 +01:00
31 lines
898 B
TypeScript
31 lines
898 B
TypeScript
import { RenderBodyArgs } from "gatsby";
|
|
import React from "react";
|
|
|
|
const setColorTheme = `
|
|
(function() {
|
|
const mode = localStorage.getItem('theme');
|
|
if (mode !== null && ['light', 'dark'].includes(mode)) {
|
|
document.documentElement.dataset.theme = mode;
|
|
return;
|
|
}
|
|
|
|
const preferredColorScheme = window.matchMedia('(prefers-color-scheme: dark)');
|
|
const hasMediaQueryPreference = typeof preferredColorScheme.matches === 'boolean';
|
|
if (hasMediaQueryPreference && preferredColorScheme.matches === true) {
|
|
document.documentElement.dataset.theme = 'dark';
|
|
} else {
|
|
document.documentElement.dataset.theme = 'light'
|
|
}
|
|
})();
|
|
`;
|
|
export const onRenderBody = ({ setPreBodyComponents }: RenderBodyArgs) => {
|
|
setPreBodyComponents([
|
|
React.createElement("script", {
|
|
key: "theme",
|
|
dangerouslySetInnerHTML: {
|
|
__html: setColorTheme,
|
|
},
|
|
}),
|
|
]);
|
|
};
|