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

[system] Allow function type for sx prop #29198

Merged
merged 5 commits into from
Oct 24, 2021
Merged

Conversation

hbjORbj
Copy link
Member

@hbjORbj hbjORbj commented Oct 21, 2021

Closes #27492

This PR enables sx prop to accept a function type in addition to object type.

- export type SxProps<Theme extends object = {}> = SystemStyleObject<Theme>;
+ /**
+ * The `SxProps` can be either object or function
+ */
+ export type SxProps<Theme extends object = {}> =
+ | SystemStyleObject<Theme>
+ | ((theme: Theme) => SystemStyleObject<Theme>);

Code sandbox: https://codesandbox.io/s/basictabs-material-demo-forked-dwslp?file=/demo.js

@mui-pr-bot
Copy link

mui-pr-bot commented Oct 21, 2021

Details of bundle changes

Generated by 🚫 dangerJS against 9098874

@hbjORbj hbjORbj added package: system Specific to @mui/system new feature New feature or request labels Oct 21, 2021
Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

This does not work.

<Tab
  label="Item One"
  sx={(theme) => ({
    ...theme.typography.h6,
    p: 10,
  })}
/>

I think you have to update styleFunctionSx.js implementation a bit. Also, please add a test to verify that it works.

My suggestion for the next PR contains a lot of file changes. first commit should be the implementation change and the next commit can be the command or script running. This is easier to review. 👍

@hbjORbj
Copy link
Member Author

hbjORbj commented Oct 21, 2021

@siriwatknp Hey! I updated the code and the code sandbox.

@siriwatknp
Copy link
Member

@siriwatknp Hey! I updated the code and the code sandbox.

Nice, can you add one more test case to styleFuncionSx.test.js to test sx as a function?

@hbjORbj hbjORbj requested a review from siriwatknp October 21, 2021 14:50
Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

👍 Awesome.

@eps1lon
Copy link
Member

eps1lon commented Oct 21, 2021

Supplying a function for the sx prop instead of an object works! but gives a warning: Warning: Failed prop type: Invalid prop "sx" of type "function"

If that is the only problem, then why does this PR change the implementation?

The problem is purely about propTypes (and probably TypeScript). There was no mention that the behavior is incorrect.

@hbjORbj
Copy link
Member Author

hbjORbj commented Oct 21, 2021

@eps1lon
The following comment is from a developer in the community, but it is found to be a false statement. As @siriwatknp pointed out, theme properties like { p: 10 } are not resolved without changing the implementation. I will edit the description.

Supplying a function for the sx prop instead of an object works! but gives a warning: Warning: Failed prop type: Invalid prop "sx" of type "function"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request package: system Specific to @mui/system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[system] sx prop supplied as a function of theme causes a warning
4 participants