Is it possible to use a callback to listen to CSS variable changes? something like this:
documentElement.addListener('css-var-main-background-color', () => {
console.log('Value changed');
});
CSS variables have access to the DOM, which means that you can change them with JavaScript.
The var() CSS function can be used to insert the value of a custom property (sometimes called a "CSS variable") instead of any part of a value of another property.
CSS variables are currently supported for 93 percent of users globally. If a browser doesn't support CSS variables, it also doesn't understand the var() function, and doesn't know what its second argument means. Instead, we need to use the cascade, as we do for every new CSS feature.
If you have full control over the code and can set CSS variables via the style
attribute on a DOM element rather than using a stylesheet, you can use MutationObserver
to detect changes on that element's style attribute and thus detect changes to the variable.
The variables must be set like this:
document.documentElement.style.setProperty('--var1', '1');
and NOT like this:
:root {
--var1: 1;
}
Then you can monitor changes to the style attribute of the target element.
let value = '1';
const styleObserver = new MutationObserver((mutations) => {
const currentValue = mutations[0].target.style.getPropertyValue('--var1');
if (currentValue !== value) {
// the variable has changed
value = currentValue;
}
});
styleObserver.observe(document.documentElement, {
attributes: true,
attributeFilter: ['style'],
});
The above example is for the html element which is usually where you put global variables, but it can work with any other element.
It is possible if you are willing to define the stylesheet in a specific way:
<style></style>
elementThen you can use MutationObserver
to watch changes to the style element and parse its contents to detect which variables have changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With