@material-ui/core
Version:
React components that implement Google's Material Design.
82 lines (67 loc) • 2.21 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import React from 'react';
import PropTypes from 'prop-types';
import hoistNonReactStatics from 'hoist-non-react-statics';
import wrapDisplayName from 'recompose/wrapDisplayName';
import createMuiTheme from './createMuiTheme';
import themeListener from './themeListener';
let defaultTheme;
function getDefaultTheme() {
if (defaultTheme) {
return defaultTheme;
}
defaultTheme = createMuiTheme();
return defaultTheme;
} // Provide the theme object as a property to the input component.
const withTheme = () => Component => {
class WithTheme extends React.Component {
constructor(props, context) {
super();
this.state = {
// We use || as the function call is lazy evaluated.
theme: themeListener.initial(context) || getDefaultTheme()
};
}
componentDidMount() {
this.unsubscribeId = themeListener.subscribe(this.context, theme => {
this.setState({
theme
});
});
}
componentWillUnmount() {
if (this.unsubscribeId !== null) {
themeListener.unsubscribe(this.context, this.unsubscribeId);
}
}
render() {
const _this$props = this.props,
{
innerRef
} = _this$props,
other = _objectWithoutProperties(_this$props, ["innerRef"]);
return React.createElement(Component, _extends({
theme: this.state.theme,
ref: innerRef
}, other));
}
}
WithTheme.propTypes = process.env.NODE_ENV !== "production" ? {
/**
* Use that property to pass a ref callback to the decorated component.
*/
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
} : {};
WithTheme.contextTypes = themeListener.contextTypes;
if (process.env.NODE_ENV !== 'production') {
WithTheme.displayName = wrapDisplayName(Component, 'WithTheme');
}
hoistNonReactStatics(WithTheme, Component);
if (process.env.NODE_ENV !== 'production') {
// Exposed for test purposes.
WithTheme.Naked = Component;
}
return WithTheme;
};
export default withTheme;