UNPKG

6.97 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9exports.default = enhanceWithRadium;
10
11var _react = require('react');
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17var _styleKeeper = require('./style-keeper.js');
18
19var _styleKeeper2 = _interopRequireDefault(_styleKeeper);
20
21var _resolveStyles = require('./resolve-styles.js');
22
23var _resolveStyles2 = _interopRequireDefault(_resolveStyles);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29function _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; }
30
31function _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; }
32
33var KEYS_TO_IGNORE_WHEN_COPYING_PROPERTIES = ['arguments', 'callee', 'caller', 'length', 'name', 'prototype', 'type'];
34
35function copyProperties(source, target) {
36 Object.getOwnPropertyNames(source).forEach(function (key) {
37 if (KEYS_TO_IGNORE_WHEN_COPYING_PROPERTIES.indexOf(key) < 0 && !target.hasOwnProperty(key)) {
38 var descriptor = Object.getOwnPropertyDescriptor(source, key);
39 Object.defineProperty(target, key, descriptor);
40 }
41 });
42}
43
44function isStateless(component) {
45 return !component.render && !(component.prototype && component.prototype.render);
46}
47
48function enhanceWithRadium(configOrComposedComponent) {
49 var _class, _temp;
50
51 var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
52
53 if (typeof configOrComposedComponent !== 'function') {
54 var newConfig = _extends({}, config, configOrComposedComponent);
55 return function (configOrComponent) {
56 return enhanceWithRadium(configOrComponent, newConfig);
57 };
58 }
59
60 var component = configOrComposedComponent;
61 var ComposedComponent = component;
62
63 // Handle stateless components
64 if (isStateless(ComposedComponent)) {
65 ComposedComponent = function (_Component) {
66 _inherits(ComposedComponent, _Component);
67
68 function ComposedComponent() {
69 _classCallCheck(this, ComposedComponent);
70
71 return _possibleConstructorReturn(this, _Component.apply(this, arguments));
72 }
73
74 ComposedComponent.prototype.render = function render() {
75 return component(this.props, this.context);
76 };
77
78 return ComposedComponent;
79 }(_react.Component);
80 ComposedComponent.displayName = component.displayName || component.name;
81 }
82
83 var RadiumEnhancer = (_temp = _class = function (_ComposedComponent) {
84 _inherits(RadiumEnhancer, _ComposedComponent);
85
86 function RadiumEnhancer() {
87 _classCallCheck(this, RadiumEnhancer);
88
89 var _this2 = _possibleConstructorReturn(this, _ComposedComponent.apply(this, arguments));
90
91 _this2.state = _this2.state || {};
92 _this2.state._radiumStyleState = {};
93 _this2._radiumIsMounted = true;
94 return _this2;
95 }
96
97 RadiumEnhancer.prototype.componentWillUnmount = function componentWillUnmount() {
98 if (_ComposedComponent.prototype.componentWillUnmount) {
99 _ComposedComponent.prototype.componentWillUnmount.call(this);
100 }
101
102 this._radiumIsMounted = false;
103
104 if (this._radiumMouseUpListener) {
105 this._radiumMouseUpListener.remove();
106 }
107
108 if (this._radiumMediaQueryListenersByQuery) {
109 Object.keys(this._radiumMediaQueryListenersByQuery).forEach(function (query) {
110 this._radiumMediaQueryListenersByQuery[query].remove();
111 }, this);
112 }
113 };
114
115 RadiumEnhancer.prototype.getChildContext = function getChildContext() {
116 var superChildContext = _ComposedComponent.prototype.getChildContext ? _ComposedComponent.prototype.getChildContext.call(this) : {};
117
118 if (!this.props.radiumConfig) {
119 return superChildContext;
120 }
121
122 var newContext = _extends({}, superChildContext);
123
124 if (this.props.radiumConfig) {
125 newContext._radiumConfig = this.props.radiumConfig;
126 }
127
128 return newContext;
129 };
130
131 RadiumEnhancer.prototype.render = function render() {
132 var renderedElement = _ComposedComponent.prototype.render.call(this);
133 var currentConfig = this.props.radiumConfig || this.context._radiumConfig || config;
134
135 if (config && currentConfig !== config) {
136 currentConfig = _extends({}, config, currentConfig);
137 }
138
139 return (0, _resolveStyles2.default)(this, renderedElement, currentConfig);
140 };
141
142 return RadiumEnhancer;
143 }(ComposedComponent), _class._isRadiumEnhanced = true, _temp);
144
145 // Class inheritance uses Object.create and because of __proto__ issues
146 // with IE <10 any static properties of the superclass aren't inherited and
147 // so need to be manually populated.
148 // See http://babeljs.io/docs/advanced/caveats/#classes-10-and-below-
149
150 copyProperties(component, RadiumEnhancer);
151
152 if (process.env.NODE_ENV !== 'production') {
153 // This also fixes React Hot Loader by exposing the original components top
154 // level prototype methods on the Radium enhanced prototype as discussed in
155 // https://github.com/FormidableLabs/radium/issues/219.
156 copyProperties(ComposedComponent.prototype, RadiumEnhancer.prototype);
157 }
158
159 if (RadiumEnhancer.propTypes && RadiumEnhancer.propTypes.style) {
160 RadiumEnhancer.propTypes = _extends({}, RadiumEnhancer.propTypes, {
161 style: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.object])
162 });
163 }
164
165 RadiumEnhancer.displayName = component.displayName || component.name || 'Component';
166
167 RadiumEnhancer.contextTypes = _extends({}, RadiumEnhancer.contextTypes, {
168 _radiumConfig: _propTypes2.default.object,
169 _radiumStyleKeeper: _propTypes2.default.instanceOf(_styleKeeper2.default)
170 });
171
172 RadiumEnhancer.childContextTypes = _extends({}, RadiumEnhancer.childContextTypes, {
173 _radiumConfig: _propTypes2.default.object,
174 _radiumStyleKeeper: _propTypes2.default.instanceOf(_styleKeeper2.default)
175 });
176
177 return RadiumEnhancer;
178}
179module.exports = exports['default'];
\No newline at end of file