UNPKG

4.38 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.flush = flush;
7exports.default = void 0;
8
9var _react = require("react");
10
11var _stylesheetRegistry = _interopRequireDefault(require("./stylesheet-registry"));
12
13function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
15function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
16
17function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
18
19function _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); } }
20
21function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
22
23function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
24
25function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
26
27function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
28
29function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
30
31function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
32
33var styleSheetRegistry = new _stylesheetRegistry.default();
34
35var JSXStyle =
36/*#__PURE__*/
37function (_Component) {
38 _inherits(JSXStyle, _Component);
39
40 function JSXStyle(props) {
41 var _this;
42
43 _classCallCheck(this, JSXStyle);
44
45 _this = _possibleConstructorReturn(this, _getPrototypeOf(JSXStyle).call(this, props));
46 _this.prevProps = {};
47 return _this;
48 }
49
50 _createClass(JSXStyle, [{
51 key: "shouldComponentUpdate",
52 // probably faster than PureComponent (shallowEqual)
53 value: function shouldComponentUpdate(otherProps) {
54 return this.props.id !== otherProps.id || // We do this check because `dynamic` is an array of strings or undefined.
55 // These are the computed values for dynamic styles.
56 String(this.props.dynamic) !== String(otherProps.dynamic);
57 }
58 }, {
59 key: "componentWillUnmount",
60 value: function componentWillUnmount() {
61 styleSheetRegistry.remove(this.props);
62 }
63 }, {
64 key: "render",
65 value: function render() {
66 // This is a workaround to make the side effect async safe in the "render" phase.
67 // See https://github.com/zeit/styled-jsx/pull/484
68 if (this.shouldComponentUpdate(this.prevProps)) {
69 // Updates
70 if (this.prevProps.id) {
71 styleSheetRegistry.remove(this.prevProps);
72 }
73
74 styleSheetRegistry.add(this.props);
75 this.prevProps = this.props;
76 }
77
78 return null;
79 }
80 }], [{
81 key: "dynamic",
82 value: function dynamic(info) {
83 return info.map(function (tagInfo) {
84 var baseId = tagInfo[0];
85 var props = tagInfo[1];
86 return styleSheetRegistry.computeId(baseId, props);
87 }).join(' ');
88 }
89 }]);
90
91 return JSXStyle;
92}(_react.Component);
93
94exports.default = JSXStyle;
95
96function flush() {
97 var cssRules = styleSheetRegistry.cssRules();
98 styleSheetRegistry.flush();
99 return cssRules;
100}
\No newline at end of file