Author: Theodore Brown (theodorejb)
Committer: Derick Rethans (derickr)
Date: 2026-11-18T18:10:27Z

Commit: 
https://github.com/php/web-php/commit/4b76361c2fde0070ee6d146538bddc7808caf8c3
Raw diff: 
https://github.com/php/web-php/commit/4b76361c2fde0070ee6d146538bddc7808caf8c3.diff

Update page theme when system color scheme changes

When the theme is set to system, listen for changes to the prefers-color-scheme 
media query so the page switches between light and dark mode without requiring 
a reload.

Changed paths:
  M  public/js/common.js


Diff:

diff --git a/public/js/common.js b/public/js/common.js
index 8219e4db2c..97f3e74e15 100644
--- a/public/js/common.js
+++ b/public/js/common.js
@@ -838,8 +838,8 @@ function getLanguage() {
 })(jQuery);
 
 const savedTheme = localStorage.theme || 'system';
-const prefersDark = matchMedia('(prefers-color-scheme: dark)').matches;
-const isDark = savedTheme === 'dark' || (savedTheme === 'system' && 
prefersDark);
+const darkSchemeQuery = matchMedia('(prefers-color-scheme: dark)');
+const isDark = savedTheme === 'dark' || (savedTheme === 'system' && 
darkSchemeQuery.matches);
 
 if (isDark) document.documentElement.classList.add('dark');
 
@@ -855,9 +855,15 @@ btn?.addEventListener('click', () => {
     applyTheme(newTheme);
 });
 
+darkSchemeQuery.addEventListener('change', () => {
+    const current = localStorage.theme || 'system';
+    if (current === 'system') {
+        applyTheme(current);
+    }
+});
+
 function applyTheme(theme) {
-    const prefersDark = matchMedia('(prefers-color-scheme: dark)').matches;
-    const isDark = theme === 'dark' || (theme === 'system' && prefersDark);
+    const isDark = theme === 'dark' || (theme === 'system' && 
darkSchemeQuery.matches);
 
     document.documentElement.classList.toggle('dark', isDark);
 

Reply via email to