Added 3-way toggle with system default.
This commit is contained in:
parent
60212fa9c7
commit
01aaf43229
2 changed files with 99 additions and 27 deletions
|
|
@ -19,26 +19,96 @@ const base = import.meta.env.BASE_URL;
|
|||
<meta name="generator" content={Astro.generator} />
|
||||
<title>{title}</title>
|
||||
<script>
|
||||
document.documentElement.classList.toggle(
|
||||
"dark",
|
||||
localStorage.theme === "dark" ||
|
||||
(!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches),
|
||||
);
|
||||
const toggle = document.querySelectorAll('[data-toggle-theme]');
|
||||
toggle.forEach((b) => {
|
||||
b.addEventListener('click', () => {
|
||||
console.log(b.attributes);
|
||||
const theme: string = document.documentElement.classList.contains('dark') ? 'dark' : 'light';
|
||||
if (theme === 'undefined' || theme === '' || theme === 'light') {
|
||||
document.documentElement.classList.toggle('dark');
|
||||
localStorage.theme = 'dark';
|
||||
} else {
|
||||
document.documentElement.classList.remove('dark');
|
||||
localStorage.theme = 'light';
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
const sysTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||
switch (localStorage.theme) {
|
||||
case 'dark':
|
||||
document.documentElement.classList.add('dark');
|
||||
break;
|
||||
case 'light':
|
||||
document.documentElement.classList.remove('dark');
|
||||
break;
|
||||
default:
|
||||
if (sysTheme === 'dark') {
|
||||
document.documentElement.classList.add('dark');
|
||||
} else {
|
||||
document.documentElement.classList.remove('dark');
|
||||
}
|
||||
}
|
||||
|
||||
class ThemeToggler {
|
||||
elements: NodeListOf<Element>;
|
||||
btnState: string = 'light';
|
||||
|
||||
constructor() {
|
||||
this.toggleClass();
|
||||
if ('theme' in localStorage) {
|
||||
this.btnState = localStorage.theme;
|
||||
}
|
||||
this.elements = document.querySelectorAll('[data-toggle-theme]');
|
||||
this.elements.forEach((e) => {
|
||||
this.setState(e, this.btnState);
|
||||
e.addEventListener('click', this);
|
||||
});
|
||||
}
|
||||
|
||||
handleEvent() {
|
||||
const state = localStorage.theme;
|
||||
this.elements.forEach((e) => {
|
||||
this.setNextState(e, state);
|
||||
const s = this.getState(e);
|
||||
console.log(s);
|
||||
if (s != localStorage.theme) {
|
||||
localStorage.theme = this.getState(e);
|
||||
}
|
||||
});
|
||||
this.toggleClass();
|
||||
}
|
||||
|
||||
getState(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';
|
||||
}
|
||||
|
||||
setState(element: Element, state: string) {
|
||||
element.setAttribute('data-toggle-theme', state);
|
||||
}
|
||||
|
||||
setNextState(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';
|
||||
}
|
||||
|
||||
toggleClass() {
|
||||
const ele = document.documentElement;
|
||||
console.log("toggle:", localStorage.theme);
|
||||
switch (localStorage.theme) {
|
||||
case 'light':
|
||||
console.log('classList', ele.classList);
|
||||
ele.classList.remove('dark');
|
||||
break;
|
||||
case 'dark':
|
||||
console.log('classList', ele.classList);
|
||||
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">
|
||||
|
|
|
|||
Loading…
Reference in a new issue