@tdb/web
Version:
Common condiguration for serving a web-site and testing web-based UI components.
89 lines (69 loc) • 3.73 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; }; }();
exports.flush = flush;
var _react = require('react');
var _stylesheetRegistry = require('./stylesheet-registry');
var _stylesheetRegistry2 = _interopRequireDefault(_stylesheetRegistry);
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; }
var styleSheetRegistry = new _stylesheetRegistry2.default();
var JSXStyle = function (_Component) {
_inherits(JSXStyle, _Component);
function JSXStyle(props) {
_classCallCheck(this, JSXStyle);
var _this = _possibleConstructorReturn(this, (JSXStyle.__proto__ || Object.getPrototypeOf(JSXStyle)).call(this, props));
_this.prevProps = {};
return _this;
}
_createClass(JSXStyle, [{
key: 'shouldComponentUpdate',
// probably faster than PureComponent (shallowEqual)
value: function shouldComponentUpdate(otherProps) {
return this.props.styleId !== otherProps.styleId ||
// We do this check because `dynamic` is an array of strings or undefined.
// These are the computed values for dynamic styles.
String(this.props.dynamic) !== String(otherProps.dynamic);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
styleSheetRegistry.remove(this.props);
}
}, {
key: 'render',
value: function render() {
// This is a workaround to make the side effect async safe in the "render" phase.
// See https://github.com/zeit/styled-jsx/pull/484
if (this.shouldComponentUpdate(this.prevProps)) {
// Updates
if (this.prevProps.styleId) {
styleSheetRegistry.remove(this.prevProps);
}
styleSheetRegistry.add(this.props);
this.prevProps = this.props;
}
return null;
}
}], [{
key: 'dynamic',
value: function dynamic(info) {
return info.map(function (tagInfo) {
var baseId = tagInfo[0];
var props = tagInfo[1];
return styleSheetRegistry.computeId(baseId, props);
}).join(' ');
}
}]);
return JSXStyle;
}(_react.Component);
exports.default = JSXStyle;
function flush() {
var cssRules = styleSheetRegistry.cssRules();
styleSheetRegistry.flush();
return cssRules;
}