react-select
Version:
A Select control built with and for ReactJS
150 lines (114 loc) • 5.91 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; }; }();
var _react = require('react');
var _constants = require('./constants');
var _utils = require('./utils');
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 canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement);
var activeScrollLocks = 0;
var ScrollLock = function (_Component) {
_inherits(ScrollLock, _Component);
function ScrollLock() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, ScrollLock);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ScrollLock.__proto__ || Object.getPrototypeOf(ScrollLock)).call.apply(_ref, [this].concat(args))), _this), _this.originalStyles = {}, _this.listenerOptions = {
capture: false,
passive: false
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(ScrollLock, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
if (!canUseDOM) return;
var _props = this.props,
accountForScrollbars = _props.accountForScrollbars,
touchScrollTarget = _props.touchScrollTarget;
var target = document.body;
var targetStyle = target && target.style;
if (accountForScrollbars) {
// store any styles already applied to the body
_constants.STYLE_KEYS.forEach(function (key) {
var val = targetStyle && targetStyle[key];
_this2.originalStyles[key] = val;
});
}
// apply the lock styles and padding if this is the first scroll lock
if (accountForScrollbars && activeScrollLocks < 1) {
var currentPadding = parseInt(this.originalStyles.paddingRight, 10) || 0;
var clientWidth = document.body ? document.body.clientWidth : 0;
var adjustedPadding = window.innerWidth - clientWidth + currentPadding || 0;
Object.keys(_constants.LOCK_STYLES).forEach(function (key) {
var val = _constants.LOCK_STYLES[key];
if (targetStyle) {
targetStyle[key] = val;
}
});
if (targetStyle) {
targetStyle.paddingRight = adjustedPadding + 'px';
}
}
// account for touch devices
if (target && (0, _utils.isTouchDevice)()) {
// Mobile Safari ignores { overflow: hidden } declaration on the body.
target.addEventListener('touchmove', _utils.preventTouchMove, this.listenerOptions);
// Allow scroll on provided target
if (touchScrollTarget) {
touchScrollTarget.addEventListener('touchstart', _utils.preventInertiaScroll, this.listenerOptions);
touchScrollTarget.addEventListener('touchmove', _utils.allowTouchMove, this.listenerOptions);
}
}
// increment active scroll locks
activeScrollLocks += 1;
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
var _this3 = this;
if (!canUseDOM) return;
var _props2 = this.props,
accountForScrollbars = _props2.accountForScrollbars,
touchScrollTarget = _props2.touchScrollTarget;
var target = document.body;
var targetStyle = target && target.style;
// safely decrement active scroll locks
activeScrollLocks = Math.max(activeScrollLocks - 1, 0);
// reapply original body styles, if any
if (accountForScrollbars && activeScrollLocks < 1) {
_constants.STYLE_KEYS.forEach(function (key) {
var val = _this3.originalStyles[key];
if (targetStyle) {
targetStyle[key] = val;
}
});
}
// remove touch listeners
if (target && (0, _utils.isTouchDevice)()) {
target.removeEventListener('touchmove', _utils.preventTouchMove, this.listenerOptions);
if (touchScrollTarget) {
touchScrollTarget.removeEventListener('touchstart', _utils.preventInertiaScroll, this.listenerOptions);
touchScrollTarget.removeEventListener('touchmove', _utils.allowTouchMove, this.listenerOptions);
}
}
}
}, {
key: 'render',
value: function render() {
return null;
}
}]);
return ScrollLock;
}(_react.Component);
ScrollLock.defaultProps = {
accountForScrollbars: true
};
exports.default = ScrollLock;