styled-components
Version:
**This is a work in progress** based off of [this demo](https://github.com/geelen/css-components-demo).
131 lines (97 loc) • 5.55 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 _ComponentStyle = require('../models/ComponentStyle');
var _ComponentStyle2 = _interopRequireDefault(_ComponentStyle);
var _validAttr = require('../utils/validAttr');
var _validAttr2 = _interopRequireDefault(_validAttr);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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);
var createStyledComponent = function createStyledComponent(target, rules, options) {
/* Handle styled(OtherStyledComponent) differently */
var isStyledComponent = {}.isPrototypeOf.call(AbstractStyledComponent, target);
if (isStyledComponent) return createStyledComponent(target.target, target.rules.concat(rules), options);
var isTag = typeof target === 'string';
var componentStyle = new _ComponentStyle2.default(rules);
var StyledComponent = function (_AbstractStyledCompon) {
_inherits(StyledComponent, _AbstractStyledCompon);
function StyledComponent() {
_classCallCheck(this, StyledComponent);
return _possibleConstructorReturn(this, (StyledComponent.__proto__ || Object.getPrototypeOf(StyledComponent)).apply(this, arguments));
}
_createClass(StyledComponent, [{
key: 'getChildContext',
value: function getChildContext() {
return { theme: this.theme };
}
}, {
key: 'componentWillMount',
value: function componentWillMount() {
this.componentWillReceiveProps(this.props, this.context);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(newProps, newContext) {
// Always pass down a theme, even if it's empty
this.theme = newContext && newContext.theme || {};
// Local copy for this instance with an update() method
var theme = Object.assign({}, this.theme, {
update: function update(values) {
this.theme = Object.assign({}, this.theme, values);
}
});
/* Generate and inject the styles and potentially update theme */
var executionContext = Object.assign({}, newProps, { theme: theme });
this.generatedClassName = componentStyle.generateAndInjectStyles(executionContext);
}
/* eslint-disable react/prop-types */
}, {
key: 'render',
value: function render() {
var _this3 = this;
var _props = this.props;
var className = _props.className;
var children = _props.children;
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] = _this3.props[propName];
});
propsForElement.className = [className, this.generatedClassName].filter(function (x) {
return x;
}).join(' ');
return (0, _react.createElement)(target, propsForElement, children);
}
}]);
return StyledComponent;
}(AbstractStyledComponent);
/* Used for inheritance */
StyledComponent.rules = rules;
StyledComponent.target = target;
StyledComponent.displayName = isTag ? 'styled.' + target : 'Styled(' + target.displayName + ')';
StyledComponent.childContextTypes = {
theme: _react.PropTypes.object
};
StyledComponent.contextTypes = {
theme: _react.PropTypes.object
};
return StyledComponent;
};
exports.default = createStyledComponent;
module.exports = exports['default'];