Skip to content

Commit 6e8c3dc

Browse files
committed
Fix darkmode
1 parent 7b5bc3c commit 6e8c3dc

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

assets/js/scripts.js

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -201,52 +201,47 @@ if (search_form) {
201201
/**
202202
* Light / Dark mode
203203
*/
204-
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
205-
if (localStorage.theme === 'system' && event.matches) {
206-
document.documentElement.classList.add('dark');
204+
const update_theme = () => {
205+
const theme = localStorage.getItem('theme') || 'system';
206+
let current_theme = theme;
207+
208+
if (theme === 'system') {
209+
current_theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
210+
document.documentElement.setAttribute('color-theme', 'system');
207211
} else {
208-
document.documentElement.classList.remove('dark');
212+
document.documentElement.setAttribute('color-theme', theme);
209213
}
210-
});
211214

212-
const update_theme = () => {
213-
if (!('theme' in localStorage)) {
214-
localStorage.theme = 'system';
215-
}
215+
document.documentElement.classList.toggle('dark', current_theme === 'dark');
216216

217-
switch (localStorage.theme) {
218-
case 'system':
219-
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
220-
document.documentElement.classList.add('dark');
221-
} else {
222-
document.documentElement.classList.remove('dark');
223-
}
224-
break;
225-
case 'dark':
226-
document.documentElement.classList.add('dark');
227-
break;
228-
case 'light':
229-
document.documentElement.classList.remove('dark');
230-
break;
231-
}
232-
}
217+
const theme_colors = {light: '#fff', dark: '#1f2937'};
218+
document.querySelector("meta[name='theme-color']").content = theme_colors[current_theme]
219+
};
233220

234-
update_theme();
221+
const init_theme_switcher = () => {
222+
const theme_switchers = document.querySelectorAll("[data-theme]");
235223

236-
const theme_sw = document.querySelectorAll("[data-theme]");
224+
theme_switchers.forEach(button => {
225+
const theme = button.getAttribute('data-theme');
237226

238-
theme_sw.forEach(button => {
239-
let theme = button.getAttribute('data-theme');
227+
button.addEventListener('click', () => {
228+
localStorage.setItem('theme', theme);
229+
update_theme();
230+
theme_switchers.forEach(btn => btn.classList.remove('active'));
231+
button.classList.add('active');
232+
});
240233

241-
button.addEventListener('click', () => {
242-
localStorage.theme = theme;
243-
update_theme();
244-
theme_sw.forEach(btn => btn.classList.remove('active'));
234+
if (theme === localStorage.getItem('theme')) {
235+
button.classList.add('active');
236+
}
237+
});
245238

246-
button.classList.add('active');
239+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
240+
if (localStorage.getItem('theme') === 'system') {
241+
update_theme();
242+
}
247243
});
244+
};
248245

249-
if (theme === localStorage.theme) {
250-
button.classList.add('active');
251-
}
252-
});
246+
update_theme();
247+
init_theme_switcher();

templates/layout.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1">
66
<title>{{ site_title }} - phpCacheAdmin</title>
77
<link rel="icon" type="image/png" sizes="32x32" href="assets/favicon.png">
8+
<meta name="theme-color" content="#ffffff">
89
<link rel="stylesheet" href="assets/css/styles.css?v={{ version }}">
910

1011
{%- if colors -%}

0 commit comments

Comments
 (0)