[web-php] master: Make theme toggle switch between system and opposite of system
| From: | Theodore Brown via Derick Rethans | Date: | Wed, 18 Nov 2026 18:10:34 +0000 |
| Subject: | [web-php] master: Make theme toggle switch between system and opposite of system | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-34216@lists.php.net to get a copy of this message | ||
Author: Theodore Brown (theodorejb)
Committer: Derick Rethans (derickr)
Date: 2026-11-18T18:10:27Z
Commit: https://github.com/php/web-php/commit/1ddae545668d384ab57bb3fb8ecf3c17065564c7
Raw diff: https://github.com/php/web-php/commit/1ddae545668d384ab57bb3fb8ecf3c17065564c7.diff
Make theme toggle switch between system and opposite of system
Instead of cycling between light, dark, and system, the theme button now switches to the opposite of
the displayed theme. If the new theme matches the system theme, the setting is saved as system, so
every click changes the theme and a saved light or dark setting is kept even if the system theme
later changes to match it.
Changed paths:
M public/js/common.js
Diff:
diff --git a/public/js/common.js b/public/js/common.js
index 97f3e74e15..d7d9ee6a57 100644
--- a/public/js/common.js
+++ b/public/js/common.js
@@ -837,30 +837,32 @@ function getLanguage() {
});
})(jQuery);
-const savedTheme = localStorage.theme || 'system';
const darkSchemeQuery = matchMedia('(prefers-color-scheme: dark)');
-const isDark = savedTheme === 'dark' || (savedTheme === 'system' &&
darkSchemeQuery.matches);
-if (isDark) document.documentElement.classList.add('dark');
+function getSystemTheme() {
+ return darkSchemeQuery.matches ? 'dark' : 'light';
+}
+
+function getSavedTheme() {
+ return localStorage.theme || 'system';
+}
-const themeOrder = ['light', 'dark', 'system'];
+const themeIcons = ['light', 'dark', 'system'];
const btn = document.querySelector('button.js-theme-switcher');
btn?.addEventListener('click', () => {
- const current = localStorage.theme || 'system';
- const nextIndex = (themeOrder.indexOf(current) + 1) % themeOrder.length;
- const newTheme = themeOrder[nextIndex];
+ // Switch to the opposite of the displayed theme, using "system"
+ // when the new theme matches the system theme
+ const savedTheme = getSavedTheme();
+ const displayedTheme = savedTheme === 'system' ? getSystemTheme() : savedTheme;
+ const oppositeTheme = displayedTheme === 'dark' ? 'light' :
'dark';
+ const newTheme = oppositeTheme === getSystemTheme() ? 'system' : oppositeTheme;
localStorage.theme = newTheme;
applyTheme(newTheme);
});
-darkSchemeQuery.addEventListener('change', () => {
- const current = localStorage.theme || 'system';
- if (current === 'system') {
- applyTheme(current);
- }
-});
+darkSchemeQuery.addEventListener('change', () => applyTheme(getSavedTheme()));
function applyTheme(theme) {
const isDark = theme === 'dark' || (theme === 'system' &&
darkSchemeQuery.matches);
@@ -868,10 +870,10 @@ function applyTheme(theme) {
document.documentElement.classList.toggle('dark', isDark);
const icons = [btn?.querySelector('svg:nth-of-type(1)'),
btn?.querySelector('svg:nth-of-type(2)'),
btn?.querySelector('svg:nth-of-type(3)')];
- icons.forEach((icon, i) => icon?.classList.toggle('hidden', themeOrder[i] !==
theme));
+ icons.forEach((icon, i) => icon?.classList.toggle('hidden', themeIcons[i] !==
theme));
}
-applyTheme(savedTheme)
+applyTheme(getSavedTheme());
const downloads = document.querySelector('.downloads');
downloads?.addEventListener('click', function (event) {