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>
|
<!doctype html>
|
||||||
<html lang="en" class="df">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<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" type="image/svg+xml" href={urlfrag(base, 'favicon.svg')} />
|
||||||
<link rel="icon" href={urlfrag(base, 'favicon.png')} />
|
<link rel="icon" href={urlfrag(base, 'favicon.png')} />
|
||||||
<title>{title}</title>
|
<title>{title}</title>
|
||||||
<script is:inline>
|
<script>
|
||||||
function hasLocalStorage() {
|
function hasLocalStorage() {
|
||||||
try {
|
try {
|
||||||
var ls = localStorage;
|
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 {
|
class ThemeToggler {
|
||||||
elements: NodeListOf<Element>;
|
elements: NodeListOf<Element>;
|
||||||
btnState: string = 'light';
|
btnThemeState: string = 'light';
|
||||||
|
exclude: string[] = ['df'];
|
||||||
|
ls: boolean = false;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.toggleClass();
|
this.ls = hasLocalStorage();
|
||||||
if ('theme' in localStorage && localStorage.theme !== 'undefined') {
|
this.toggleThemeClass();
|
||||||
this.btnState = localStorage.theme;
|
if (this.ls && ('theme' in localStorage && localStorage.theme !== 'undefined')) {
|
||||||
|
this.btnThemeState = localStorage.theme;
|
||||||
} else {
|
} else {
|
||||||
this.btnState = 'system';
|
this.btnThemeState = 'system';
|
||||||
}
|
}
|
||||||
this.elements = document.querySelectorAll('[data-toggle-theme]');
|
this.elements = document.querySelectorAll('[data-toggle-theme]');
|
||||||
this.elements.forEach((e) => {
|
this.elements.forEach((e) => {
|
||||||
this.setState(e, this.btnState);
|
this.setThemeState(e, this.btnThemeState);
|
||||||
e.addEventListener('click', this);
|
e.addEventListener('click', this);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -79,16 +62,16 @@ class ThemeToggler {
|
||||||
handleEvent() {
|
handleEvent() {
|
||||||
const state = localStorage.theme;
|
const state = localStorage.theme;
|
||||||
this.elements.forEach((e) => {
|
this.elements.forEach((e) => {
|
||||||
this.setNextState(e, state);
|
this.setNextTheme(e, state);
|
||||||
const s = this.getState(e);
|
const s = this.getThemeState(e);
|
||||||
if (s != localStorage.theme) {
|
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');
|
const attr = element.getAttribute('data-toggle-theme');
|
||||||
return attr != null ? attr : 'light';
|
return attr != null ? attr : 'light';
|
||||||
}
|
}
|
||||||
|
|
@ -97,11 +80,11 @@ class ThemeToggler {
|
||||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
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);
|
element.setAttribute('data-toggle-theme', state);
|
||||||
}
|
}
|
||||||
|
|
||||||
setNextState(element: Element, state: string) {
|
setNextTheme(element: Element, state: string) {
|
||||||
const states = ['light', 'dark', 'system'];
|
const states = ['light', 'dark', 'system'];
|
||||||
let next = states.indexOf(state) + 1;
|
let next = states.indexOf(state) + 1;
|
||||||
if (next === states.length) {
|
if (next === states.length) {
|
||||||
|
|
@ -114,8 +97,13 @@ class ThemeToggler {
|
||||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleClass() {
|
toggleThemeClass() {
|
||||||
const ele = document.documentElement;
|
const ele = document.documentElement;
|
||||||
|
for (const e of this.exclude) {
|
||||||
|
if (ele.classList.contains(e)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
switch (localStorage.theme) {
|
switch (localStorage.theme) {
|
||||||
case 'light':
|
case 'light':
|
||||||
ele.classList.remove('dark');
|
ele.classList.remove('dark');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue