@edpi/react-scroll-position
Version:
Remember and restore React scroll position for the nodes.
190 lines (155 loc) • 8.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.memoryStore = void 0;
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; 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); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _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); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function getScrollPosition(target) {
if (target instanceof window.Window) {
return {
x: target.scrollX,
y: target.scrollY
};
}
return {
x: target.scrollLeft,
y: target.scrollTop
};
}
function scroll(target, x, y) {
if (target instanceof window.Window) {
target.scrollTo(x, y);
} else {
target.scrollLeft = x;
target.scrollTop = y;
}
}
var memoryStore = {
data: new Map(),
get: function get(key) {
if (!key) {
return null;
}
return this.data.get(key) || null;
},
set: function set(key, data) {
if (!key) {
return;
}
return this.data.set(key, data);
}
};
exports.memoryStore = memoryStore;
var ScrollPosition = /*#__PURE__*/function (_React$Component) {
_inherits(ScrollPosition, _React$Component);
var _super = _createSuper(ScrollPosition);
function ScrollPosition() {
var _this;
_classCallCheck(this, ScrollPosition);
_this = _super.apply(this, arguments);
_this.attachScrollNode = _this.attachScrollNode.bind(_assertThisInitialized(_this));
_this.getScroll = _this.getScroll.bind(_assertThisInitialized(_this));
_this.getScrollNode = _this.getScrollNode.bind(_assertThisInitialized(_this));
_this.setScroll = _this.setScroll.bind(_assertThisInitialized(_this));
_this._target = window;
return _this;
}
_createClass(ScrollPosition, [{
key: "attachScrollNode",
value: function attachScrollNode(node) {
this._target = node;
}
}, {
key: "getScroll",
value: function getScroll() {
if (this._target) {
return getScrollPosition(this._target);
}
}
}, {
key: "getScrollNode",
value: function getScrollNode() {
if (this._target) {
return this._target;
}
}
}, {
key: "setScroll",
value: function setScroll(pos) {
pos = pos || this.props.store.get(this.props.scrollKey);
if (this._target && pos) {
scroll(this._target, pos.x, pos.y);
}
}
}, {
key: "saveScroll",
value: function saveScroll(key) {
if (this._target) {
var pos = getScrollPosition(this._target);
key = key || this.props.scrollKey;
this.props.store.set(key, pos);
}
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
this.setScroll();
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if (this.props.scrollKey !== nextProps.scrollKey) {
this.saveScroll();
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (this.props.scrollKey !== prevProps.scrollKey) {
this.setScroll();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.saveScroll();
delete this._target;
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
_this$props$children = _this$props.children,
children = _this$props$children === void 0 ? null : _this$props$children,
props = _objectWithoutProperties(_this$props, ["children"]);
return children && children(_objectSpread(_objectSpread({}, props), {}, {
attachScrollNode: this.attachScrollNode,
getScroll: this.getScroll,
getScrollNode: this.getScrollNode,
setScroll: this.setScroll
}));
}
}]);
return ScrollPosition;
}(_react["default"].Component);
exports["default"] = ScrollPosition;
ScrollPosition.defaultProps = {
store: memoryStore
};