Created
July 24, 2015 09:32
-
-
Save idoo/454f2849c3ed31d585b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! React Starter Kit | MIT License | http://www.reactstarterkit.com/ */ | |
import React, { PropTypes } from 'react'; // eslint-disable-line no-unused-vars | |
import emptyFunction from '../../node_modules/react/lib/emptyFunction'; | |
import mui from 'material-ui'; | |
function withContext(ComposedComponent) { | |
return class WithContext { | |
static propTypes = { | |
context: PropTypes.shape({ | |
onInsertCss: PropTypes.func, | |
onSetTitle: PropTypes.func, | |
onSetMeta: PropTypes.func, | |
onPageNotFound: PropTypes.func | |
}) | |
}; | |
static childContextTypes = { | |
onInsertCss: PropTypes.func.isRequired, | |
onSetTitle: PropTypes.func.isRequired, | |
onSetMeta: PropTypes.func.isRequired, | |
onPageNotFound: PropTypes.func.isRequired | |
}; | |
getChildContext() { | |
let context = this.props.context; | |
let ThemeManager = new mui.Styles.ThemeManager(); | |
return { | |
onInsertCss: context.onInsertCss || emptyFunction, | |
onSetTitle: context.onSetTitle || emptyFunction, | |
onSetMeta: context.onSetMeta || emptyFunction, | |
onPageNotFound: context.onPageNotFound || emptyFunction, | |
muiTheme: ThemeManager.getCurrentTheme() | |
}; | |
} | |
render() { | |
let { context, ...other } = this.props; // eslint-disable-line no-unused-vars | |
return <ComposedComponent {...other} />; | |
} | |
}; | |
} | |
export default withContext; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! React Starter Kit | MIT License | http://www.reactstarterkit.com/ */ | |
import React, { PropTypes } from 'react'; // eslint-disable-line no-unused-vars | |
import invariant from '../../node_modules/react/lib/invariant'; | |
import { canUseDOM } from '../../node_modules/react/lib/ExecutionEnvironment'; | |
let count = 0; | |
function withStyles(styles) { | |
return (ComposedComponent) => class WithStyles { | |
static contextTypes = { | |
onInsertCss: PropTypes.func | |
}; | |
static childContextTypes = { | |
muiTheme: React.PropTypes.object | |
}; | |
constructor() { | |
this.refCount = 0; | |
ComposedComponent.prototype.renderCss = function (css) { | |
let style; | |
if (canUseDOM) { | |
if (this.styleId && (style = document.getElementById(this.styleId))) { | |
if ('textContent' in style) { | |
style.textContent = css; | |
} else { | |
style.styleSheet.cssText = css; | |
} | |
} else { | |
this.styleId = `dynamic-css-${count++}`; | |
style = document.createElement('style'); | |
style.setAttribute('id', this.styleId); | |
style.setAttribute('type', 'text/css'); | |
if ('textContent' in style) { | |
style.textContent = css; | |
} else { | |
style.styleSheet.cssText = css; | |
} | |
document.getElementsByTagName('head')[0].appendChild(style); | |
this.refCount++; | |
} | |
} else { | |
this.context.onInsertCss(css); | |
} | |
}.bind(this); | |
} | |
componentWillMount() { | |
if (canUseDOM) { | |
invariant(styles.use, `The style-loader must be configured with reference-counted API.`); | |
styles.use(); | |
} else { | |
this.context.onInsertCss(styles.toString()); | |
} | |
} | |
componentWillUnmount() { | |
styles.unuse(); | |
if (this.styleId) { | |
this.refCount--; | |
if (this.refCount < 1) { | |
let style = document.getElementById(this.styleId); | |
if (style) { | |
style.parentNode.removeChild(style); | |
} | |
} | |
} | |
} | |
render() { | |
return <ComposedComponent {...this.props} />; | |
} | |
}; | |
} | |
export default withStyles; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment