Skip to content

Commit

Permalink
Merge pull request #797 from SimonDegraeve/theme-decorator-hoc
Browse files Browse the repository at this point in the history
Add "Theme" high order component and "theme" decorator
  • Loading branch information
Hai Nguyen committed Jun 9, 2015
2 parents 011e2eb + 8861a9e commit 26081eb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = {
Snackbar: require('./snackbar'),
Tab: require('./tabs/tab'),
Tabs: require('./tabs/tabs'),
Theme: require('./theme'),
Toggle: require('./toggle'),
TimePicker: require('./time-picker'),
TextField: require('./text-field'),
Expand Down
66 changes: 66 additions & 0 deletions src/theme.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var React = require('react');
var ThemeManager = require('./styles/theme-manager');


var Theme = React.createClass({

propTypes: {
theme: React.PropTypes.object
},

childContextTypes: {
muiTheme: React.PropTypes.object.isRequired,
muiThemeManager: React.PropTypes.object.isRequired
},

getChildContext: function() {
return {
muiTheme: this.themeManager.getCurrentTheme(),
muiThemeManager: this.themeManager
};
},

componentWillMount: function() {
this.themeManager = new ThemeManager();

if (this.props.theme) {
this.themeManager.setTheme(this.props.theme);
}
},

render: function() {
return this.props.children({
muiTheme: this.themeManager.getCurrentTheme(),
muiThemeManager: this.themeManager
});
}
});


function getDisplayName(Component) {
return Component.displayName || Component.name || 'Component';
}

function theme(customTheme) {
return function(Component) {
return React.createClass({

displayName: 'Theme(' + getDisplayName(Component) + ')',

render: function() {
return (
<Theme theme={customTheme}>
{
function(props) {
return (<Component {...this.props} {...props}/>);
}.bind(this)
}
</Theme>
);
}
});
}
}

module.exports = Theme;
module.exports.theme = theme;

0 comments on commit 26081eb

Please sign in to comment.