Rudra
Where to use JS to update CSS variables
Hi! I'm using CSS variables to set my tailwind colors dynamically - I'm fetching the colors from the API and updating the CSS variables with the values I get back. I'm currently storing the values I get back in a persisted Pinia store, however, the CSS variables get set back to default after refreshing/leaving the page, so I need to run
document.documentElement.style.setProperty('--my-color-var', valueFromApi)
when that happens.
Would it be correct to run this setProperty inside a middleware? Or is there a better way to do this?4 replies