UNPKG

2.2 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.flush = flush;
5exports["default"] = void 0;
6
7var _react = require("react");
8
9var _stylesheetRegistry = _interopRequireDefault(require("./stylesheet-registry"));
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
12
13function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
14
15var styleSheetRegistry = new _stylesheetRegistry["default"]();
16
17var JSXStyle = /*#__PURE__*/function (_Component) {
18 _inheritsLoose(JSXStyle, _Component);
19
20 function JSXStyle(props) {
21 var _this;
22
23 _this = _Component.call(this, props) || this;
24 _this.prevProps = {};
25 return _this;
26 }
27
28 JSXStyle.dynamic = function dynamic(info) {
29 return info.map(function (tagInfo) {
30 var baseId = tagInfo[0];
31 var props = tagInfo[1];
32 return styleSheetRegistry.computeId(baseId, props);
33 }).join(' ');
34 } // probably faster than PureComponent (shallowEqual)
35 ;
36
37 var _proto = JSXStyle.prototype;
38
39 _proto.shouldComponentUpdate = function shouldComponentUpdate(otherProps) {
40 return this.props.id !== otherProps.id || // We do this check because `dynamic` is an array of strings or undefined.
41 // These are the computed values for dynamic styles.
42 String(this.props.dynamic) !== String(otherProps.dynamic);
43 };
44
45 _proto.componentWillUnmount = function componentWillUnmount() {
46 styleSheetRegistry.remove(this.props);
47 };
48
49 _proto.render = function render() {
50 // This is a workaround to make the side effect async safe in the "render" phase.
51 // See https://github.com/zeit/styled-jsx/pull/484
52 if (this.shouldComponentUpdate(this.prevProps)) {
53 // Updates
54 if (this.prevProps.id) {
55 styleSheetRegistry.remove(this.prevProps);
56 }
57
58 styleSheetRegistry.add(this.props);
59 this.prevProps = this.props;
60 }
61
62 return null;
63 };
64
65 return JSXStyle;
66}(_react.Component);
67
68exports["default"] = JSXStyle;
69
70function flush() {
71 var cssRules = styleSheetRegistry.cssRules();
72 styleSheetRegistry.flush();
73 return cssRules;
74}
\No newline at end of file