New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-router-prompt

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-router-prompt - npm Package Compare versions

Comparing version 0.2.2-beta to 0.3.0

6

dist/hooks/use-confirm.d.ts

@@ -0,1 +1,3 @@

import { Transition } from "history";
import { ReactRouterPromptProps } from "..";
declare interface InitialStateType {

@@ -7,6 +9,6 @@ isActive: boolean;

declare interface ConfirmLeaveReturnType extends InitialStateType {
onConfirm: () => Promise<boolean>;
onConfirm: (tx: Transition) => Promise<boolean>;
resetConfirmation: () => void;
}
declare const useConfirm: () => ConfirmLeaveReturnType;
declare const useConfirm: (when: ReactRouterPromptProps["when"]) => ConfirmLeaveReturnType;
export default useConfirm;
import React from "react";
declare type ReactRouterPromptProps = {
when: boolean;
import { Action, Location } from "history";
export declare type ReactRouterPromptProps = {
when: boolean | ((nextLocation: Location, _action: Action) => boolean);
children: (data: {

@@ -5,0 +6,0 @@ isActive: boolean;

@@ -409,2 +409,30 @@ 'use strict';

function useBlocker(blocker, when) {
if (when === void 0) {
when = true;
}
var _useContext = React.useContext(reactRouterDom.UNSAFE_NavigationContext),
navigator = _useContext.navigator;
React.useEffect(function () {
if (!when) return; // @ts-ignore
var unblock = navigator.block(function (tx) {
var autoUnblockingTx = _extends({}, tx, {
retry: function retry() {
// Automatically unblock the transition so it can play all the way
// through before retrying it. TODO: Figure out how to re-enable
// this block if the transition is cancelled for some reason.
unblock();
tx.retry();
}
});
blocker(autoUnblockingTx);
});
return unblock;
}, [navigator, blocker, when]);
}
var ConfirmContext = /*#__PURE__*/React__default.createContext(null);

@@ -437,3 +465,3 @@

var useConfirm = function useConfirm() {
var useConfirm = function useConfirm(when) {
var _useState = React.useState(initialConfirmState),

@@ -460,20 +488,54 @@ confirm = _useState[0],

}, []);
var onConfirm = /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
var onConfirm = React.useCallback( /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(tx) {
var promise;
return _regeneratorRuntime().wrap(function _callee$(_context) {
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) {
switch (_context.prev = _context.next) {
switch (_context2.prev = _context2.next) {
case 0:
promise = new Promise(function (resolve, reject) {
setConfirm(function (prevState) {
return _extends({}, prevState, {
isActive: true,
proceed: resolve,
cancel: reject
});
});
});
return _context.abrupt("return", promise.then(function () {
promise = new Promise( /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(resolve, reject) {
var shouldPrompt;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
setConfirm(function (prevState) {
return _extends({}, prevState, {
isActive: true,
proceed: resolve,
cancel: reject
});
}); // Go ahead and resolve the promise when the `when` function
// returns `false`, which means the prompt should not be displayed
// and navigation should occur.
if (!(typeof when === "function")) {
_context.next = 6;
break;
}
_context.next = 4;
return when(tx.location, tx.action);
case 4:
shouldPrompt = _context.sent;
if (!shouldPrompt) {
resolve(true);
}
case 6:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function (_x2, _x3) {
return _ref3.apply(this, arguments);
};
}());
return _context2.abrupt("return", promise.then(function () {
setResolve == null ? void 0 : setResolve(true);

@@ -494,13 +556,12 @@ setConfirm(_extends({}, confirm, {

case "end":
return _context.stop();
return _context2.stop();
}
}
}, _callee);
}, _callee2);
}));
return function onConfirm() {
return function (_x) {
return _ref2.apply(this, arguments);
};
}();
}(), [confirm, setResolve, when]);
return _extends({}, confirm, {

@@ -512,30 +573,2 @@ onConfirm: onConfirm,

function useBlocker(blocker, when) {
if (when === void 0) {
when = true;
}
var _useContext = React.useContext(reactRouterDom.UNSAFE_NavigationContext),
navigator = _useContext.navigator;
React.useEffect(function () {
if (!when) return; // @ts-ignore
var unblock = navigator.block(function (tx) {
var autoUnblockingTx = _extends({}, tx, {
retry: function retry() {
// Automatically unblock the transition so it can play all the way
// through before retrying it. TODO: Figure out how to re-enable
// this block if the transition is cancelled for some reason.
unblock();
tx.retry();
}
});
blocker(autoUnblockingTx);
});
return unblock;
}, [navigator, blocker, when]);
}
/**

@@ -563,3 +596,3 @@ * A replacement component for the react-router `Prompt`.

var _useConfirm = useConfirm(),
var _useConfirm = useConfirm(when),
onConfirm = _useConfirm.onConfirm,

@@ -574,7 +607,5 @@ resetConfirmation = _useConfirm.resetConfirmation,

var blocker = React.useCallback(
/*#__PURE__*/
// @ts-ignore
function () {
var blocker = React.useCallback( /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(tx) {
var result;
return _regeneratorRuntime().wrap(function _callee$(_context) {

@@ -585,14 +616,13 @@ while (1) {

_context.next = 2;
return onConfirm();
return onConfirm(tx);
case 2:
if (!_context.sent) {
_context.next = 5;
break;
result = _context.sent;
if (result) {
resetConfirmation();
tx.retry();
}
resetConfirmation();
tx.retry();
case 5:
case 4:
case "end":

@@ -608,3 +638,4 @@ return _context.stop();

};
}(), [resetConfirmation, onConfirm]);
}(), [resetConfirmation, onConfirm]); // TODO: Check fif effect is required to reset resolution if "when" is changed.
useBlocker(blocker, when && !resolve);

@@ -611,0 +642,0 @@ return React__default.createElement("div", null, children({

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("react"),r=(t=e)&&"object"==typeof t&&"default"in t?t.default:t,n=require("react-router-dom");function o(){o=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},i=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",c=n.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function f(t,e,r,n){var o=Object.create((e&&e.prototype instanceof h?e:h).prototype),i=new O(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=b(a,r);if(c){if(c===l)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=s(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,i),o}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=f;var l={};function h(){}function v(){}function p(){}var d={};u(d,i,(function(){return this}));var y=Object.getPrototypeOf,g=y&&y(y(_([])));g&&g!==e&&r.call(g,i)&&(d=g);var m=p.prototype=h.prototype=Object.create(d);function w(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function x(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,c){var u=s(t[o],t,i);if("throw"!==u.type){var f=u.arg,l=f.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(l).then((function(t){f.value=t,a(f)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function b(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,b(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function E(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function L(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function O(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(E,this),this.reset(!0)}function _(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:j}}function j(){return{value:void 0,done:!0}}return v.prototype=p,u(m,"constructor",p),u(p,"constructor",v),v.displayName=u(p,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,u(t,c,"GeneratorFunction")),t.prototype=Object.create(m),t},t.awrap=function(t){return{__await:t}},w(x.prototype),u(x.prototype,a,(function(){return this})),t.AsyncIterator=x,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new x(f(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},w(m),u(m,c,"Generator"),u(m,i,(function(){return this})),u(m,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=_,O.prototype={constructor:O,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(L),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),L(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;L(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:_(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function a(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function c(t){i(a,n,o,c,u,"next",t)}function u(t){i(a,n,o,c,u,"throw",t)}c(void 0)}))}}function c(){return(c=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}var u=r.createContext(null),f=function(t){var n=t.children,o=e.useState(!1);return r.createElement(u.Provider,{value:{resolve:o[0],setResolve:o[1]}},n)},s=function(){},l={isActive:!1,proceed:s,cancel:s},h=function(t){var i=t.when,f=t.children,s=function(){var t=e.useState(l),r=t[0],n=t[1],i=(e.useContext(u)||{}).setResolve;e.useEffect((function(){return r.isActive&&(window.onbeforeunload=function(){return!1}),function(){r.isActive&&(window.onbeforeunload=null)}}),[r]);var f=e.useCallback((function(){n(l)}),[]),s=function(){var t=a(o().mark((function t(){var e;return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=new Promise((function(t,e){n((function(r){return c({},r,{isActive:!0,proceed:t,cancel:e})}))})),t.abrupt("return",e.then((function(){return null==i||i(!0),n(c({},r,{isActive:!1})),!0}),(function(){return n(c({},r,{isActive:!1})),null==i||i(!1),!1})));case 2:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}();return c({},r,{onConfirm:s,resetConfirmation:f})}(),h=s.onConfirm,v=s.resetConfirmation,p=s.isActive,d=s.proceed,y=s.cancel,g=(e.useContext(u)||{}).resolve;return function(t,r){void 0===r&&(r=!0);var o=e.useContext(n.UNSAFE_NavigationContext).navigator;e.useEffect((function(){if(r){var e=o.block((function(r){var n=c({},r,{retry:function(){e(),r.retry()}});t(n)}));return e}}),[o,t,r])}(e.useCallback(function(){var t=a(o().mark((function t(e){return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,h();case 2:if(!t.sent){t.next=5;break}v(),e.retry();case 5:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),[v,h]),i&&!g),r.createElement("div",null,f({isActive:p,onConfirm:d,onCancel:y}))};exports.default=function(t){return r.createElement(f,null,r.createElement(h,Object.assign({},t)))};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("react"),r=(t=e)&&"object"==typeof t&&"default"in t?t.default:t,n=require("react-router-dom");function o(){o=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},i=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",c=n.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function f(t,e,r,n){var o=Object.create((e&&e.prototype instanceof h?e:h).prototype),i=new O(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=b(a,r);if(c){if(c===l)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=s(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,i),o}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=f;var l={};function h(){}function v(){}function p(){}var d={};u(d,i,(function(){return this}));var y=Object.getPrototypeOf,m=y&&y(y(_([])));m&&m!==e&&r.call(m,i)&&(d=m);var g=p.prototype=h.prototype=Object.create(d);function w(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function x(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,c){var u=s(t[o],t,i);if("throw"!==u.type){var f=u.arg,l=f.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(l).then((function(t){f.value=t,a(f)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function b(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,b(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function E(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function L(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function O(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(E,this),this.reset(!0)}function _(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:j}}function j(){return{value:void 0,done:!0}}return v.prototype=p,u(g,"constructor",p),u(p,"constructor",v),v.displayName=u(p,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,u(t,c,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},w(x.prototype),u(x.prototype,a,(function(){return this})),t.AsyncIterator=x,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new x(f(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},w(g),u(g,c,"Generator"),u(g,i,(function(){return this})),u(g,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=_,O.prototype={constructor:O,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(L),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),L(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;L(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:_(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function a(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function c(t){i(a,n,o,c,u,"next",t)}function u(t){i(a,n,o,c,u,"throw",t)}c(void 0)}))}}function c(){return(c=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}var u=r.createContext(null),f=function(t){var n=t.children,o=e.useState(!1);return r.createElement(u.Provider,{value:{resolve:o[0],setResolve:o[1]}},n)},s=function(){},l={isActive:!1,proceed:s,cancel:s},h=function(t){var i=t.when,f=t.children,s=function(t){var r=e.useState(l),n=r[0],i=r[1],f=(e.useContext(u)||{}).setResolve;e.useEffect((function(){return n.isActive&&(window.onbeforeunload=function(){return!1}),function(){n.isActive&&(window.onbeforeunload=null)}}),[n]);var s=e.useCallback((function(){i(l)}),[]),h=e.useCallback(function(){var e=a(o().mark((function e(r){var u;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return u=new Promise(function(){var e=a(o().mark((function e(n,a){return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(i((function(t){return c({},t,{isActive:!0,proceed:n,cancel:a})})),"function"!=typeof t){e.next=6;break}return e.next=4,t(r.location,r.action);case 4:e.sent||n(!0);case 6:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}()),e.abrupt("return",u.then((function(){return null==f||f(!0),i(c({},n,{isActive:!1})),!0}),(function(){return i(c({},n,{isActive:!1})),null==f||f(!1),!1})));case 2:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),[n,f,t]);return c({},n,{onConfirm:h,resetConfirmation:s})}(i),h=s.onConfirm,v=s.resetConfirmation,p=s.isActive,d=s.proceed,y=s.cancel,m=(e.useContext(u)||{}).resolve;return function(t,r){void 0===r&&(r=!0);var o=e.useContext(n.UNSAFE_NavigationContext).navigator;e.useEffect((function(){if(r){var e=o.block((function(r){var n=c({},r,{retry:function(){e(),r.retry()}});t(n)}));return e}}),[o,t,r])}(e.useCallback(function(){var t=a(o().mark((function t(e){return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,h(e);case 2:t.sent&&(v(),e.retry());case 4:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),[v,h]),i&&!m),r.createElement("div",null,f({isActive:p,onConfirm:d,onCancel:y}))};exports.default=function(t){return r.createElement(f,null,r.createElement(h,Object.assign({},t)))};
//# sourceMappingURL=react-router-prompt.cjs.production.min.js.map

@@ -1,2 +0,2 @@

import React, { useState, useContext, useEffect, useCallback } from 'react';
import React, { useContext, useEffect, useState, useCallback } from 'react';
import { UNSAFE_NavigationContext } from 'react-router-dom';

@@ -402,2 +402,30 @@

function useBlocker(blocker, when) {
if (when === void 0) {
when = true;
}
var _useContext = useContext(UNSAFE_NavigationContext),
navigator = _useContext.navigator;
useEffect(function () {
if (!when) return; // @ts-ignore
var unblock = navigator.block(function (tx) {
var autoUnblockingTx = _extends({}, tx, {
retry: function retry() {
// Automatically unblock the transition so it can play all the way
// through before retrying it. TODO: Figure out how to re-enable
// this block if the transition is cancelled for some reason.
unblock();
tx.retry();
}
});
blocker(autoUnblockingTx);
});
return unblock;
}, [navigator, blocker, when]);
}
var ConfirmContext = /*#__PURE__*/React.createContext(null);

@@ -430,3 +458,3 @@

var useConfirm = function useConfirm() {
var useConfirm = function useConfirm(when) {
var _useState = useState(initialConfirmState),

@@ -453,20 +481,54 @@ confirm = _useState[0],

}, []);
var onConfirm = /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
var onConfirm = useCallback( /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(tx) {
var promise;
return _regeneratorRuntime().wrap(function _callee$(_context) {
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) {
switch (_context.prev = _context.next) {
switch (_context2.prev = _context2.next) {
case 0:
promise = new Promise(function (resolve, reject) {
setConfirm(function (prevState) {
return _extends({}, prevState, {
isActive: true,
proceed: resolve,
cancel: reject
});
});
});
return _context.abrupt("return", promise.then(function () {
promise = new Promise( /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(resolve, reject) {
var shouldPrompt;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
setConfirm(function (prevState) {
return _extends({}, prevState, {
isActive: true,
proceed: resolve,
cancel: reject
});
}); // Go ahead and resolve the promise when the `when` function
// returns `false`, which means the prompt should not be displayed
// and navigation should occur.
if (!(typeof when === "function")) {
_context.next = 6;
break;
}
_context.next = 4;
return when(tx.location, tx.action);
case 4:
shouldPrompt = _context.sent;
if (!shouldPrompt) {
resolve(true);
}
case 6:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function (_x2, _x3) {
return _ref3.apply(this, arguments);
};
}());
return _context2.abrupt("return", promise.then(function () {
setResolve == null ? void 0 : setResolve(true);

@@ -487,13 +549,12 @@ setConfirm(_extends({}, confirm, {

case "end":
return _context.stop();
return _context2.stop();
}
}
}, _callee);
}, _callee2);
}));
return function onConfirm() {
return function (_x) {
return _ref2.apply(this, arguments);
};
}();
}(), [confirm, setResolve, when]);
return _extends({}, confirm, {

@@ -505,30 +566,2 @@ onConfirm: onConfirm,

function useBlocker(blocker, when) {
if (when === void 0) {
when = true;
}
var _useContext = useContext(UNSAFE_NavigationContext),
navigator = _useContext.navigator;
useEffect(function () {
if (!when) return; // @ts-ignore
var unblock = navigator.block(function (tx) {
var autoUnblockingTx = _extends({}, tx, {
retry: function retry() {
// Automatically unblock the transition so it can play all the way
// through before retrying it. TODO: Figure out how to re-enable
// this block if the transition is cancelled for some reason.
unblock();
tx.retry();
}
});
blocker(autoUnblockingTx);
});
return unblock;
}, [navigator, blocker, when]);
}
/**

@@ -556,3 +589,3 @@ * A replacement component for the react-router `Prompt`.

var _useConfirm = useConfirm(),
var _useConfirm = useConfirm(when),
onConfirm = _useConfirm.onConfirm,

@@ -567,7 +600,5 @@ resetConfirmation = _useConfirm.resetConfirmation,

var blocker = useCallback(
/*#__PURE__*/
// @ts-ignore
function () {
var blocker = useCallback( /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(tx) {
var result;
return _regeneratorRuntime().wrap(function _callee$(_context) {

@@ -578,14 +609,13 @@ while (1) {

_context.next = 2;
return onConfirm();
return onConfirm(tx);
case 2:
if (!_context.sent) {
_context.next = 5;
break;
result = _context.sent;
if (result) {
resetConfirmation();
tx.retry();
}
resetConfirmation();
tx.retry();
case 5:
case 4:
case "end":

@@ -601,3 +631,4 @@ return _context.stop();

};
}(), [resetConfirmation, onConfirm]);
}(), [resetConfirmation, onConfirm]); // TODO: Check fif effect is required to reset resolution if "when" is changed.
useBlocker(blocker, when && !resolve);

@@ -604,0 +635,0 @@ return React.createElement("div", null, children({

{
"name": "react-router-prompt",
"version": "0.2.2-beta",
"version": "0.3.0",
"description": "React Router Navigation Prompt for v6",

@@ -37,3 +37,3 @@ "main": "dist/index.js",

"devDependencies": {
"@size-limit/preset-small-lib": "^7.0.8",
"@size-limit/preset-small-lib": "^8.1.0",
"@types/react": "^18.0.15",

@@ -48,3 +48,3 @@ "@types/react-dom": "^18.0.6",

"rimraf": "^3.0.2",
"size-limit": "^7.0.8",
"size-limit": "^8.1.0",
"tsdx": "^0.14.1",

@@ -51,0 +51,0 @@ "tslib": "^2.4.0",

import { useState, useEffect, useCallback, useContext } from "react";
import { ConfirmContext } from "../ConfirmContext";
import { Transition } from "history";
import { ReactRouterPromptProps } from "..";

@@ -21,7 +23,9 @@ const noop = () => {

declare interface ConfirmLeaveReturnType extends InitialStateType {
onConfirm: () => Promise<boolean>;
onConfirm: (tx: Transition) => Promise<boolean>;
resetConfirmation: () => void;
}
const useConfirm = (): ConfirmLeaveReturnType => {
const useConfirm = (
when: ReactRouterPromptProps["when"]
): ConfirmLeaveReturnType => {
const [confirm, setConfirm] = useState<InitialStateType>(initialConfirmState);

@@ -46,26 +50,39 @@ const { setResolve } = useContext(ConfirmContext) || {};

const onConfirm = async (): Promise<boolean> => {
const promise = new Promise((resolve, reject) => {
setConfirm((prevState: InitialStateType) => ({
...prevState,
isActive: true,
proceed: resolve,
cancel: reject,
}));
});
const onConfirm = useCallback(
async (tx: Transition): Promise<boolean> => {
const promise = new Promise(async (resolve, reject) => {
setConfirm((prevState: InitialStateType) => ({
...prevState,
isActive: true,
proceed: resolve,
cancel: reject,
}));
return promise.then(
() => {
setResolve?.(true);
setConfirm({ ...confirm, isActive: false });
return true;
},
() => {
setConfirm({ ...confirm, isActive: false });
setResolve?.(false);
return false;
}
);
};
// Go ahead and resolve the promise when the `when` function
// returns `false`, which means the prompt should not be displayed
// and navigation should occur.
if (typeof when === "function") {
const shouldPrompt = await when(tx.location, tx.action);
if (!shouldPrompt) {
resolve(true);
}
}
});
return promise.then(
() => {
setResolve?.(true);
setConfirm({ ...confirm, isActive: false });
return true;
},
() => {
setConfirm({ ...confirm, isActive: false });
setResolve?.(false);
return false;
}
);
},
[confirm, setResolve, when]
);
return {

@@ -72,0 +89,0 @@ ...confirm,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc