Currently, color mode switcher displays Light or Dark icon, when no color mode has been stored in the localStorage.
The culprit is following code:
|
const getPreferredTheme = () => { |
|
const storedTheme = getStoredTheme() |
|
if (storedTheme) { |
|
return storedTheme |
|
} |
|
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' |
|
} |
:
The function getPreferredTheme converts non-stored item (null) to light or dark value on the output, Thus, wrong active color mode button is matched.
Further, handling of actual theme set in IU is splitted between the above function and following function:
|
const setTheme = theme => { |
|
if (theme === 'auto') { |
|
document.documentElement.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')) |
|
} else { |
|
document.documentElement.setAttribute('data-bs-theme', theme) |
|
} |
|
} |
The function setTheme has special case for input parameter auto. Therefore cases where there is no value stored in localStorage and when value auto has been stored are handled in a slightly different way.
Further, value auto makes sense only for the color-swicher component, as a part of element ID in [data-bs-theme-value="auto"]. In CSS only values light and dark are valid.
Further yet, the event handler:
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { |
|
const storedTheme = getStoredTheme() |
|
if (storedTheme !== 'light' && storedTheme !== 'dark') { |
|
setTheme(getPreferredTheme()) |
|
} |
|
}) |
Currently works correctly, only if there are only light and dark color modes stored in localStorage, and there are no custom color modes added by user.
Fix could be following:
- Threat
localStorage.getItem('theme') === null and localStorage.getItem('theme') === 'auto' in the same manner. Or even better, do not store any value in auto mode in localStorage.
- Use
auto suffix only for color-switcher UI handling, as a fallback value for localStorage.getItem('theme') === null.
Currently, color mode switcher displays
LightorDarkicon, when no color mode has been stored in thelocalStorage.The culprit is following code:
examples/color-modes/js/color-modes.js
Lines 13 to 20 in 6786420
The function
getPreferredThemeconverts non-stored item (null) tolightordarkvalue on the output, Thus, wrong active color mode button is matched.Further, handling of actual theme set in IU is splitted between the above function and following function:
examples/color-modes/js/color-modes.js
Lines 22 to 28 in 6786420
The function
setThemehas special case for input parameterauto. Therefore cases where there is no value stored inlocalStorageand when valueautohas been stored are handled in a slightly different way.Further, value
automakes sense only for thecolor-swichercomponent, as a part of element ID in[data-bs-theme-value="auto"]. In CSS only valueslightanddarkare valid.Further yet, the event handler:
examples/color-modes/js/color-modes.js
Lines 60 to 65 in 6786420
Currently works correctly, only if there are only
lightanddarkcolor modes stored inlocalStorage, and there are no custom color modes added by user.Fix could be following:
localStorage.getItem('theme') === nullandlocalStorage.getItem('theme') === 'auto'in the same manner. Or even better, do not store any value inautomode inlocalStorage.autosuffix only for color-switcher UI handling, as a fallback value forlocalStorage.getItem('theme') === null.