Skip to content

Strange behavior when using stylex.defineVars on previously defined vars #870

Answered by nmn
zaydek asked this question in Q&A
Discussion options

You must be logged in to vote

I think your question is missing the most important part of the equation which is what happens when you theme the variables.

This should work:

const vars = stylex.defineVars({
  foo: ...,
});
const derivedVars = stylex.defineVars({
  bar: `... ${vars.foo}`,
});

But it's important to understand that if you use a theme (or even stylex.create) to override vars.foo, derivedVars.bar will not reflect that new value. This is just how CSS variables work.

However, you can make it work if you reset derivedVars every time you override a value in vars.

You can do it like so:

const _theme1 = stylex.createTheme(vars, {
  foo: '<some new color>',
});
const resetDerivedVars = stylex.createTheme(derivedVars,

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@zaydek
Comment options

@zaydek
Comment options

@nmn
Comment options

nmn Jan 21, 2025
Collaborator

Answer selected by zaydek
@zaydek
Comment options

@nmn
Comment options

nmn Jan 22, 2025
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants