question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[system] `styled` should accept sx syntax

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary 💡

styled and sx should accept the same set of props.

Examples 🌈

const WrapperPaper = styled(Paper)({
  my: 2,
  mx: "auto",
  padding: 2,
  minWidth: "600px",
});
// is different from
<Paper sx={{ my: 2, mx: "auto", padding: 2, minWidth: "600px" }}>

The WrapperPaper component doesn’t seem to apply the shortcuts image

TypeScript accepts those mx and my props though in the styled call, they are simply not translated afterward. The sx version works fine, but leads to code duplication in my use case.

Motivation 🔦

I’d expect both to have the same API.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
mnajdovacommented, Nov 22, 2021

I don’t have to get the theme and pass it to the sx function again. I expect that this should work.

Will need to check to see how this could work.

Also, what I suggest should work with all of these scenarios:

I would try to keep the API to minimum, I would say the first and last APIs are enough.

Is the second option possible?

Yes, but there would be the conflict between the sx prop and the sx function, we would need to override it which can be surprising. What’s wrong with importing a module? Otherwise we need to again invent some new API. With the import is clear what is expected - I am preprocessing the input of the styled() utility.

1reaction
eric-burelcommented, Sep 30, 2021

An sx function that behaves like a mix between styled and sx prop could be defined like this:

// hoc with syntax similar to styled but accepting sx styles
const sx = (C) => (sxStyle) => (props) => {
   return <C sx={typeof sxStyle === "function" ? sxStyle(props) : sxStyle} {...props} />
}
// usage
const StyledFooBasic= sx(Foo)({ mx: 1, backgroundColor: "red", padding: theme => theme.spacing(1) })
) 
const StyledFooWithProps = sx(Foo)(
  props => ({ mx: 1, backgroundColor: props.myCustomColor, padding: theme => theme.spacing(1) })
) 

That’s very draftish and lacks typings, but I think this could be done already in user-land. I haven’t yet switched my main project to mui v5 but I might give this a shot later on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[system] `styled` should accept sx syntax · Issue #28678 - GitHub
I think it seems possible to accept sx directly in styled but the only concern is about performance gain.
Read more >
The sx prop - MUI System
The sx prop lets you work with a superset of CSS that packages all of the style functions exposed in @mui/system . You...
Read more >
The `sx` Prop - Theme UI
The sx prop lets you add any valid CSS to an element, while using values from your theme to keep styles consistent. You...
Read more >
The Ultimate Guide to the MUI 'sx' Prop - Smart Devpreneur
Superset of CSS – sx can accept all CSS attributes (using JSS syntax) and it can accept all values from the “MUI System“....
Read more >
What is the purpose of sx prop in Material UI? - Stack Overflow
The sx props allows you to use all the mui system properties along with CSS properites. with style ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found