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ynswVFO5QkQchMBnNqcC6vqhrGn1pqvBapJPC8bwfLZEwOSw7meRckI+ScKUBZSwQbMASZwHipJ0lac1MQ6wWfiOTHvIkC7esOfpwAGXBBn1SChjA4aRppMbZu5iZICmfhmOmmbZnmwVFkgpblkglbyjWx31sZ8DNswbZwB2zDdrt+JAA
|
|
import React from 'react';
|
|
import wrapDisplayName from 'recompose/wrapDisplayName';
|
|
import type { HigherOrderComponent } from 'react-flow-types';
|
|
import createMuiTheme from './createMuiTheme';
|
|
import themeListener from './themeListener';
|
|
|
|
let defaultTheme;
|
|
|
|
function getDefaultTheme() {
|
|
if (defaultTheme) {
|
|
return defaultTheme;
|
|
}
|
|
|
|
defaultTheme = createMuiTheme();
|
|
return defaultTheme;
|
|
}
|
|
|
|
export type InjectedProps = { theme: Object };
|
|
|
|
// Provide the theme object as a property to the input component.
|
|
const withTheme = (): HigherOrderComponent<{}, InjectedProps> => (Component: any): any => {
|
|
class WithTheme extends React.Component<{}, { theme: Object }> {
|
|
static contextTypes = themeListener.contextTypes;
|
|
static displayName = wrapDisplayName(Component, 'withTheme');
|
|
|
|
// Exposed for test purposes.
|
|
static Naked = Component;
|
|
|
|
constructor(props, context) {
|
|
super(props, context);
|
|
this.state = {
|
|
// We use || as it's lazy evaluated.
|
|
theme: themeListener.initial(context) || getDefaultTheme(),
|
|
};
|
|
}
|
|
|
|
state = {};
|
|
|
|
componentDidMount() {
|
|
this.unsubscribeId = themeListener.subscribe(this.context, theme => {
|
|
this.setState({ theme });
|
|
});
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
if (this.unsubscribeId !== null) {
|
|
themeListener.unsubscribe(this.context, this.unsubscribeId);
|
|
}
|
|
}
|
|
|
|
unsubscribeId = null;
|
|
|
|
render() {
|
|
return <Component theme={this.state.theme} {...this.props} />;
|
|
}
|
|
}
|
|
|
|
return WithTheme;
|
|
};
|
|
|
|
export default withTheme;
|