Move theme to a separate file.
This commit is contained in:
parent
7de41b6892
commit
4a4f1f0cc0
2 changed files with 110 additions and 109 deletions
|
|
@ -21,115 +21,8 @@ const base = import.meta.env.BASE_URL;
|
||||||
<link rel="icon" href={urlfrag(base, 'favicon.png')} />
|
<link rel="icon" href={urlfrag(base, 'favicon.png')} />
|
||||||
<title>{title}</title>
|
<title>{title}</title>
|
||||||
<script>
|
<script>
|
||||||
function hasLocalStorage() {
|
import { theme } from '../utils/theme.ts';
|
||||||
try {
|
theme();
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (localStorage.af_enabled === '1') {
|
|
||||||
if (localStorage.af_mode === 'df') {
|
|
||||||
ele.classList.remove('dark');
|
|
||||||
ele.classList.remove('hc');
|
|
||||||
ele.classList.add('df');
|
|
||||||
}
|
|
||||||
if (localStorage.af_mode === 'hc') {
|
|
||||||
ele.classList.remove('dark');
|
|
||||||
ele.classList.remove('df');
|
|
||||||
ele.classList.add('hc');
|
|
||||||
}
|
|
||||||
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>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body class="flex flex-col min-h-screen">
|
<body class="flex flex-col min-h-screen">
|
||||||
|
|
|
||||||
108
src/utils/theme.ts
Normal file
108
src/utils/theme.ts
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
function hasLocalStorage(): boolean {
|
||||||
|
try {
|
||||||
|
const ls: Storage = localStorage;
|
||||||
|
const key: string = "__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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleThemeClass() {
|
||||||
|
const ele = document.documentElement;
|
||||||
|
for (const e of this.exclude) {
|
||||||
|
if (ele.classList.contains(e)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (localStorage.af_enabled === '1') {
|
||||||
|
if (localStorage.af_mode === 'df') {
|
||||||
|
ele.classList.remove('dark');
|
||||||
|
ele.classList.remove('hc');
|
||||||
|
ele.classList.add('df');
|
||||||
|
}
|
||||||
|
if (localStorage.af_mode === 'hc') {
|
||||||
|
ele.classList.remove('dark');
|
||||||
|
ele.classList.remove('df');
|
||||||
|
ele.classList.add('hc');
|
||||||
|
}
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const theme = () => {
|
||||||
|
new ThemeToggler();
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue