Skip to content

Commit 26081eb

Browse files
committed
Merge pull request mui#797 from SimonDegraeve/theme-decorator-hoc
Add "Theme" high order component and "theme" decorator
2 parents 011e2eb + 8861a9e commit 26081eb

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = {
4848
Snackbar: require('./snackbar'),
4949
Tab: require('./tabs/tab'),
5050
Tabs: require('./tabs/tabs'),
51+
Theme: require('./theme'),
5152
Toggle: require('./toggle'),
5253
TimePicker: require('./time-picker'),
5354
TextField: require('./text-field'),

src/theme.jsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var React = require('react');
2+
var ThemeManager = require('./styles/theme-manager');
3+
4+
5+
var Theme = React.createClass({
6+
7+
propTypes: {
8+
theme: React.PropTypes.object
9+
},
10+
11+
childContextTypes: {
12+
muiTheme: React.PropTypes.object.isRequired,
13+
muiThemeManager: React.PropTypes.object.isRequired
14+
},
15+
16+
getChildContext: function() {
17+
return {
18+
muiTheme: this.themeManager.getCurrentTheme(),
19+
muiThemeManager: this.themeManager
20+
};
21+
},
22+
23+
componentWillMount: function() {
24+
this.themeManager = new ThemeManager();
25+
26+
if (this.props.theme) {
27+
this.themeManager.setTheme(this.props.theme);
28+
}
29+
},
30+
31+
render: function() {
32+
return this.props.children({
33+
muiTheme: this.themeManager.getCurrentTheme(),
34+
muiThemeManager: this.themeManager
35+
});
36+
}
37+
});
38+
39+
40+
function getDisplayName(Component) {
41+
return Component.displayName || Component.name || 'Component';
42+
}
43+
44+
function theme(customTheme) {
45+
return function(Component) {
46+
return React.createClass({
47+
48+
displayName: 'Theme(' + getDisplayName(Component) + ')',
49+
50+
render: function() {
51+
return (
52+
<Theme theme={customTheme}>
53+
{
54+
function(props) {
55+
return (<Component {...this.props} {...props}/>);
56+
}.bind(this)
57+
}
58+
</Theme>
59+
);
60+
}
61+
});
62+
}
63+
}
64+
65+
module.exports = Theme;
66+
module.exports.theme = theme;

0 commit comments

Comments
 (0)