-
Say you have something like const vars = stylex.defineVars({
foo: ...,
}) And then you realize you want to compose those vars so you don't need to repeat yourself: const vars = stylex.defineVars({
foo: ...,
});
const derivedVars = stylex.defineVars({
bar: `... ${vars.foo}`,
}); I found that doing something like this causes a lot of problems and is unpredictable. I can create a repro if necesarry but is this an unsupported pattern and if so -- how come?
const BAR = `... ${vars.foo}`;
const FOO = "--foo"
const vars = stylex.defineVars({
[FOO]: ...,
});
const derivedVars = stylex.defineVars({
bar: `... ${FOO}`,
}); Nevermind that didn't work either. I'm guessing what's happening when you create a For clarity, my use case is something like this: const themeVars = stylex.defineVars({
colorHairline: "color-mix(in hsl, transparent, white 10%)",
});
const themeVarsDerived = stylex.defineVars({
boxShadowHairline: `inset 0 0 0 1px ${themeVars.colorHairline}`,
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
OK I found the solution, it's quite subtle. Because I'm overwriting the dynamic variables to begin with, it appears StyleX needs to me to restate the derived variables too, not just the first-order variables I am references. So to be explicit, the following did not work: const styles = stylex.create({
dynamicTheme: (theme: UseThemeVarsEditorTheme) => ({
[themeVars.focusRingOffset]: `${theme.focusRingOffset.value}px`,
[themeVars.focusRingThickness]: `${theme.focusRingThickness.value}px`,
[themeVars.focusRingColorAccent]: theme.focusRingColorAccent.value,
[themeVars.colorAccent]: theme.colorAccent.value,
}),
}); And finally, this works: const styles = stylex.create({
dynamicTheme: (theme: UseThemeVarsEditorTheme) => ({
[themeVars.focusRingOffset]: `${theme.focusRingOffset.value}px`,
[themeVars.focusRingThickness]: `${theme.focusRingThickness.value}px`,
[themeVars.focusRingColorAccent]: theme.focusRingColorAccent.value,
[themeVars.colorAccent]: theme.colorAccent.value,
+ [derivedThemeVars.colorAccentHover]: `oklch(from ${theme.colorAccent.value} calc(l + 0.05) c h)`,
+ [derivedThemeVars.colorAccentActive]: `oklch(from ${theme.colorAccent.value} calc(l + 0.10) c h)`,
}),
}); I don't really understand why but I'm glad I can use the derived defineVars pattern still. |
Beta Was this translation helpful? Give feedback.
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:
But it's important to understand that if you use a theme (or even
stylex.create
) to overridevars.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 invars
.You can do it like so: