styled-components
Version:
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
141 lines (105 loc) • 6.06 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 _validAttr = require('../utils/validAttr');
var _validAttr2 = _interopRequireDefault(_validAttr);
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 AbstractStyledComponent = function (_Component) {
_inherits(AbstractStyledComponent, _Component);
function AbstractStyledComponent() {
_classCallCheck(this, AbstractStyledComponent);
return _possibleConstructorReturn(this, (AbstractStyledComponent.__proto__ || Object.getPrototypeOf(AbstractStyledComponent)).apply(this, arguments));
}
return AbstractStyledComponent;
}(_react.Component);
exports.default = function (ComponentStyle) {
var createStyledComponent = function createStyledComponent(target, rules, parent) {
/* Handle styled(OtherStyledComponent) differently */
var isStyledComponent = AbstractStyledComponent.isPrototypeOf(target);
if (isStyledComponent) {
return createStyledComponent(target.target, target.rules.concat(rules), target);
}
var isTag = typeof target === 'string';
var componentStyle = new ComponentStyle(rules);
var ParentComponent = parent || AbstractStyledComponent;
var StyledComponent = function (_ParentComponent) {
_inherits(StyledComponent, _ParentComponent);
function StyledComponent() {
_classCallCheck(this, StyledComponent);
var _this2 = _possibleConstructorReturn(this, (StyledComponent.__proto__ || Object.getPrototypeOf(StyledComponent)).call(this));
_this2.state = {
theme: null
};
return _this2;
}
_createClass(StyledComponent, [{
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 _this4 = this;
var _props = this.props;
var className = _props.className;
var children = _props.children;
var innerRef = _props.innerRef;
var theme = this.state.theme || this.props.theme || {};
var executionContext = Object.assign({}, this.props, { theme: theme });
var generatedClassName = componentStyle.generateAndInjectStyles(executionContext);
var propsForElement = {};
/* Don't pass through non HTML tags through to HTML elements */
Object.keys(this.props).filter(function (propName) {
return !isTag || (0, _validAttr2.default)(propName);
}).forEach(function (propName) {
propsForElement[propName] = _this4.props[propName];
});
propsForElement.className = [className, generatedClassName].filter(function (x) {
return x;
}).join(' ');
if (innerRef) {
propsForElement.ref = innerRef;
}
return (0, _react.createElement)(target, propsForElement, children);
}
}]);
return StyledComponent;
}(ParentComponent);
/* Used for inheritance */
StyledComponent.rules = rules;
StyledComponent.target = target;
StyledComponent.displayName = isTag ? 'styled.' + target : 'Styled(' + target.displayName + ')';
StyledComponent.contextTypes = _defineProperty({}, _ThemeProvider.CHANNEL, _react.PropTypes.func);
return StyledComponent;
};
return createStyledComponent;
};
module.exports = exports['default'];