Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the sx prop to work with statically defined variables #296

Closed
ahtcx opened this issue Nov 1, 2024 · 2 comments
Closed

Allow the sx prop to work with statically defined variables #296

ahtcx opened this issue Nov 1, 2024 · 2 comments

Comments

@ahtcx
Copy link

ahtcx commented Nov 1, 2024

Summary

It seems that currently CSS objects must be defined inline for the sx prop to be picked up on and the styles statically extracted.

Ideally we would have the option to define static CSS object variables which can be provided to the sx prop.

Examples

<div sx={{ background: "blue" }} />
// ✅ this correctly renders as `<div className="..."></div>`

const elementCss = { background: "blue" };
<div sx={elementCss} />
// ❌ currently renders as `<div sx="[object Object]"></div>`
// ✅ ideally would render as `<div className="..."></div>`

Potentially if needed styles could require a css branding function to make it easy for the compiler to pick up on?

const elementCss = css({ background: "blue" });
<div sx={elementCss} />

Motivation

I have a large codebase using @emotion/react with their css prop and I have a lot of code very similar to my example below. Allowing this would enable simple migrations away from @emotion/react by substituting the css prop for the sx prop. Excited to see where this library goes, it looks great!

const rootCss = (theme) => css({ margin: theme.spacing(1) });

const rootByColor = {
  red: css({ background: "red" }),
  blue: css({ background: "blue" }),
};

function App(props) {
  return <div sx={[rootCss, rootByColor[props.color]]} />;
}

Search keywords: sx

@ahtcx ahtcx added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 1, 2024
@ahtcx
Copy link
Author

ahtcx commented Nov 2, 2024

Sorry about that, I'm now realising that the already exported css function works exactly as I'm describing above. 🤦

@ahtcx ahtcx closed this as completed Nov 2, 2024
@github-actions github-actions bot removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 2, 2024
Copy link

github-actions bot commented Nov 2, 2024

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@ahtcx How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant