"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = void 0; var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _scrollBehavior = _interopRequireDefault(require("scroll-behavior")); var _propTypes = _interopRequireDefault(require("prop-types")); var _history = require("@reach/router/lib/history"); var _StateStorage = _interopRequireDefault(require("./StateStorage")); var propTypes = { shouldUpdateScroll: _propTypes.default.func, children: _propTypes.default.element.isRequired, location: _propTypes.default.object.isRequired }; var childContextTypes = { scrollBehavior: _propTypes.default.object.isRequired }; var ScrollContext = /*#__PURE__*/ function (_React$Component) { (0, _inheritsLoose2.default)(ScrollContext, _React$Component); function ScrollContext(props, context) { var _this; _this = _React$Component.call(this, props, context) || this; (0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "shouldUpdateScroll", function (prevRouterProps, routerProps) { var shouldUpdateScroll = _this.props.shouldUpdateScroll; if (!shouldUpdateScroll) { return true; } // Hack to allow accessing scrollBehavior._stateStorage. return shouldUpdateScroll.call(_this.scrollBehavior, prevRouterProps, routerProps); }); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "registerElement", function (key, element, shouldUpdateScroll) { _this.scrollBehavior.registerElement(key, element, shouldUpdateScroll, _this.getRouterProps()); }); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "unregisterElement", function (key) { _this.scrollBehavior.unregisterElement(key); }); _this.scrollBehavior = new _scrollBehavior.default({ addTransitionHook: _history.globalHistory.listen, stateStorage: new _StateStorage.default(), getCurrentLocation: function getCurrentLocation() { return _this.props.location; }, shouldUpdateScroll: _this.shouldUpdateScroll }); return _this; } var _proto = ScrollContext.prototype; _proto.getChildContext = function getChildContext() { return { scrollBehavior: this }; }; _proto.componentDidUpdate = function componentDidUpdate(prevProps) { var location = this.props.location; var prevLocation = prevProps.location; if (location === prevLocation) { return; } var prevRouterProps = { location: prevProps.location // The "scroll-behavior" package expects the "action" to be on the location // object so let's copy it over. // Temp hack while awaiting https://github.com/reach/router/issues/119 }; if (window.__navigatingToLink) { location.action = "PUSH"; } else { location.action = "POP"; } this.scrollBehavior.updateScroll(prevRouterProps, { history: _history.globalHistory, location: location }); }; _proto.componentWillUnmount = function componentWillUnmount() { this.scrollBehavior.stop(); }; _proto.getRouterProps = function getRouterProps() { var location = this.props.location; return { location: location, history: _history.globalHistory }; }; _proto.render = function render() { return _react.default.Children.only(this.props.children); }; return ScrollContext; }(_react.default.Component); ScrollContext.propTypes = propTypes; ScrollContext.childContextTypes = childContextTypes; var _default = ScrollContext; exports.default = _default;