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

[experimentalStyled] Make sx style fn optional #23714

Merged
merged 4 commits into from
Nov 26, 2020

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Nov 25, 2020

This PR adds an option for making the sx prop optional as part of the experimentalStyled utility. This could unblock developers to customize the behavior of the resolver for the sx prop, depending on their needs. A simple example of how it could be used can be found here - https://codesandbox.io/s/confident-bush-346hb?file=/src/App.js (the value for the m prop is always the negative value of the resolved one).

Discussion around this issue can be found here - #23496

@mui-pr-bot
Copy link

mui-pr-bot commented Nov 25, 2020

Details of bundle changes

Generated by 🚫 dangerJS against 978eb34

@mnajdova mnajdova marked this pull request as draft November 25, 2020 14:01
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnajdova Interesting approach. Thoughts:

  1. I think that the option to disable the sx prop is interesting for design teams that want to minimize entropy. It's not unusual to have developers that want to enforce high constraints on the exposed API. I'm not sure, however, that it's common enough yet to support it, considering a workaround is to wrap. Maybe it would even work better with an option in the theme.
  2. Instead of disabling the sx prop, what would be wrong if a developer uses a different name for the prop?
  3. In [system] transform and themeKey do not work together when using style from material-ui/system  #23496, the root of the issue for the author seems to be a missing spacing feature. Something developer can find alternatives in: tailwindcss, or all the Stack components. I start to wonder if we even need a Stack component if a spacingX and spacingY value in the system wouldn't be enough.
  4. In [system] transform and themeKey do not work together when using style from material-ui/system  #23496, the problem I was wondering about is the notion of a custom superset of CSS you can use. It might be overkill to have it in the theme as done in https://stitches.dev/docs/utils, considering we didn't yet see a developer that had this use case.

if (!skipSx) {
expressionsWithDefaultTheme.push((props) => {
const theme = isEmpty(props.theme) ? defaultTheme : props.theme;
return styleFunctionSx({ ...props, theme });
Copy link
Member

@oliviertassinari oliviertassinari Nov 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might understand why the curried API on styled-system, css() method, maybe it's so they don't need to spread the props, I wonder if it helps with performance:

Suggested change
return styleFunctionSx({ ...props, theme });
return styleFunctionSx(props)(theme);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me test this separately, wouldn't like to break something else somewhere :)))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But looks like it really may help

@mnajdova
Copy link
Member Author

I think that the option to disable the sx prop is interesting for design teams that want to minimize entropy. It's not unusual to have developers that want to enforce high constraints on the exposed API. I'm not sure, however, that it's common enough yet to support it, considering a workaround is to wrap. Maybe it would even work better with an option in the theme.

Instead of disabling the sx prop, what would be wrong if a developer uses a different name for the prop?

The biggest problem and the reason why I decided to add this option is the following - experimentalStyled appends the function for the sx prop last (it will always win), so even if you have custom styles under experimentalStyled like:

const CustomBox = experimentalStyled('div')(customSxFunction);

our default styleFunctionSx would still win as it runs last. I am trying to make an alternative for this. Using different prop sounds good, but still, I think it would be beneficial if we support this...

In #23496, the root of the issue for the author seems to be a missing spacing feature. Something developer can find alternatives in: tailwindcss, or all the Stack components. I start to wonder if we even need a Stack component if a spacingX and spacingY value in the system wouldn't be enough.

Maybe adding a gapX & gapY would solve the issue. However, I think it's better if we have Stack component dedicated to this, just to preserve the simplicity of the sx prop that we want to use everywhere. Not all components need this behavior, so I would be careful will adding new features, to be honest...

In #23496, the problem I was wondering about is the notion of a custom superset of CSS you can use. It might be overkill to have it in the theme as done in https://stitches.dev/docs/utils, considering we didn't yet see a developer that had this use case.

At this moment I wouldn't go that far. Adding custom props and handling of existing ones is pretty easy anyway as demonstrated on the codesandbox - https://codesandbox.io/s/confident-bush-346hb?file=/src/App.js I would stay away from introducing new API until we really see a value in it, otherwise, we will again introduce new abstractions that developers will need to learn...

@mnajdova mnajdova requested a review from eps1lon November 25, 2020 18:58
@mnajdova mnajdova merged commit 6112feb into mui:next Nov 26, 2020
@zannager zannager added the package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. label Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants