mirror of
https://github.com/Xevion/icons.git
synced 2025-12-06 03:15:24 -06:00
Attempt to improve darkmode FOUC with direct script tag in head
This commit is contained in:
@@ -1,15 +1,16 @@
|
|||||||
const valid = ['system', 'dark', 'light'];
|
const valid = ['system', 'dark', 'light'];
|
||||||
|
|
||||||
console.log(localStorage.theme);
|
|
||||||
// Set the default to system, ignore & delete invalid values
|
// Set the default to system, ignore & delete invalid values
|
||||||
if (!('theme' in localStorage) || valid.indexOf(localStorage.theme) === -1)
|
if (!('theme' in localStorage) || valid.indexOf(localStorage.theme) === -1)
|
||||||
localStorage.theme = 'system';
|
localStorage.theme = 'system';
|
||||||
|
|
||||||
if (localStorage.theme === 'system') {
|
// Process the theme extracted
|
||||||
|
const currentTheme = localStorage.theme;
|
||||||
|
if (currentTheme === 'system') {
|
||||||
const preferDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
const preferDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||||
document.documentElement.classList.add(preferDark ? 'dark' : 'light');
|
document.documentElement.classList.add(preferDark ? 'dark' : 'light');
|
||||||
} else if (localStorage.theme === 'dark') {
|
} else if (currentTheme === 'dark') {
|
||||||
document.documentElement.classList.add('dark')
|
document.documentElement.classList.add('dark')
|
||||||
} else if (localStorage.theme === 'light') {
|
} else if (currentTheme === 'light') {
|
||||||
document.documentElement.classList.remove('dark')
|
document.documentElement.classList.remove('dark')
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,9 @@ import Script from 'next/script'
|
|||||||
export default function Document() {
|
export default function Document() {
|
||||||
return (
|
return (
|
||||||
<Html>
|
<Html>
|
||||||
<Head />
|
<Head>
|
||||||
|
<script src="/js/darkmode.js" />
|
||||||
|
</Head>
|
||||||
<body>
|
<body>
|
||||||
<Main/>
|
<Main/>
|
||||||
<NextScript/>
|
<NextScript/>
|
||||||
|
|||||||
Reference in New Issue
Block a user