diff --git a/src/styles/MuiThemeProvider.js b/src/styles/MuiThemeProvider.js index 58f76125cab530..c922bb8760708b 100644 --- a/src/styles/MuiThemeProvider.js +++ b/src/styles/MuiThemeProvider.js @@ -1,158 +1,9 @@ // @flow weak -import { Component } from 'react'; -import PropTypes from 'prop-types'; -import { create } from 'jss'; -import { createStyleManager } from 'jss-theme-reactor/styleManager'; -import jssPreset from 'jss-preset-default'; import createMuiTheme from './theme'; +import muiThemeProviderFactory, { + MUI_SHEET_ORDER as muiSheetOrder, +} from './muiThemeProviderFactory'; -export const MUI_SHEET_ORDER = [ - 'MuiTextarea', - 'MuiInput', - 'MuiGrid', - 'MuiCollapse', - 'MuiFade', - 'MuiSlide', +export const MUI_SHEET_ORDER = muiSheetOrder; - 'MuiBackdrop', - 'MuiModal', - - 'MuiRipple', - 'MuiTouchRipple', - - 'MuiButtonBase', - - 'MuiFormLabel', - 'MuiFormGroup', - - 'MuiTypography', - 'MuiPaper', - 'MuiDivider', - - 'MuiPopover', - - 'MuiButton', - 'MuiIconButton', - - 'MuiSvgIcon', - 'MuiIcon', - - 'MuiSwitchBase', - 'MuiSwitch', - 'MuiCheckbox', - 'MuiRadio', - 'MuiRadioGroup', - 'MuiSwitchLabel', - - 'MuiDialog', - 'MuiDialogActions', - 'MuiDialogContent', - 'MuiDialogContentText', - 'MuiDialogTitle', - - 'MuiTabIndicator', - 'MuiTab', - 'MuiTabs', - - 'MuiBottomNavigationButton', - 'MuiBottomNavigation', - - 'MuiCircularProgress', - 'MuiLinearProgress', - - 'MuiAppBar', - 'MuiDrawer', - - 'MuiAvatar', - - 'MuiChip', - - 'MuiListItem', - 'MuiListItemText', - 'MuiListItemSecondaryAction', - 'MuiListItemAvatar', - 'MuiListItemIcon', - 'MuiListSubheader', - 'MuiList', - - 'MuiMenu', - 'MuiMenuItem', - - 'MuiCardContent', - 'MuiCardMedia', - 'MuiCardActions', - 'MuiCardHeader', - 'MuiCard', - - 'MuiTextFieldLabel', - 'MuiTextFieldInput', - 'MuiTextField', - - 'MuiTable', - 'MuiTableHead', - 'MuiTableRow', - 'MuiTableCell', - 'MuiTableBody', - 'MuiTableSortLabel', - 'MuiToolbar', - - 'MuiBadge', -]; - -export default class MuiThemeProvider extends Component { - static propTypes = { - children: PropTypes.element.isRequired, - styleManager: PropTypes.object, - theme: PropTypes.object, - }; - - static childContextTypes = { - styleManager: PropTypes.object.isRequired, - }; - - static createDefaultContext(props = {}) { - const theme = props.theme || createMuiTheme(); - const styleManager = - props.styleManager || - createStyleManager({ - theme, - jss: create(jssPreset()), - }); - - if (!styleManager.sheetOrder) { - styleManager.setSheetOrder(MUI_SHEET_ORDER); - } - - return { theme, styleManager }; - } - - getChildContext() { - return { - styleManager: this.styleManager, - }; - } - - componentWillMount() { - const { theme, styleManager } = MuiThemeProvider.createDefaultContext(this.props); - this.theme = theme; - this.styleManager = styleManager; - } - - componentWillUpdate(nextProps) { - if (this.styleManager !== nextProps.styleManager) { - const { theme, styleManager } = MuiThemeProvider.createDefaultContext(nextProps); - this.theme = theme; - this.styleManager = styleManager; - } else if (this.theme && nextProps.theme && nextProps.theme !== this.theme) { - this.theme = nextProps.theme; - this.styleManager.updateTheme(this.theme); - } - } - - theme = undefined; - styleManager = undefined; - - render() { - return this.props.children; - } -} +export default muiThemeProviderFactory(createMuiTheme()); diff --git a/src/styles/muiThemeProviderFactory.js b/src/styles/muiThemeProviderFactory.js new file mode 100644 index 00000000000000..0a1b3324adfafe --- /dev/null +++ b/src/styles/muiThemeProviderFactory.js @@ -0,0 +1,159 @@ +// @flow weak +import { Component } from 'react'; +import PropTypes from 'prop-types'; +import { create } from 'jss'; +import { createStyleManager } from 'jss-theme-reactor/styleManager'; +import jssPreset from 'jss-preset-default'; + +export const MUI_SHEET_ORDER = [ + 'MuiTextarea', + 'MuiInput', + 'MuiGrid', + 'MuiCollapse', + 'MuiFade', + 'MuiSlide', + + 'MuiBackdrop', + 'MuiModal', + + 'MuiRipple', + 'MuiTouchRipple', + + 'MuiButtonBase', + + 'MuiFormLabel', + 'MuiFormGroup', + + 'MuiTypography', + 'MuiPaper', + 'MuiDivider', + + 'MuiPopover', + + 'MuiButton', + 'MuiIconButton', + + 'MuiSvgIcon', + 'MuiIcon', + + 'MuiSwitchBase', + 'MuiSwitch', + 'MuiCheckbox', + 'MuiRadio', + 'MuiRadioGroup', + 'MuiSwitchLabel', + + 'MuiDialog', + 'MuiDialogActions', + 'MuiDialogContent', + 'MuiDialogContentText', + 'MuiDialogTitle', + + 'MuiTabIndicator', + 'MuiTab', + 'MuiTabs', + + 'MuiBottomNavigationButton', + 'MuiBottomNavigation', + + 'MuiCircularProgress', + 'MuiLinearProgress', + + 'MuiAppBar', + 'MuiDrawer', + + 'MuiAvatar', + + 'MuiChip', + + 'MuiListItem', + 'MuiListItemText', + 'MuiListItemSecondaryAction', + 'MuiListItemAvatar', + 'MuiListItemIcon', + 'MuiListSubheader', + 'MuiList', + + 'MuiMenu', + 'MuiMenuItem', + + 'MuiCardContent', + 'MuiCardMedia', + 'MuiCardActions', + 'MuiCardHeader', + 'MuiCard', + + 'MuiTextFieldLabel', + 'MuiTextFieldInput', + 'MuiTextField', + + 'MuiTable', + 'MuiTableHead', + 'MuiTableRow', + 'MuiTableCell', + 'MuiTableBody', + 'MuiTableSortLabel', + 'MuiToolbar', + + 'MuiBadge', +]; + +export default function muiThemeProviderFactory(defaultTheme) { + return class MuiThemeProvider extends Component { + static propTypes = { + children: PropTypes.element.isRequired, + styleManager: PropTypes.object, + theme: PropTypes.object, + }; + + static childContextTypes = { + styleManager: PropTypes.object.isRequired, + }; + + static createDefaultContext(props = {}) { + const theme = props.theme || defaultTheme; + const styleManager = + props.styleManager || + createStyleManager({ + theme, + jss: create(jssPreset()), + }); + + if (!styleManager.sheetOrder) { + styleManager.setSheetOrder(MUI_SHEET_ORDER); + } + + return { theme, styleManager }; + } + + getChildContext() { + return { + styleManager: this.styleManager, + }; + } + + componentWillMount() { + const { theme, styleManager } = MuiThemeProvider.createDefaultContext(this.props); + this.theme = theme; + this.styleManager = styleManager; + } + + componentWillUpdate(nextProps) { + if (this.styleManager !== nextProps.styleManager) { + const { theme, styleManager } = MuiThemeProvider.createDefaultContext(nextProps); + this.theme = theme; + this.styleManager = styleManager; + } else if (this.theme && nextProps.theme && nextProps.theme !== this.theme) { + this.theme = nextProps.theme; + this.styleManager.updateTheme(this.theme); + } + } + + theme = undefined; + styleManager = undefined; + + render() { + return this.props.children; + } + }; +}