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

[styles] Allow CSS properties to be functions #15546

Merged
merged 18 commits into from
Jun 7, 2019

Conversation

merceyz
Copy link
Member

@merceyz merceyz commented May 1, 2019

  • Updated type definition to let CSS properties be functions that returns a value

Fixes #14814
Fixes #16007

@mui-pr-bot
Copy link

mui-pr-bot commented May 1, 2019

No bundle size changes comparing 2e78390...4b05497

Generated by 🚫 dangerJS against 4b05497

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

Can you add TS demos for https://next.material-ui.com/css-in-js/basics/#adapting-the-styled-components-api and https://next.material-ui.com/css-in-js/basics/#adapting-the-higher-order-component-api as well?

Looks like this change is required for these to work. This would mean we have a test for the change.

packages/material-ui-styles/src/withStyles/withStyles.d.ts Outdated Show resolved Hide resolved
@merceyz
Copy link
Member Author

merceyz commented May 2, 2019

Can you add TS demos for https://next.material-ui.com/css-in-js/basics/#adapting-the-styled-components-api and https://next.material-ui.com/css-in-js/basics/#adapting-the-higher-order-component-api as well?

Yes, though it's blocked by #15501 which is blocked by this PR. So the demos should probably be in their own PR

@eps1lon
Copy link
Member

eps1lon commented May 2, 2019

@merceyz Alright #15501 should be green. You can base this PR on #15501 and then we'll see if this worked.

This PR should also bring back docs/src/pages/css-in-js/basics/AdaptingHook.tsx

@merceyz
Copy link
Member Author

merceyz commented May 2, 2019

@eps1lon Argos complaining now, everything else passed though so I'll wait for #15501 to be merged and then reset

@eps1lon eps1lon self-requested a review May 3, 2019 09:28
Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

I don't think it has any impact whether we use object or {} as the base type for Props. What object tries to convey is that a plain object should be passed which we can't type check with TypeScript so we just communicate the requirement.

Rest looks awesome.

@merceyz
Copy link
Member Author

merceyz commented May 3, 2019

@eps1lon I noticed that core has it's own CSSProperties interface, tried to replace it with this one but couldn't. Can you take a look?

@eps1lon eps1lon self-requested a review May 3, 2019 10:55
@eps1lon
Copy link
Member

eps1lon commented May 3, 2019

Right. I was wondering why this was working now (I thought I tried it before) but it only works because @material-ui/styles has very few test cases. As soon as you test this approach more thoroughly it breaks.

The problem is that there are just way to many overloads and for structural types it somehow breaks down because at some point the compiler doesn't know if it is themed based or props based.

I somewhat gave up on typing every possible pattern. We support props based styling on the class key level which should cover most of the use cases. If you can make this work for the current test suite in @material-ui/core I'm happy to merge. Right now it breaks too many common usage patterns.

@eps1lon eps1lon added the PR: needs revision The pull request can't be merged. More details is available on the code review or fails in the CI label May 3, 2019
@oliviertassinari
Copy link
Member

Should we close the pull request, for now?

@oliviertassinari oliviertassinari changed the base branch from next to master May 23, 2019 21:09
@mui-pr-bot
Copy link

mui-pr-bot commented May 30, 2019

No bundle size changes comparing f246dbd...912b97f

Generated by 🚫 dangerJS against 912b97f

@merceyz
Copy link
Member Author

merceyz commented May 30, 2019

Right. I was wondering why this was working now (I thought I tried it before) but it only works because @material-ui/styles has very few test cases. As soon as you test this approach more thoroughly it breaks.

@eps1lon I didn't have time to look into this properly until now, but I think you're wrong here.
makeStyles from @material-ui/core is the exact same as the one from @material-ui/styles

As I didn't update createStyles in @material-ui/core, it only works with makeStyles

@eps1lon
Copy link
Member

eps1lon commented May 31, 2019

I didn't have time to look into this properly until now, but I think you're wrong here.
makeStyles from @material-ui/core is the exact same as the one from @material-ui/styles

About what? You can look at the repo and you can compare the amount of test in packages/material-ui/test/typescript/styles.spec vs packages/material-ui-styles/test/styles.spec and find that the core has way more test cases.

I'm not sure you're aware what is typed where currently i.e. core/styles/withStyles has a different definition compared to styles/withStyles which is why changing styles/withStyles doesn't cause breakage because it doesn't have the same tests as core/styles/withStyles.

Until we can consolidate both definitions we shouldn't make changes to the implementations because we don't have a clear picture what is tested.

@merceyz
Copy link
Member Author

merceyz commented May 31, 2019

@eps1lon You're right, my bad. Working on it now.

@merceyz
Copy link
Member Author

merceyz commented May 31, 2019

@eps1lon That should do it

@merceyz
Copy link
Member Author

merceyz commented Jun 3, 2019

@eps1lon Requesting a review

@eps1lon
Copy link
Member

eps1lon commented Jun 4, 2019

I'd like to consolidate the types and tests for mui/core and mui/core/styles first to have an overview if something is already broken between core/styles and styles. Then we can move forward.

@eps1lon eps1lon added package: material-ui Specific to @mui/material package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. and removed PR: needs revision The pull request can't be merged. More details is available on the code review or fails in the CI labels Jun 7, 2019
Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

Very nice. Took the liberty and named some remaining unnamed generic arguments when we expect a certain type. T, U etc only make sense for truly generic types e.g Pick, Omit

packages/material-ui-styles/src/withStyles/withStyles.d.ts Outdated Show resolved Hide resolved
@eps1lon eps1lon changed the title [styles] Update type definition to let CSS properties be functions [styles] Allow CSS properties to be functions Jun 7, 2019
@eps1lon
Copy link
Member

eps1lon commented Jun 7, 2019

@merceyz Could you take a look at 912b97f? Hope this improves backward compatibility.

@merceyz
Copy link
Member Author

merceyz commented Jun 7, 2019

@eps1lon Since props is optional, that should keep it backwards compatible. Should we add a comment to switch it for V5?
Otherwise looks good

@eps1lon
Copy link
Member

eps1lon commented Jun 7, 2019

It's only a minor inconvenience/unelegant code. Nothing that warrants a breaking change in the future. Maybe that'll change. Let's leave this for now and see how it goes.

@eps1lon eps1lon merged commit 557b690 into mui:master Jun 7, 2019
@merceyz merceyz deleted the styles/css-properties branch June 7, 2019 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: material-ui Specific to @mui/material package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typescript error when using dynamic props for styling styled function typespec failing
4 participants