react-native-web-headroom
Version:
React Native Web Headroom
238 lines (176 loc) • 9.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _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; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
exports.default = enhanceWithRadium;
var _react = require('react');
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _styleKeeper = require('./style-keeper.js');
var _styleKeeper2 = _interopRequireDefault(_styleKeeper);
var _resolveStyles = require('./resolve-styles.js');
var _resolveStyles2 = _interopRequireDefault(_resolveStyles);
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 KEYS_TO_IGNORE_WHEN_COPYING_PROPERTIES = ['arguments', 'callee', 'caller', 'length', 'name', 'prototype', 'type'];
function copyProperties(source, target) {
Object.getOwnPropertyNames(source).forEach(function (key) {
if (KEYS_TO_IGNORE_WHEN_COPYING_PROPERTIES.indexOf(key) < 0 && !target.hasOwnProperty(key)) {
var descriptor = Object.getOwnPropertyDescriptor(source, key);
Object.defineProperty(target, key, descriptor);
}
});
}
function isStateless(component) {
return !component.render && !(component.prototype && component.prototype.render);
}
// Check if value is a real ES class in Native / Node code.
// See: https://stackoverflow.com/a/30760236
function isNativeClass(component) {
return typeof component === 'function' && /^\s*class\s+/.test(component.toString());
}
// Manually apply babel-ish class inheritance.
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 === 'undefined' ? 'undefined' : _typeof(superClass)));
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) {
if (Object.setPrototypeOf) {
Object.setPrototypeOf(subClass, superClass);
} else {
subClass.__proto__ = superClass; // eslint-disable-line no-proto
}
}
}
function enhanceWithRadium(configOrComposedComponent) {
var _class, _temp;
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (typeof configOrComposedComponent !== 'function') {
var newConfig = _extends({}, config, configOrComposedComponent);
return function (configOrComponent) {
return enhanceWithRadium(configOrComponent, newConfig);
};
}
var component = configOrComposedComponent;
var ComposedComponent = component;
// Handle Native ES classes.
if (isNativeClass(ComposedComponent)) {
// Manually approximate babel's class transpilation, but _with_ a real `new` call.
ComposedComponent = function (OrigComponent) {
function NewComponent() {
// Ordinarily, babel would produce something like:
//
// ```
// return _possibleConstructorReturn(this, OrigComponent.apply(this, arguments));
// ```
//
// Instead, we just call `new` directly without the `_possibleConstructorReturn` wrapper.
var source = new (Function.prototype.bind.apply(OrigComponent, [null].concat(Array.prototype.slice.call(arguments))))();
// Then we manually update context with properties.
copyProperties(source, this);
return this;
}
inherits(NewComponent, OrigComponent);
return NewComponent;
}(ComposedComponent);
}
// Handle stateless components
if (isStateless(ComposedComponent)) {
ComposedComponent = function (_Component) {
_inherits(ComposedComponent, _Component);
function ComposedComponent() {
_classCallCheck(this, ComposedComponent);
return _possibleConstructorReturn(this, _Component.apply(this, arguments));
}
ComposedComponent.prototype.render = function render() {
return component(this.props, this.context);
};
return ComposedComponent;
}(_react.Component);
ComposedComponent.displayName = component.displayName || component.name;
}
var RadiumEnhancer = (_temp = _class = function (_ComposedComponent) {
_inherits(RadiumEnhancer, _ComposedComponent);
function RadiumEnhancer() {
_classCallCheck(this, RadiumEnhancer);
var _this2 = _possibleConstructorReturn(this, _ComposedComponent.apply(this, arguments));
_this2.state = _this2.state || {};
_this2.state._radiumStyleState = {};
_this2._radiumIsMounted = true;
return _this2;
}
RadiumEnhancer.prototype.componentWillUnmount = function componentWillUnmount() {
if (_ComposedComponent.prototype.componentWillUnmount) {
_ComposedComponent.prototype.componentWillUnmount.call(this);
}
this._radiumIsMounted = false;
if (this._radiumMouseUpListener) {
this._radiumMouseUpListener.remove();
}
if (this._radiumMediaQueryListenersByQuery) {
Object.keys(this._radiumMediaQueryListenersByQuery).forEach(function (query) {
this._radiumMediaQueryListenersByQuery[query].remove();
}, this);
}
};
RadiumEnhancer.prototype.getChildContext = function getChildContext() {
var superChildContext = _ComposedComponent.prototype.getChildContext ? _ComposedComponent.prototype.getChildContext.call(this) : {};
if (!this.props.radiumConfig) {
return superChildContext;
}
var newContext = _extends({}, superChildContext);
if (this.props.radiumConfig) {
newContext._radiumConfig = this.props.radiumConfig;
}
return newContext;
};
RadiumEnhancer.prototype.render = function render() {
var renderedElement = _ComposedComponent.prototype.render.call(this);
var currentConfig = this.props.radiumConfig || this.context._radiumConfig || config;
if (config && currentConfig !== config) {
currentConfig = _extends({}, config, currentConfig);
}
return (0, _resolveStyles2.default)(this, renderedElement, currentConfig);
};
return RadiumEnhancer;
}(ComposedComponent), _class._isRadiumEnhanced = true, _temp);
// Class inheritance uses Object.create and because of __proto__ issues
// with IE <10 any static properties of the superclass aren't inherited and
// so need to be manually populated.
// See http://babeljs.io/docs/advanced/caveats/#classes-10-and-below-
copyProperties(component, RadiumEnhancer);
if (process.env.NODE_ENV !== 'production') {
// This also fixes React Hot Loader by exposing the original components top
// level prototype methods on the Radium enhanced prototype as discussed in
// https://github.com/FormidableLabs/radium/issues/219.
copyProperties(ComposedComponent.prototype, RadiumEnhancer.prototype);
}
if (RadiumEnhancer.propTypes && RadiumEnhancer.propTypes.style) {
RadiumEnhancer.propTypes = _extends({}, RadiumEnhancer.propTypes, {
style: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.object])
});
}
RadiumEnhancer.displayName = component.displayName || component.name || 'Component';
RadiumEnhancer.contextTypes = _extends({}, RadiumEnhancer.contextTypes, {
_radiumConfig: _propTypes2.default.object,
_radiumStyleKeeper: _propTypes2.default.instanceOf(_styleKeeper2.default)
});
RadiumEnhancer.childContextTypes = _extends({}, RadiumEnhancer.childContextTypes, {
_radiumConfig: _propTypes2.default.object,
_radiumStyleKeeper: _propTypes2.default.instanceOf(_styleKeeper2.default)
});
return RadiumEnhancer;
}
module.exports = exports['default'];