27 lines
655 B
JavaScript
27 lines
655 B
JavaScript
|
|
import { getTheme, setTheme } from './db.js';
|
||
|
|
|
||
|
|
const THEMES = ['auto', 'dark', 'light'];
|
||
|
|
|
||
|
|
function applyTheme(value) {
|
||
|
|
const root = document.documentElement;
|
||
|
|
if (value === 'auto') root.removeAttribute('data-theme');
|
||
|
|
else root.setAttribute('data-theme', value);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function initTheme() {
|
||
|
|
const t = await getTheme();
|
||
|
|
applyTheme(t || 'auto');
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function cycleTheme() {
|
||
|
|
const cur = (await getTheme()) || 'auto';
|
||
|
|
const next = THEMES[(THEMES.indexOf(cur) + 1) % THEMES.length];
|
||
|
|
await setTheme(next);
|
||
|
|
applyTheme(next);
|
||
|
|
return next;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function currentTheme() {
|
||
|
|
return (await getTheme()) || 'auto';
|
||
|
|
}
|