You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
994 B
JavaScript
35 lines
994 B
JavaScript
class ThemeSwitcher {
|
|
constructor() {
|
|
this.cb = document.getElementById('theme-cb');
|
|
this.current = localStorage.getItem('cmux-remote-theme') || 'auto';
|
|
|
|
this.apply();
|
|
if (this.cb) this.cb.addEventListener('change', () => this.toggle());
|
|
}
|
|
|
|
toggle() {
|
|
const resolved = this.getResolved();
|
|
this.current = resolved === 'dark' ? 'light' : 'dark';
|
|
localStorage.setItem('cmux-remote-theme', this.current);
|
|
this.apply();
|
|
}
|
|
|
|
getResolved() {
|
|
if (this.current === 'auto') {
|
|
return window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
|
|
}
|
|
return this.current;
|
|
}
|
|
|
|
apply() {
|
|
const theme = this.getResolved();
|
|
document.documentElement.setAttribute('data-theme', theme);
|
|
if (this.cb) this.cb.checked = (theme === 'dark');
|
|
|
|
const meta = document.querySelector('meta[name="theme-color"]');
|
|
if (meta) meta.content = theme === 'dark' ? '#0c0c14' : '#f8f9fc';
|
|
}
|
|
}
|
|
|
|
window.ThemeSwitcher = ThemeSwitcher;
|