Added is:inline styling script to remove FOUC.
This commit is contained in:
parent
0d81d9a63d
commit
cc38ede6ce
1 changed files with 36 additions and 0 deletions
|
|
@ -18,6 +18,42 @@ const base = import.meta.env.BASE_URL;
|
|||
<link rel="icon" href={urlfrag(base, 'favicon.png')} />
|
||||
<meta name="generator" content={Astro.generator} />
|
||||
<title>{title}</title>
|
||||
<script is:inline>
|
||||
function hasLocalStorage() {
|
||||
try {
|
||||
var ls = localStorage;
|
||||
var key = "__test";
|
||||
ls.setItem(key, key);
|
||||
ls.removeItem(key);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return (
|
||||
e instanceof DOMException &&
|
||||
e.name === "QuotaExceededError" &&
|
||||
localStorage &&
|
||||
localStorage.length !== 0
|
||||
);
|
||||
}
|
||||
}
|
||||
var theme = 'light';
|
||||
if (hasLocalStorage()) {
|
||||
theme = (('theme' in localStorage) && localStorage.theme !== 'undefined') ? localStorage.theme : 'light';
|
||||
}
|
||||
switch (theme) {
|
||||
case 'light':
|
||||
document.documentElement.classList.remove('dark');
|
||||
break
|
||||
case 'dark':
|
||||
document.documentElement.classList.toggle('dark', true);
|
||||
break
|
||||
default:
|
||||
if (matchMedia('(prefers-color-scheme: dark').matches) {
|
||||
document.documentElement.classList.toggle('dark', true);
|
||||
} else {
|
||||
document.documentElement.classList.remove('dark');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script>
|
||||
|
||||
class ThemeToggler {
|
||||
|
|
|
|||
Loading…
Reference in a new issue