Files
plugin-dynamic-theme-style/assets/js/auto-style-switch.js
2025-12-14 15:17:06 +01:00

15 lines
616 B
JavaScript

/**
* Detects system/browser color scheme and sets the "style_variant" cookie accordingly.
* Reloads the page if the variant changes.
*/
(function () {
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const cookie = document.cookie.split('; ').find(r => r.startsWith('style_variant='));
const current = cookie ? cookie.split('=')[1] : '';
const desired = prefersDark ? '01-evening' : 'default'; // Customize variant names as needed
if (current !== desired) {
document.cookie = "style_variant=" + desired + "; path=/; max-age=31536000";
location.reload();
}
})();