-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[WIP] Major Theme Handling Migration #2585
Conversation
return <WrappedComponent {...props} muiTheme={muiTheme} />; | ||
}; | ||
function MuiComponent(props, {muiTheme = getMuiTheme()}) { | ||
return React.createElement(WrappedComponent, {muiTheme, ...props}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a jsx file, so I changed it to React.createElement. + I think {muiTheme, ...props} is better, allows you to override the theme through the props. might come in handy :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@subjectix Yes, this is better, thanks! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool 😁 😁
I also migrated 2 components as a demonstration of how awesome |
@subjectix I'm excited for this change 👍 ! This should help reduce a lot of the cognitive overhead in developing and maintaining components and reduce the LOC for each component too. I'll review this in more detail later and report back. Some initial thoughts:
My gut feeling is to leave it as
Should we consider using immutable.js for calculating/merging/comparing themes objects (as opposed to lodash)? I haven't given it much thought myself but I read at #1176 that defining themes as immutable structures could help improve or open up the possibility for some performance optimizations. This PR seems like a natural place to include that change if we want to do that. |
This is awesome! So much code can be removed. I was going the exact same path after reading @newoga's PR, trying to learn React at the same time. Here are the issues I found, that you might not be addressing yet, @subjectix :
My stab at it, mostly similar, with the addition of proxying public methods is at |
I didn't mean expose this to public :D I meant in some cases, maybe we want override the theme
We don't merge and update themes so much. It's no more than possibly once, per page load. and immutable.js is a big library. I use it in every project I work on. but it's not needed here in my opinion. that performance problem can be easily fixed with memoization. I'm against adding immutable.js as a dependency to this project. The themes are created once or twice per load! and the styles will be fixed with simple memoization. But I'm surely open minded to reconsider. You do have pretty good ideas 👍 👍 |
@ntgn81 Thank you for the insights. You have a point. However, we are deprecating imperative methods. So we'll re expose them for now, to provide a little patch. I really like your proxy pattern 👍 👍
I don't understand. can you please explain? what do you mean by "exposing properties"? 😁 |
Got it, I better understand what you mean now.
😆 😆
Sounds good to me. Like I said, I haven't given it too much thought and I don't have enough experience with immutable.js to make a strong case for or against it so I trust your opinion. In my opinion, the less dependencies the better. 😄 |
@ntgn81 Good catch! I hadn't considered those implications. I agree with @subjectix that long term it's better to deprecate those methods and find better and more React-like/idiomatic approaches to doing the same things. |
@subjectix , like this - https://github.com/callemall/material-ui/blob/master/src/dialog.jsx#L403, or this https://github.com/callemall/material-ui/blob/master/src/DropDownMenu/DropDownMenu.jsx#L235. doSomething() {
console.log(this.refs.comp.style);
}
render() {
<SomeMuiComponent ref="comp" />
} Not an issue if we don't have these kind of things :D |
@ntgn81 😱 😱 That looks way too hacky. I think we should never have those 😁 Thanks for the heads up! I'll keep an eye for them. P.S. The code in Dialog isn't using the |
Lol, I hoped you'd tell me there's a magical way to make it work through HOC. Glad I was useful! I'll try to dig through It should have became dark too since it's a child of DatePicker, right? |
I'm not sure what the use-case would be. I mean the props are passed down to children. If they have props they have it from your render method. and if it's in the render method you can access in much easier ways. Can you give me an example of how this would be beneficial? I might be able to find a way to make it happen.
It might be caused by some unstable react methods used to make RenderToLayer possible, maybe passing theme as child through the portal fixes this. |
Oh sorry, I was just trying to be funny. Especially with how noobish I am with React that there's something I totally missed. 😅 😅 |
@ntgn81 😄 I understand. It took me a long time to wrap my head around how React works 😁 |
Got it! Looks like Once I the getChildContext() {
return {
muiTheme: this._getMuiTheme(),
};
},
render() {
const muiTheme = this._getMuiTheme();
return React.createElement(WrappedComponent, {...this.props, muiTheme, ref} );
},
_getMuiTheme() {
return this.props.muiTheme || this.context.muiTheme || getDefaultTheme();
}, |
@ntgn81 Interesting! Thanks for sharing 👍 👍 👍 |
For sure! I learned a lot with this 😂 Applied the new decorator to everything under |
@ntgn81 Glad to hear it works. thanks 😄 |
/** | ||
* get the MUI theme corresponding to a base theme. | ||
*/ | ||
export function getMuiTheme(baseTheme) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add a second parameter called muiTheme
?
That would override some value of the outputted muiTheme
.
Right now, when we want to change some value of the outputted muiTheme
, we have to use, for instance
muiTheme.appBar = Object.assign({}, muiTheme.appBar, {
textColor: colors.white,
height: 56,
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your idea 👍 👍 yeah, I'll figure out a clean way to do this. thanks 👍
I don't really have side on this. Both approaches are fine to me.
I think that it's great. Regarding using Immutable.js, I don't think that it's a good idea. If someone need to update the
It does fix it, but it would be better to work with the context 😁 |
We'll it's not our bug 😆 Just a patch until it's fixed 😄 |
About the naming, I think at least for muiTheme it's better to use underscore. it's much more expressive. I'll go with underscore. and update the commits, tell me how you will like them. |
@subjectix That was one last comment on that file, I promise 🙏 Sorry for being nosey, I just switched my project completely to React with this. I want nothing but for this project to keep kicking more butts 🚀 |
@ntgn81 Feedbacks are always welcome, so no worries at all 👍 Thanks a lot for your insights. 👍 👍 |
@@ -31,11 +31,12 @@ | |||
"homepage": "http://material-ui.com/", | |||
"dependencies": { | |||
"inline-style-prefixer": "^0.5.2", | |||
"lodash.merge": "^3.3.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can remove this dependency now ^.^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no we need this for merging themes. assign won't help us there
@ntgn81 Your list list saved me hours! Thanks a lot 🙏 🙏 |
@oliviertassinari I don't know why, but this PR also fixed Dialog not receiving The parent theme O.O I have no idea how. but it works now in the customization page of my last commit 😆 |
@newoga @oliviertassinari @ntgn81 @mbrookes This is final with 2 minor issues:
I could fix these, But I think merging this so other works can continue takes higher priority. Also I need you guys to go through the changes and look out for regressions. Thanks 👍 🎉 |
I see all green after so long 🎉 🎉 @oliviertassinari I would rather keep the history as it is. rebasing cleanly is impossible, I can make this at most 10 clean commits which would make huge commits. That will severely hurt tracking changes if something comes up. I advice against squshing these commits. These are major changes, they should be easily tracked. But I'm open to discussion 😁 😁 Also, I haven't forgotten about Page.jsx over index.jsx. I'll do it another PR. |
That's because the <DialogInline {...this.props} />
That's going to be 70 commits, I'm not against it. I would say that you can make some commit as
🎉
I'm gonna run my app tests base on this branch |
I'm scared. 😅 I'll preface by saying that l'll go with whatever you and @oliviertassinari agree to do. Even with all of the separate commits, this branch/PR is already really tough to review because of then number of changes 😄 😄 I don't think this should all be squashed down to one commit, though I don't think we should have 70 commits either. I think if I had a choice, I would prefer if some of the core changes were implemented as a separate PRs first, then we could have have a big migration PR that pretty much changes context components to
Maybe it's too much work to extract now at this point, but those are my 2 cents! 😄 |
@newoga I agree, this would have been better with smaller PR. But since it's already done. Let's see. My custom context is not passed down correctly (I see the default one).I had to replace childContextTypes: {
muiTheme: React.PropTypes.object,
},
getChildContext() {
return {
muiTheme: muiTheme,
};
}, with childContextTypes: {
_muiTheme: React.PropTypes.object,
},
getChildContext() {
return {
_muiTheme: muiTheme,
};
}, Why did you add an underscore to the context and not just the property?
|
@newoga I feel ya 😨 😨 I'll try to figure out the best way to do this. I think your idea is good.
So they all looked the same, I can change it, won't be so hard. Want me to change it?
I can do that. Should I close this and make separate PRs?
how about the old |
I'm going to break this down into smaller PRs:
sounds good? |
This is a continuation of #2541 that handles some other issues as well. But this is incomplete I wish to migrate the entire codebase to use HOC theme wrappers written by @newoga as well (Thanks a lot ^_^). However, before doing that I would like to discuss some matters that are very essential on how this PR will continue.
Closes #2460
Do we agree on having the internal name for. Yes! A discussion on this is at Proposal: Use underscore for internal props #2580muiTheme
renamed to_muiTheme
that is, every component will eventually receive a prop called_muiTheme
I includedacceptedlodash.merge
for deep merging baseTheme. For this reason, there will be no need to writeThemeManager.getMuiTheme(DefaultRawTheme)
,ThemeManager.getMuiTheme()
does the same thing. That is because an empty object is merged with the default Theme and then with the input of this function. simplifies a lot of things. Does everyone agree on this behavior? It doesn't break anything, but rather, it prevents future regressions caused by adding props to baseThemes.TODO:
Replace Style mixin with Util functionsI will leave this for another PRAdd info on this in the documentation.Another PR@oliviertassinari @newoga @mbrookes Take a look ;)