styled-components
Version:
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
127 lines (99 loc) • 5.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CHANNEL = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _isFunction = require('lodash/isFunction');
var _isFunction2 = _interopRequireDefault(_isFunction);
var _isPlainObject = require('lodash/isPlainObject');
var _isPlainObject2 = _interopRequireDefault(_isPlainObject);
var _createBroadcast = require('../utils/create-broadcast');
var _createBroadcast2 = _interopRequireDefault(_createBroadcast);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// NOTE: DO NOT CHANGE, changing this is a semver major change!
var CHANNEL = exports.CHANNEL = '__styled-components__';
/**
* Provide a theme to an entire react component tree via context and event listeners (have to do
* both context and event emitter as pure components block context updates)
*/
var ThemeProvider = function (_Component) {
_inherits(ThemeProvider, _Component);
function ThemeProvider() {
_classCallCheck(this, ThemeProvider);
var _this = _possibleConstructorReturn(this, (ThemeProvider.__proto__ || Object.getPrototypeOf(ThemeProvider)).call(this));
_this.getTheme = _this.getTheme.bind(_this);
return _this;
}
_createClass(ThemeProvider, [{
key: 'componentWillMount',
value: function componentWillMount() {
var _this2 = this;
// If there is a ThemeProvider wrapper anywhere around this theme provider, merge this theme
// with the outer theme
if (this.context[CHANNEL]) {
var subscribe = this.context[CHANNEL];
this.unsubscribeToOuter = subscribe(function (theme) {
_this2.outerTheme = theme;
});
}
this.broadcast = (0, _createBroadcast2.default)(this.getTheme());
}
}, {
key: 'getChildContext',
value: function getChildContext() {
return Object.assign({}, this.context, _defineProperty({}, CHANNEL, this.broadcast.subscribe));
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (this.props.theme !== nextProps.theme) this.broadcast.publish(this.getTheme(nextProps.theme));
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.context[CHANNEL]) {
this.unsubscribeToOuter();
}
}
// Get the theme from the props, supporting both (outerTheme) => {} as well as object notation
}, {
key: 'getTheme',
value: function getTheme(passedTheme) {
var theme = passedTheme || this.props.theme;
if ((0, _isFunction2.default)(theme)) {
var mergedTheme = theme(this.outerTheme);
if (!(0, _isPlainObject2.default)(mergedTheme)) {
throw new Error('[ThemeProvider] Please return an object from your theme function, i.e. theme={() => ({})}!');
}
return mergedTheme;
}
if (!(0, _isPlainObject2.default)(theme)) {
throw new Error('[ThemeProvider] Please make your theme prop a plain object');
}
return Object.assign({}, this.outerTheme, theme);
}
}, {
key: 'render',
value: function render() {
if (!this.props.children) {
return null;
}
return _react2.default.Children.only(this.props.children);
}
}]);
return ThemeProvider;
}(_react.Component);
ThemeProvider.propTypes = {
children: _react.PropTypes.node,
theme: _react.PropTypes.oneOfType([_react.PropTypes.func, _react.PropTypes.object])
};
ThemeProvider.childContextTypes = _defineProperty({}, CHANNEL, _react.PropTypes.func.isRequired);
ThemeProvider.contextTypes = _defineProperty({}, CHANNEL, _react.PropTypes.func);
exports.default = ThemeProvider;