@cap3/capitano-theme
Version:
Extensible and flexible theme system
46 lines • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// @flow
const React = require("react");
const prop_types_1 = require("prop-types");
exports.emotionContextChannel = "__EMOTION_THEMING__";
exports.emotionContextTypes = {
[exports.emotionContextChannel]: prop_types_1.object.isRequired,
};
class EmotionThemeConsumer extends React.Component {
constructor() {
super(...arguments);
this.unsubscribeId = -1;
this.state = {};
}
componentDidMount() {
const themeContext = this.context[exports.emotionContextChannel];
if (themeContext === undefined) {
// eslint-disable-next-line no-console
console.error("[withTheme] Please use ThemeProvider to be able to use withTheme");
return;
}
this.unsubscribeId = themeContext.subscribe(theme => {
this.setState({ theme });
});
}
componentWillUnmount() {
const themeContext = this.context[exports.emotionContextChannel];
if (this.unsubscribeId !== -1 && themeContext) {
themeContext.unsubscribe(this.unsubscribeId);
}
}
render() {
const { children } = this.props;
const { theme } = this.state;
if (children && theme) {
return children(theme);
}
else {
return null;
}
}
}
EmotionThemeConsumer.contextTypes = exports.emotionContextTypes;
exports.EmotionThemeConsumer = EmotionThemeConsumer;
//# sourceMappingURL=EmotionThemeConsumer.js.map