-
Can someone help me figure out why the code below is not generating any valid css classes? I get the following error message // USER_COLORS <-- array e.g. ['red', 'green', 'blue'];
const avatar_color = USER_COLORS.reduce(
(obj, item) => ({
...obj,
[item]: css`
--avatar-color: var(${vars.color[item]});
`,
}),
{} as Record<UserColors, string>
); Grateful for help :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is because ecsstatic doesn't run your entire code - it simply finds interpolated expressions and tries to resolve them using the variables available at the top level. Think of it like a slightly more powerful version of This means any variable referenced inside To fix this problem, ecsstatic would need to run your entire file (perhaps in a node VM) and then collect the style rules after all expressions are resolved. This is a slow, complicated and problematic process. I was actually looking into this a while ago and one of the problems I ran into is that the Node VM module doesn't have stable support for ESM, which is kind of a dealbreaker. If you really need to achieve this, maybe you could use I should probably also document this limitation somewhere. |
Beta Was this translation helpful? Give feedback.
This is because ecsstatic doesn't run your entire code - it simply finds interpolated expressions and tries to resolve them using the variables available at the top level. Think of it like a slightly more powerful version of
eval()
.This means any variable referenced inside
${}
must be defined at the top-level. In this case,item
comes from the function param, so it can't be evaluated.To fix this problem, ecsstatic would need to run your entire file (perhaps in a node VM) and then collect the style rules after all expressions are resolved. This is a slow, complicated and problematic process. I was actually looking into this a while ago and one of the problems I ran into is that the Node …