react-router-last-location
Version:
Provides access to the last location in react + react-router (v4.x) apps. Useful for handling internal routing. Easily prevent leaving your app by users.
376 lines (340 loc) • 13.7 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("react"), require("react-router-dom"), require("prop-types"), require("history"));
else if(typeof define === 'function' && define.amd)
define(["react", "react-router-dom", "prop-types", "history"], factory);
else if(typeof exports === 'object')
exports["ReactRouterLastLocation"] = factory(require("react"), require("react-router-dom"), require("prop-types"), require("history"));
else
root["ReactRouterLastLocation"] = factory(root["react"], root["react-router-dom"], root["prop-types"], root["history"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE__0__, __WEBPACK_EXTERNAL_MODULE__1__, __WEBPACK_EXTERNAL_MODULE__6__, __WEBPACK_EXTERNAL_MODULE__10__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 4);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE__0__;
/***/ }),
/* 1 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE__1__;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __webpack_require__(0);
var PropTypes = __webpack_require__(6);
var react_router_dom_1 = __webpack_require__(1);
var LastLocationContext_1 = __webpack_require__(3);
var prevent_1 = __webpack_require__(7);
var lastLocation = null;
var updateLastLocation = function (_a) {
var location = _a.location, nextLocation = _a.nextLocation, watchOnlyPathname = _a.watchOnlyPathname;
if (location === null) {
return;
}
if (nextLocation === location) {
return;
}
if (watchOnlyPathname && location.pathname === nextLocation.pathname) {
return;
}
if (prevent_1.shouldPrevent(nextLocation) && !prevent_1.hasBeenPrevented(nextLocation)) {
prevent_1.prevent(nextLocation);
return;
}
lastLocation = __assign({}, location);
};
var LastLocationProvider = /** @class */ (function (_super) {
__extends(LastLocationProvider, _super);
function LastLocationProvider() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
currentLocation: null,
};
return _this;
}
LastLocationProvider.getDerivedStateFromProps = function (props, state) {
updateLastLocation({
location: state.currentLocation,
nextLocation: props.location,
watchOnlyPathname: props.watchOnlyPathname,
});
return {
currentLocation: props.location,
};
};
LastLocationProvider.prototype.render = function () {
var children = this.props.children;
return (React.createElement(LastLocationContext_1.default.Provider, { value: lastLocation }, children));
};
LastLocationProvider.propTypes = {
watchOnlyPathname: PropTypes.bool,
children: PropTypes.node.isRequired,
};
LastLocationProvider.defaultProps = {
watchOnlyPathname: false,
};
return LastLocationProvider;
}(React.Component));
exports.getLastLocation = function () { return lastLocation; };
exports.setLastLocation = function (nextLastLocation) {
lastLocation = nextLastLocation;
};
exports.default = react_router_dom_1.withRouter(LastLocationProvider);
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __webpack_require__(0);
var LastLocationContext = react_1.createContext(null);
exports.default = LastLocationContext;
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", { value: true });
var withLastLocation_1 = __webpack_require__(5);
exports.withLastLocation = withLastLocation_1.default;
var LastLocationProvider_1 = __webpack_require__(2);
exports.LastLocationProvider = LastLocationProvider_1.default;
var useLastLocation_1 = __webpack_require__(8);
exports.useLastLocation = useLastLocation_1.default;
var RedirectWithoutLastLocation_1 = __webpack_require__(9);
exports.RedirectWithoutLastLocation = RedirectWithoutLastLocation_1.default;
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __webpack_require__(0);
var react_router_dom_1 = __webpack_require__(1);
var LastLocationProvider_1 = __webpack_require__(2);
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
var withLastLocation = function (WrappedComponent) {
var WithLastLocation = function (props) { return (React.createElement(WrappedComponent, __assign({ lastLocation: LastLocationProvider_1.getLastLocation() }, props))); };
WithLastLocation.displayName = "WithLastLocation(" + getDisplayName(WrappedComponent) + ")";
return react_router_dom_1.withRouter(WithLastLocation);
};
exports.default = withLastLocation;
/***/ }),
/* 6 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE__6__;
/***/ }),
/* 7 */
/***/ (function(module, exports, __webpack_require__) {
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* I could only check `key` here but according to:
* @see https://reacttraining.com/react-router/web/api/location
* `key` property is not available when HashHistory is used.
*/
var props = ['key', 'pathname', 'search', 'hash'];
var isEqual = function (a, b) { return props.every(function (prop) { return a[prop] === b[prop]; }); };
var prevented = [];
exports.prevent = function (location) {
var state = location.state, rest = __rest(location, ["state"]);
prevented.push(rest);
};
exports.hasBeenPrevented = function (location) {
return prevented.some(function (preventedLocation) { return isEqual(location, preventedLocation); });
};
exports.shouldPrevent = function (location) {
return Boolean(location.state && location.state.preventLastLocation);
};
/***/ }),
/* 8 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __webpack_require__(0);
var LastLocationContext_1 = __webpack_require__(3);
function useLastLocation() {
return react_1.useContext(LastLocationContext_1.default);
}
exports.default = useLastLocation;
/***/ }),
/* 9 */
/***/ (function(module, exports, __webpack_require__) {
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __webpack_require__(0);
var react_router_dom_1 = __webpack_require__(1);
var history_1 = __webpack_require__(10);
var RedirectWithoutLastLocation = function (_a) {
var to = _a.to, rest = __rest(_a, ["to"]);
var finalTo;
if (typeof to === 'string') {
finalTo = history_1.createLocation(to, { preventLastLocation: true });
}
else {
finalTo = __assign({}, to, { state: __assign({ preventLastLocation: true }, to.state) });
}
return React.createElement(react_router_dom_1.Redirect, __assign({}, rest, { to: finalTo }));
};
exports.default = RedirectWithoutLastLocation;
/***/ }),
/* 10 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE__10__;
/***/ })
/******/ ]);
});