kanidm/server/core/static/style.js
George Wu a3358828a8
Add support for prefers-color-scheme using Bootstrap classes. (#3327)
* Add support for prefers-color-scheme using Bootstrap classes.
* Move stylesheet changes to separate javascript file.
* fix(html): don't specify the integrity hash in the tag for style.js
* fix(log): debug-log integrity hashes for troubleshooting
* fix(css): move to using bootstrap standard variables for colours and theming
* fix(js): rewrite to simplify and use standard bootstrap functionality
* fix(makefile): codespell thingie was complaining
* run prettier on css/js.

---------

Co-authored-by: James Hodgkinson <james@terminaloutcomes.com>
2025-01-06 10:58:42 +00:00

20 lines
797 B
JavaScript

/** Queries the user's preferred colour scheme and returns the appropriate value.
From https://getbootstrap.com/docs/5.3/customize/color-modes/#javascript
*/
function getPreferredTheme() {
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}
/** Sets the theme.
*/
function updateColourScheme() {
const theme = getPreferredTheme();
console.debug(`updateColourScheme theme->${theme}`);
document.documentElement.setAttribute("data-bs-theme", theme);
}
updateColourScheme();
window.matchMedia("(prefers-color-scheme: light)").addEventListener("change", updateColourScheme);
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", updateColourScheme);
document.body.addEventListener("htmx:afterOnLoad", updateColourScheme);