Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow the system theme (when the theme is toggled to match it) #63

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions frontend/App.vue
Original file line number Diff line number Diff line change
@@ -493,9 +493,17 @@ export default defineComponent({
},

toggle_theme(): void {
this.theme = this.theme === 'light' ? 'dark' : 'light';
this.update_theme(this.theme === 'light' ? 'dark' : 'light');
},

update_theme(theme: 'light' | 'dark'): void {
this.theme = theme;
const system_theme = this.dark_system_theme.matches ? 'dark' : 'light';
try {
window.localStorage.setItem('user-theme', this.theme);
// If the user's theme now matches the system theme, continue following the
// system theme
if (this.theme === system_theme) this.store.removeItem('user-theme');
else this.store.setItem('user-theme', this.theme);
} catch (e) {}
document.documentElement.className = this.theme;
},
@@ -507,6 +515,7 @@ export default defineComponent({
"Your browser doesn't support local storage, which is required for the app to function properly. Please consider updating.",
);
}
const dark_system_theme = window.matchMedia('(prefers-color-scheme: dark)');
return {
dismissed: false,
done_searching: false,
@@ -524,11 +533,10 @@ export default defineComponent({
scope: 'en',
scroll_up: false,
store: window.localStorage || null,
dark_system_theme,
theme:
window.localStorage.getItem('user-theme') ??
(window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light'),
(dark_system_theme.matches ? 'dark' : 'light'),
token: null as string | null,
username: null as string | null,
version,
@@ -573,6 +581,10 @@ export default defineComponent({
this.navigate(decodeURIComponent(window.location.hash.substring(1))),
);
document.body.onscroll = () => this.scrape_cache();
this.dark_system_theme.addEventListener('change', () => {
if (this.store.getItem('user-theme') === null)
this.update_theme(this.dark_system_theme.matches ? 'dark' : 'light');
});
},
updated() {
if (this.scroll_up) {