styled-components
Version:
**This is a work in progress** based off of [this demo](https://github.com/geelen/css-components-demo).
118 lines (87 loc) • 5.19 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);
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 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, options) {
/* Handle styled(OtherStyledNativeComponent) differently */
var isStyledNativeComponent = {}.isPrototypeOf.call(AbstractStyledNativeComponent, target);
if (isStyledNativeComponent) {
return createStyledNativeComponent(target.target, target.rules.concat(rules), options);
}
var inlineStyle = new _InlineStyle2.default(rules);
var StyledNativeComponent = function (_AbstractStyledNative) {
_inherits(StyledNativeComponent, _AbstractStyledNative);
function StyledNativeComponent() {
_classCallCheck(this, StyledNativeComponent);
return _possibleConstructorReturn(this, (StyledNativeComponent.__proto__ || Object.getPrototypeOf(StyledNativeComponent)).apply(this, arguments));
}
_createClass(StyledNativeComponent, [{
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.generatedStyles = inlineStyle.generateStyleObject(executionContext);
}
/* eslint-disable react/prop-types */
}, {
key: 'render',
value: function render() {
var _props = this.props;
var style = _props.style;
var children = _props.children;
var propsForElement = Object.assign({}, this.props);
propsForElement.style = Object.assign({}, style, this.generatedStyles);
return (0, _react.createElement)(target, propsForElement, children);
}
}]);
return StyledNativeComponent;
}(AbstractStyledNativeComponent);
/* Used for inheritance */
StyledNativeComponent.rules = rules;
StyledNativeComponent.target = target;
StyledNativeComponent.displayName = target.displayName ? 'Styled(' + target.displayName + ')' : 'styled.' + target;
StyledNativeComponent.childContextTypes = {
theme: _react.PropTypes.object
};
StyledNativeComponent.contextTypes = {
theme: _react.PropTypes.object
};
return StyledNativeComponent;
};
exports.default = createStyledNativeComponent;
module.exports = exports['default'];