astroturfcss / astroturf

Better Styling through Compiling: CSS-in-JS for those that want it all.
https://astroturfcss.github.io/astroturf/
MIT License
2.28k stars 60 forks source link

Using a static value imported from a different file inside the css props doesn't work #742

Closed IgnusG closed 1 year ago

IgnusG commented 2 years ago

I know it's not possible to use static values imported from other files (only the current one), but dynamic values should be supported through the css prop:

import TOKENS from "./tokens";

function Something(props: React.HTMLAttributes<HTMLDivElement>) {
  return (
    <div
      {...props}
      css={css`
        background-color: ${TOKENS.BLUE};
      `}
    />
  );
}

This results in the following error:

./src/Something.tsx
TypeError: Cannot read properties of undefined (reading 'find')
    at async Promise.all (index 0)

If I change the lookup slightly by "artificially adding" an evaluation step it works again:

import TOKENS from "./tokens";

function Something(props: React.HTMLAttributes<HTMLDivElement>) {
  return (
    <div
      {...props}
      css={css`
        background-color: ${true && TOKENS.BLUE}; // Note the always true conditional
      `}
    />
  );
}

I think this should be relaxed such that static values that are not found in the current file are treated as dynamic values so that the workaround isn't necessary.

IgnusG commented 2 years ago

This also works:

import TOKENS from "./tokens";

function Something(props: React.HTMLAttributes<HTMLDivElement>) {
  return (
    <div
      {...props}
      css={css`
        background-color: ${`${TOKENS.BLUE}`}; // Wrapping the static value in another string
      `}
    />
  );
}
jquense commented 2 years ago

this should already be supported...we use dynamic values like this all the time with the css prop. Are you sure there isn't an issue with the token value? Would you mind putting together a reproduction i can run, since this looks more like a bug than anything.