Rework theme manager.
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.
This commit is contained in:
parent
33d5328055
commit
94de73c1a5
1 changed files with 24 additions and 36 deletions
|
|
@ -11,7 +11,7 @@ const base = import.meta.env.BASE_URL;
|
|||
---
|
||||
|
||||
<!doctype html>
|
||||
<html lang="en" class="df">
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
|
@ -20,7 +20,7 @@ const base = import.meta.env.BASE_URL;
|
|||
<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>
|
||||
<script>
|
||||
function hasLocalStorage() {
|
||||
try {
|
||||
var ls = localStorage;
|
||||
|
|
@ -37,41 +37,24 @@ function hasLocalStorage() {
|
|||
);
|
||||
}
|
||||
}
|
||||
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';
|
||||
btnThemeState: string = 'light';
|
||||
exclude: string[] = ['df'];
|
||||
ls: boolean = false;
|
||||
|
||||
constructor() {
|
||||
this.toggleClass();
|
||||
if ('theme' in localStorage && localStorage.theme !== 'undefined') {
|
||||
this.btnState = localStorage.theme;
|
||||
this.ls = hasLocalStorage();
|
||||
this.toggleThemeClass();
|
||||
if (this.ls && ('theme' in localStorage && localStorage.theme !== 'undefined')) {
|
||||
this.btnThemeState = localStorage.theme;
|
||||
} else {
|
||||
this.btnState = 'system';
|
||||
this.btnThemeState = 'system';
|
||||
}
|
||||
this.elements = document.querySelectorAll('[data-toggle-theme]');
|
||||
this.elements.forEach((e) => {
|
||||
this.setState(e, this.btnState);
|
||||
this.setThemeState(e, this.btnThemeState);
|
||||
e.addEventListener('click', this);
|
||||
});
|
||||
}
|
||||
|
|
@ -79,16 +62,16 @@ class ThemeToggler {
|
|||
handleEvent() {
|
||||
const state = localStorage.theme;
|
||||
this.elements.forEach((e) => {
|
||||
this.setNextState(e, state);
|
||||
const s = this.getState(e);
|
||||
this.setNextTheme(e, state);
|
||||
const s = this.getThemeState(e);
|
||||
if (s != localStorage.theme) {
|
||||
localStorage.theme = this.getState(e);
|
||||
localStorage.theme = this.getThemeState(e);
|
||||
}
|
||||
});
|
||||
this.toggleClass();
|
||||
this.toggleThemeClass();
|
||||
}
|
||||
|
||||
getState(element: Element): string {
|
||||
getThemeState(element: Element): string {
|
||||
const attr = element.getAttribute('data-toggle-theme');
|
||||
return attr != null ? attr : 'light';
|
||||
}
|
||||
|
|
@ -97,11 +80,11 @@ class ThemeToggler {
|
|||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||
}
|
||||
|
||||
setState(element: Element, state: string) {
|
||||
setThemeState(element: Element, state: string) {
|
||||
element.setAttribute('data-toggle-theme', state);
|
||||
}
|
||||
|
||||
setNextState(element: Element, state: string) {
|
||||
setNextTheme(element: Element, state: string) {
|
||||
const states = ['light', 'dark', 'system'];
|
||||
let next = states.indexOf(state) + 1;
|
||||
if (next === states.length) {
|
||||
|
|
@ -114,8 +97,13 @@ class ThemeToggler {
|
|||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||
}
|
||||
|
||||
toggleClass() {
|
||||
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');
|
||||
|
|
|
|||
Loading…
Reference in a new issue