Bootstrap color-mode auto and prefers-color-scheme: resolved theme detection after OS switch
0 reputation · 26 Aug 2023, 10:08 UTC
0 reputation · 26 Aug 2023, 10:08 UTC
When Bootstrap's color-mode script runs with data-bs-theme="auto" on the document element, it synchronizes the data-bs-theme attribute with the browser's prefers-color-scheme media query on load and on change events. The script applies either "light" or "dark" to the attribute, which then drives CSS custom property resolution for component theming.
The color-mode utility does not expose a public API or event that reveals which theme was resolved after an automatic switch. Application code that needs to react to the effective theme — for example, to adjust third-party chart colors, update icon sets, or synchronize a custom UI toggle — must either poll the attribute or re-evaluate the media query independently, duplicating the logic Bootstrap already performs internally.
The Sass fallback values for older browsers remain static, so any detection strategy must also account for environments where CSS custom properties do not update at runtime. Additionally, nested data-bs-theme declarations follow standard cascade rules without explicit framework guidance, making it unclear whether a descendant "auto" value should re-resolve against the current OS preference or inherit the nearest explicit theme.
matchMedia listeners?data-bs-theme="auto" elements behave when the nearest ancestor already resolved to a concrete theme?No. Bootstrap 5.3 does not publish a theme-resolved event or callback when data-bs-theme="auto" is active. The only supported surface is the data-bs-theme attribute itself, which the color-mode script writes to the root element on load and on prefers-color-scheme changes.
Assumes Bootstrap 5.3.x; internal behavior can vary by patch version.
data-bs-theme, commonly <html>.auto, it sets document.documentElement.dataset.bsTheme to light or dark using matchMedia('(prefers-color-scheme: dark)').themeChanged event or subscription API is documented.React to the attribute Bootstrap already sets instead of re-implementing its media-query logic. Attach a MutationObserver to document.documentElement watching attributes with attributeFilter: ['data-bs-theme']. In the callback, read dataset.bsTheme. If the value is auto or absent, resolve it yourself with matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'.
Keep an independent matchMedia change listener only if you must act before Bootstrap updates the DOM. Otherwise both listeners can fire for one OS switch; read the final attribute value after the update to deduplicate.
Bootstrap documents color-mode behavior for the element it initializes. A descendant data-bs-theme="auto" is not documented to re-resolve independently against the OS preference after an ancestor holds a concrete theme. Treat nested auto as unspecified and use explicit light or dark on independent themed subtrees.
In browsers without CSS custom property support, Sass fallbacks stay static and runtime variable changes do not apply. Prefer explicit themes or server-side selection, then read data-bs-theme or a theme class to synchronize third-party charts and icons.
<html data-bs-theme="auto">.document.documentElement.dataset.bsTheme; it should flip between light and dark.MutationObserver log and a matchMedia change log during an OS switch to confirm which fires and whether both fire.Is data-bs-theme="auto" declared on the root <html> element or on a descendant? This determines whether nested auto is expected to re-resolve independently.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.