aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen Akkerman <[email protected]>2023-05-26 06:18:49 +0200
committerGitHub <[email protected]>2023-05-25 21:18:49 -0700
commitf0be063c9792f6fd123d933335efd0d8cc7f6f1f (patch)
tree7ecbf3595a38132961d6a3101a4a6d303a9b43a8
parentde6b9a7933a9187101204e1e1f90a532ff61237b (diff)
downloadbootstrap-f0be063c9792f6fd123d933335efd0d8cc7f6f1f.tar.xz
bootstrap-f0be063c9792f6fd123d933335efd0d8cc7f6f1f.zip
Update color-modes.js (#38626)
* Update color-modes.js Fix IF statement in the prefer-color-scheme change listener always evaluating to `true` and changing the theme to "dark" even when "light" is set as the preferred theme. | `||` | `x !== "light"` | `x !== "dark"` | Result | |--|:--:|:--:|:--:| | `x = "light"` | ○ | ● | ● | | `x = "dark"` | ● | ○ | ● | | `x = "auto"` | ● | ● | ● | | `x = "bogus"` | ● | ● | ● | <hr> | `&&` | `x !== "light"` | `x !== "dark"` | Result | |--|:--:|:--:|:--:| | `x = "light"` | ○ | ● | ○ | | `x = "dark"` | ● | ○ | ○ | | `x = "auto"` | ● | ● | ● | | `x = "bogus"` | ● | ● | ● | * Implement re-read of stored theme
-rw-r--r--site/static/docs/5.3/assets/js/color-modes.js9
1 files changed, 6 insertions, 3 deletions
diff --git a/site/static/docs/5.3/assets/js/color-modes.js b/site/static/docs/5.3/assets/js/color-modes.js
index 4528ba36b..32cbaa174 100644
--- a/site/static/docs/5.3/assets/js/color-modes.js
+++ b/site/static/docs/5.3/assets/js/color-modes.js
@@ -7,9 +7,11 @@
(() => {
'use strict'
- const storedTheme = localStorage.getItem('theme')
+ const getStoredTheme = () => localStorage.getItem('theme')
+ const setStoredTheme = theme => localStorage.setItem('theme', theme)
const getPreferredTheme = () => {
+ const storedTheme = getStoredTheme()
if (storedTheme) {
return storedTheme
}
@@ -17,7 +19,7 @@
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}
- const setTheme = function (theme) {
+ const setTheme = theme => {
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-bs-theme', 'dark')
} else {
@@ -56,6 +58,7 @@
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
+ const storedTheme = getStoredTheme()
if (storedTheme !== 'light' || storedTheme !== 'dark') {
setTheme(getPreferredTheme())
}
@@ -68,7 +71,7 @@
.forEach(toggle => {
toggle.addEventListener('click', () => {
const theme = toggle.getAttribute('data-bs-theme-value')
- localStorage.setItem('theme', theme)
+ setStoredTheme(theme)
setTheme(theme)
showActiveTheme(theme, true)
})