138 lines
No EOL
3.3 KiB
Text
138 lines
No EOL
3.3 KiB
Text
---
|
|
//import "../global.css";
|
|
import Header from "../components/Header.astro";
|
|
import Footer from "../components/Footer.astro";
|
|
|
|
import { urlfrag } from "../utils/path.js";
|
|
|
|
const { title } = Astro.props;
|
|
const base = import.meta.env.BASE_URL;
|
|
---
|
|
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<link rel="icon" type="image/svg+xml" href={urlfrag(base, 'favicon.svg')} />
|
|
<link rel="icon" href={urlfrag(base, 'favicon.png')} />
|
|
<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 {
|
|
elements: NodeListOf<Element>;
|
|
btnState: string = 'light';
|
|
|
|
constructor() {
|
|
this.toggleClass();
|
|
if ('theme' in localStorage && localStorage.theme !== 'undefined') {
|
|
this.btnState = localStorage.theme;
|
|
} else {
|
|
this.btnState = 'system';
|
|
}
|
|
this.elements = document.querySelectorAll('[data-toggle-theme]');
|
|
this.elements.forEach((e) => {
|
|
this.setState(e, this.btnState);
|
|
e.addEventListener('click', this);
|
|
});
|
|
}
|
|
|
|
handleEvent() {
|
|
const state = localStorage.theme;
|
|
this.elements.forEach((e) => {
|
|
this.setNextState(e, state);
|
|
const s = this.getState(e);
|
|
if (s != localStorage.theme) {
|
|
localStorage.theme = this.getState(e);
|
|
}
|
|
});
|
|
this.toggleClass();
|
|
}
|
|
|
|
getState(element: Element): string {
|
|
const attr = element.getAttribute('data-toggle-theme');
|
|
return attr != null ? attr : 'light';
|
|
}
|
|
|
|
getSysTheme(): string {
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
}
|
|
|
|
setState(element: Element, state: string) {
|
|
element.setAttribute('data-toggle-theme', state);
|
|
}
|
|
|
|
setNextState(element: Element, state: string) {
|
|
const states = ['light', 'dark', 'system'];
|
|
let next = states.indexOf(state) + 1;
|
|
if (next === states.length) {
|
|
next = 0;
|
|
}
|
|
element.setAttribute('data-toggle-theme', states[next]);
|
|
}
|
|
|
|
sysTheme(): string {
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
}
|
|
|
|
toggleClass() {
|
|
const ele = document.documentElement;
|
|
switch (localStorage.theme) {
|
|
case 'light':
|
|
ele.classList.remove('dark');
|
|
break;
|
|
case 'dark':
|
|
ele.classList.add('dark');
|
|
break;
|
|
default:
|
|
ele.classList.toggle('dark', this.getSysTheme() === 'dark');
|
|
}
|
|
}
|
|
}
|
|
new ThemeToggler();
|
|
</script>
|
|
</head>
|
|
<body class="flex flex-col min-h-screen">
|
|
<Header />
|
|
<main class="grow">
|
|
<slot />
|
|
</main>
|
|
<Footer />
|
|
</body>
|
|
</html> |