This is eventually going to handle themes and accessibility visuals. The bare theme script has been removed as this doesn't do anything to mitigate the FUOC.
129 lines
No EOL
3.1 KiB
Text
129 lines
No EOL
3.1 KiB
Text
---
|
|
//import "../global.css";
|
|
import Header from "../components/Header.astro";
|
|
import Footer from "../components/Footer.astro";
|
|
import Meta from "../components/Meta.astro";
|
|
|
|
import { urlfrag } from "../utils/path.js";
|
|
|
|
const { title, image } = 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" />
|
|
<meta name="theme-color" content="#28303c" />
|
|
<Meta title={title} image={image} />
|
|
<link rel="icon" type="image/svg+xml" href={urlfrag(base, 'favicon.svg')} />
|
|
<link rel="icon" href={urlfrag(base, 'favicon.png')} />
|
|
<title>{title}</title>
|
|
<script>
|
|
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
|
|
);
|
|
}
|
|
}
|
|
|
|
class ThemeToggler {
|
|
elements: NodeListOf<Element>;
|
|
btnThemeState: string = 'light';
|
|
exclude: string[] = ['df'];
|
|
ls: boolean = false;
|
|
|
|
constructor() {
|
|
this.ls = hasLocalStorage();
|
|
this.toggleThemeClass();
|
|
if (this.ls && ('theme' in localStorage && localStorage.theme !== 'undefined')) {
|
|
this.btnThemeState = localStorage.theme;
|
|
} else {
|
|
this.btnThemeState = 'system';
|
|
}
|
|
this.elements = document.querySelectorAll('[data-toggle-theme]');
|
|
this.elements.forEach((e) => {
|
|
this.setThemeState(e, this.btnThemeState);
|
|
e.addEventListener('click', this);
|
|
});
|
|
}
|
|
|
|
handleEvent() {
|
|
const state = localStorage.theme;
|
|
this.elements.forEach((e) => {
|
|
this.setNextTheme(e, state);
|
|
const s = this.getThemeState(e);
|
|
if (s != localStorage.theme) {
|
|
localStorage.theme = this.getThemeState(e);
|
|
}
|
|
});
|
|
this.toggleThemeClass();
|
|
}
|
|
|
|
getThemeState(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';
|
|
}
|
|
|
|
setThemeState(element: Element, state: string) {
|
|
element.setAttribute('data-toggle-theme', state);
|
|
}
|
|
|
|
setNextTheme(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';
|
|
}
|
|
|
|
toggleThemeClass() {
|
|
const ele = document.documentElement;
|
|
for (const e of this.exclude) {
|
|
if (ele.classList.contains(e)) {
|
|
return;
|
|
}
|
|
}
|
|
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> |