UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

98 lines (79 loc) 2.6 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose"; /* eslint-disable no-underscore-dangle */ import React from 'react'; import PropTypes from 'prop-types'; import hoistNonReactStatics from 'hoist-non-react-statics'; import { getDisplayName, ponyfillGlobal } from '@material-ui/utils'; import createMuiTheme from './createMuiTheme'; import themeListener from './themeListener'; let defaultTheme; function getDefaultTheme() { if (defaultTheme) { return defaultTheme; } defaultTheme = createMuiTheme({ typography: { suppressWarning: true } }); return defaultTheme; } // Provide the theme object as a property to the input component. const withThemeOld = () => 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 = _objectWithoutPropertiesLoose(_this$props, ["innerRef"]); return React.createElement(Component, _extends({ theme: this.state.theme, ref: innerRef }, other)); } } process.env.NODE_ENV !== "production" ? WithTheme.propTypes = { /** * Use that property to pass a ref callback to the decorated component. */ innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]) } : void 0; WithTheme.contextTypes = themeListener.contextTypes; if (process.env.NODE_ENV !== 'production') { WithTheme.displayName = `WithTheme(${getDisplayName(Component)})`; } hoistNonReactStatics(WithTheme, Component); if (process.env.NODE_ENV !== 'production') { // Exposed for test purposes. WithTheme.Naked = Component; } return WithTheme; }; /* istanbul ignore if */ if (!ponyfillGlobal.__MUI_STYLES__) { ponyfillGlobal.__MUI_STYLES__ = {}; } if (!ponyfillGlobal.__MUI_STYLES__.withTheme) { ponyfillGlobal.__MUI_STYLES__.withTheme = withThemeOld; } export default ponyfillGlobal.__MUI_STYLES__.withTheme;