Skip to content

Commit

Permalink
[MuiThemeProvider] muiThemeProviderFactory implemented (#6900) (#7000)
Browse files Browse the repository at this point in the history
  • Loading branch information
viotti authored and oliviertassinari committed May 30, 2017
1 parent bd4b46b commit b99fcab
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 154 deletions.
159 changes: 5 additions & 154 deletions src/styles/MuiThemeProvider.js
Original file line number Diff line number Diff line change
@@ -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());
159 changes: 159 additions & 0 deletions src/styles/muiThemeProviderFactory.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
}

0 comments on commit b99fcab

Please sign in to comment.