-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-ssr.jsx
74 lines (66 loc) · 2.26 KB
/
gatsby-ssr.jsx
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
import React from 'react'
import Layout from './wrapPageElement'
import SSR from './wrapRootElement'
const themeScript = `
let mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
function updateTheme(savedTheme) {
let theme = 'dark'
try {
if (!savedTheme) {
savedTheme = window.localStorage.theme
}
if (savedTheme === 'dark') {
theme = 'dark'
document.documentElement.classList.add('dark')
} else if (savedTheme === 'light') {
theme = 'light'
document.documentElement.classList.remove('dark')
} else if (mediaQuery.matches) {
document.documentElement.classList.add('dark')
} else {
document.documentElement.classList.remove('dark')
}
} catch {
theme = 'light'
document.documentElement.classList.remove('dark')
}
return theme
}
function updateThemeWithoutTransitions(savedTheme) {
updateTheme(savedTheme)
document.documentElement.classList.add('[&_*]:!transition-none')
window.setTimeout(() => {
document.documentElement.classList.remove('[&_*]:!transition-none')
}, 0)
}
document.documentElement.setAttribute('data-theme', updateTheme())
new MutationObserver(([{ oldValue }]) => {
let newValue = document.documentElement.getAttribute('data-theme')
if (newValue !== oldValue) {
try {
window.localStorage.setItem('theme', newValue)
} catch {}
updateThemeWithoutTransitions(newValue)
}
}).observe(document.documentElement, { attributeFilter: ['data-theme'], attributeOldValue: true })
mediaQuery.addEventListener('change', updateThemeWithoutTransitions)
window.addEventListener('storage', updateThemeWithoutTransitions)
`;
const HtmlAttributes = {
className:'h-full antialiased scroll-smooth', lang: 'bs',
};
const HeadComponents = [
<script key='darkmode' dangerouslySetInnerHTML={{ __html: themeScript }} />,
];
const BodyAttributes = {
className: "bg-indigo-50 dark:bg-slate-900",
};
export const onRenderBody = (
{ setHtmlAttributes, setHeadComponents, setBodyAttributes }
) => {
setHtmlAttributes(HtmlAttributes);
setHeadComponents(HeadComponents);
setBodyAttributes(BodyAttributes);
};
export const wrapRootElement = SSR;
export const wrapPageElement = Layout;