This feature, combined with support for the prefers-color-scheme media query makes it extremely easy to create custom themes that respond to the selected theme in the host application.
I’m not sure if you can set these variables outside of @media styles like:
@media (prefers-color-scheme: dark), (prefers-color-scheme: darkest) {
//add your CSS styles here
}
As far as I can tell, setProperty doesn’t seem to be working w/ CSS variables. @amandah This is something that should be updated in the docs – can you create a task for that? @pkrishna This should probably be added to our backlog to address.
In the meantime you could have a <style> tag that you modified the text content to update the CSS variables. That seems to work, whereas setting the property directly doesn’t seem to communicate all the way through to trigger a visual update.
Getting property values back is a little more challenging – some elements should work OK if you use getComputedStyle, but Spectrum UXP elements won’t always pass those through. For example, you could have a div, assign the --uxp-host-text-color value to the text color, and query it back… but this is likely asking for trouble (I think you’ll have to wait a frame before the values become stable).
In short: better to handle the colors in CSS and not use JS to read or manipulate them directly. If you need to change the value, modify the stylesheet as you would an HTML element rather than manipulating the individual rules.
All that said, it’d be good to know why you want to modify the colors via JS – what feature are you unlocking with this?
I have a color swatch that I use to represent the color applied to a selected object such as a shape or text. Or if nothing is selected the foreground color selected in the document. Setting a css var seemed like a more modern way of going about this as I can just the same color on multiple items (fills, strokes, text, background-color) than setting the color directly on each item.
I can work around this however I am finding that the foreground color of the doc is not 100% the same as the color shown in the picker.
I posted about this but have not seen any response.