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 (92 loc) • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _InlineStyle = require('./InlineStyle');
var _InlineStyle2 = _interopRequireDefault(_InlineStyle);
var _ThemeProvider = require('./ThemeProvider');
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; }
/* eslint-disable react/prefer-stateless-function */
var AbstractStyledNativeComponent = function (_Component) {
_inherits(AbstractStyledNativeComponent, _Component);
function AbstractStyledNativeComponent() {
_classCallCheck(this, AbstractStyledNativeComponent);
return _possibleConstructorReturn(this, (AbstractStyledNativeComponent.__proto__ || Object.getPrototypeOf(AbstractStyledNativeComponent)).apply(this, arguments));
}
return AbstractStyledNativeComponent;
}(_react.Component);
var createStyledNativeComponent = function createStyledNativeComponent(target, rules, parent) {
/* Handle styled(OtherStyledNativeComponent) differently */
var isStyledNativeComponent = AbstractStyledNativeComponent.isPrototypeOf(target);
if (isStyledNativeComponent) {
return createStyledNativeComponent(target.target, target.rules.concat(rules), target);
}
var inlineStyle = new _InlineStyle2.default(rules);
var ParentComponent = parent || AbstractStyledNativeComponent;
var StyledNativeComponent = function (_ParentComponent) {
_inherits(StyledNativeComponent, _ParentComponent);
function StyledNativeComponent() {
_classCallCheck(this, StyledNativeComponent);
var _this2 = _possibleConstructorReturn(this, (StyledNativeComponent.__proto__ || Object.getPrototypeOf(StyledNativeComponent)).call(this));
_this2.state = {
theme: null
};
return _this2;
}
_createClass(StyledNativeComponent, [{
key: 'componentWillMount',
value: function componentWillMount() {
var _this3 = this;
// If there is a theme in the context, subscribe to the event emitter. This is necessary
// due to pure components blocking context updates, this circumvents that by updating when an
// event is emitted
if (this.context[_ThemeProvider.CHANNEL]) {
var subscribe = this.context[_ThemeProvider.CHANNEL];
this.unsubscribe = subscribe(function (theme) {
// This will be called once immediately
_this3.setState({ theme: theme });
});
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.unsubscribe) {
this.unsubscribe();
}
}
/* eslint-disable react/prop-types */
}, {
key: 'render',
value: function render() {
var _props = this.props;
var style = _props.style;
var children = _props.children;
var innerRef = _props.innerRef;
var theme = this.state.theme || this.props.theme || {};
var generatedStyles = inlineStyle.generateStyleObject({ theme: theme });
var propsForElement = Object.assign({}, this.props);
propsForElement.style = [generatedStyles, style];
if (innerRef) {
propsForElement.ref = innerRef;
}
return (0, _react.createElement)(target, propsForElement, children);
}
}]);
return StyledNativeComponent;
}(ParentComponent);
/* Used for inheritance */
StyledNativeComponent.rules = rules;
StyledNativeComponent.target = target;
StyledNativeComponent.displayName = target.displayName ? 'Styled(' + target.displayName + ')' : 'styled.' + target;
StyledNativeComponent.contextTypes = _defineProperty({}, _ThemeProvider.CHANNEL, _react.PropTypes.object);
return StyledNativeComponent;
};
exports.default = createStyledNativeComponent;
module.exports = exports['default'];