react-router-prompt
Advanced tools
Comparing version
declare interface InitialStateType { | ||
isActive: boolean; | ||
hasConfirmed: boolean; | ||
proceed: (value: unknown) => void; | ||
@@ -5,0 +4,0 @@ cancel: (value: unknown) => void; |
@@ -10,20 +10,3 @@ import React from "react"; | ||
}; | ||
/** | ||
* A replacement component for the react-router `Prompt`. | ||
* Allows for more flexible dialogs. | ||
* | ||
* @example | ||
* <ReactRouterPrompt when={isDirty}> | ||
* {({isActive, onConfirm, onCancel}) => ( | ||
* <Modal show={isActive}> | ||
* <div> | ||
* <p>Do you really want to leave?</p> | ||
* <button onClick={onCancel}>Cancel</button> | ||
* <button onClick={onConfirm}>Ok</button> | ||
* </div> | ||
* </Modal> | ||
* )} | ||
* </ReactRouterPrompt> | ||
*/ | ||
declare const ReactRouterPrompt: React.FC<ReactRouterPromptProps>; | ||
export default ReactRouterPrompt; | ||
declare const Main: React.FC<ReactRouterPromptProps>; | ||
export default Main; |
@@ -409,2 +409,19 @@ 'use strict'; | ||
var ConfirmContext = /*#__PURE__*/React__default.createContext(null); | ||
var ConfirmContextProvider = function ConfirmContextProvider(_ref) { | ||
var children = _ref.children; | ||
var _useState = React.useState(false), | ||
resolve = _useState[0], | ||
setResolve = _useState[1]; | ||
return React__default.createElement(ConfirmContext.Provider, { | ||
value: { | ||
resolve: resolve, | ||
setResolve: setResolve | ||
} | ||
}, children); | ||
}; | ||
var noop = function noop() { | ||
@@ -416,3 +433,2 @@ /*No Operation*/ | ||
isActive: false, | ||
hasConfirmed: false, | ||
proceed: noop, | ||
@@ -427,2 +443,5 @@ cancel: noop | ||
var _ref = React.useContext(ConfirmContext) || {}, | ||
setResolve = _ref.setResolve; | ||
React.useEffect(function () { | ||
@@ -439,28 +458,49 @@ if (confirm.isActive) { | ||
}, [confirm]); | ||
var onConfirm = React.useCallback(function () { | ||
var promise = new Promise(function (resolve, reject) { | ||
setConfirm(function (prevState) { | ||
return _extends({}, prevState, { | ||
isActive: true, | ||
proceed: resolve, | ||
cancel: reject | ||
}); | ||
}); | ||
}); | ||
return promise.then(function () { | ||
setConfirm(_extends({}, confirm, { | ||
isActive: false, | ||
hasConfirmed: true | ||
})); | ||
return true; | ||
}, function () { | ||
setConfirm(_extends({}, confirm, { | ||
isActive: false | ||
})); | ||
return false; | ||
}); | ||
}, [confirm]); | ||
var resetConfirmation = React.useCallback(function () { | ||
setConfirm(initialConfirmState); | ||
}, []); | ||
var onConfirm = /*#__PURE__*/function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() { | ||
var promise; | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.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 () { | ||
setResolve == null ? void 0 : setResolve(true); | ||
setConfirm(_extends({}, confirm, { | ||
isActive: false | ||
})); | ||
return true; | ||
}, function () { | ||
setConfirm(_extends({}, confirm, { | ||
isActive: false | ||
})); | ||
setResolve == null ? void 0 : setResolve(false); | ||
return false; | ||
})); | ||
case 2: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return function onConfirm() { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
return _extends({}, confirm, { | ||
@@ -523,9 +563,11 @@ onConfirm: onConfirm, | ||
var _useConfirm = useConfirm(), | ||
onConfirm = _useConfirm.onConfirm, | ||
resetConfirmation = _useConfirm.resetConfirmation, | ||
isActive = _useConfirm.isActive, | ||
proceed = _useConfirm.proceed, | ||
cancel = _useConfirm.cancel, | ||
onConfirm = _useConfirm.onConfirm, | ||
hasConfirmed = _useConfirm.hasConfirmed, | ||
resetConfirmation = _useConfirm.resetConfirmation; | ||
cancel = _useConfirm.cancel; | ||
var _ref2 = React.useContext(ConfirmContext) || {}, | ||
resolve = _ref2.resolve; | ||
var blocker = React.useCallback( | ||
@@ -535,3 +577,3 @@ /*#__PURE__*/ | ||
function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(tx) { | ||
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(tx) { | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
@@ -562,6 +604,6 @@ while (1) { | ||
return function (_x) { | ||
return _ref2.apply(this, arguments); | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}(), [resetConfirmation, onConfirm]); | ||
useBlocker(blocker, when && !hasConfirmed); | ||
useBlocker(blocker, when && !resolve); | ||
return React__default.createElement("div", null, children({ | ||
@@ -574,3 +616,7 @@ isActive: isActive, | ||
exports.default = ReactRouterPrompt; | ||
var Main = function Main(props) { | ||
return React__default.createElement(ConfirmContextProvider, null, React__default.createElement(ReactRouterPrompt, Object.assign({}, props))); | ||
}; | ||
exports.default = Main; | ||
//# sourceMappingURL=react-router-prompt.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,r=require("react"),e=(t=r)&&"object"==typeof t&&"default"in t?t.default:t,n=require("react-router-dom");function o(){o=function(){return t};var t={},r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},i=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",c=n.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function f(t,r,e,n){var o=Object.create((r&&r.prototype instanceof h?r:h).prototype),i=new _(n||[]);return o._invoke=function(t,r,e){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(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var c=x(a,e);if(c){if(c===l)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var u=s(t,r,e);if("normal"===u.type){if(n=e.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n="completed",e.method="throw",e.arg=u.arg)}}}(t,e,i),o}function s(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}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(O([])));g&&g!==r&&e.call(g,i)&&(d=g);var m=p.prototype=h.prototype=Object.create(d);function w(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function b(t,r){var n;this._invoke=function(o,i){function a(){return new r((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&&e.call(l,"__await")?r.resolve(l.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):r.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 x(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,x(t,r),"throw"===r.method))return l;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=s(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,l;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,l):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,l)}function L(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function E(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(L,this),this.reset(!0)}function O(t){if(t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function r(){for(;++n<t.length;)if(e.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};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 r="function"==typeof t&&t.constructor;return!!r&&(r===v||"GeneratorFunction"===(r.displayName||r.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(b.prototype),u(b.prototype,a,(function(){return this})),t.AsyncIterator=b,t.async=function(r,e,n,o,i){void 0===i&&(i=Promise);var a=new b(f(r,e,n,o),i);return t.isGeneratorFunction(e)?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 r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=O,_.prototype={constructor:_,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(E),!t)for(var r in this)"t"===r.charAt(0)&&e.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=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 r=this;function n(e,n){return a.type="throw",a.arg=t,r.next=e,n&&(r.method="next",r.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=e.call(i,"catchLoc"),u=e.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,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,r){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&&r&&(this.next=r),l},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),E(e),l}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;E(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:O(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,r,e,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).then(n,o)}function a(){return(a=Object.assign?Object.assign.bind():function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(this,arguments)}var c=function(){},u={isActive:!1,hasConfirmed:!1,proceed:c,cancel:c};exports.default=function(t){var c=t.when,f=t.children,s=function(){var t=r.useState(u),e=t[0],n=t[1];r.useEffect((function(){return e.isActive&&(window.onbeforeunload=function(){return!1}),function(){e.isActive&&(window.onbeforeunload=null)}}),[e]);var o=r.useCallback((function(){return new Promise((function(t,r){n((function(e){return a({},e,{isActive:!0,proceed:t,cancel:r})}))})).then((function(){return n(a({},e,{isActive:!1,hasConfirmed:!0})),!0}),(function(){return n(a({},e,{isActive:!1})),!1}))}),[e]),i=r.useCallback((function(){n(u)}),[]);return a({},e,{onConfirm:o,resetConfirmation:i})}(),l=s.isActive,h=s.proceed,v=s.cancel,p=s.onConfirm,d=s.hasConfirmed,y=s.resetConfirmation;return function(t,e){void 0===e&&(e=!0);var o=r.useContext(n.UNSAFE_NavigationContext).navigator;r.useEffect((function(){if(e){var r=o.block((function(e){var n=a({},e,{retry:function(){r(),e.retry()}});t(n)}));return r}}),[o,t,e])}(r.useCallback(function(){var t,r=(t=o().mark((function t(r){return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,p();case 2:if(!t.sent){t.next=5;break}y(),r.retry();case 5:case"end":return t.stop()}}),t)})),function(){var r=this,e=arguments;return new Promise((function(n,o){var a=t.apply(r,e);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)}))});return function(t){return r.apply(this,arguments)}}(),[y,p]),c&&!d),e.createElement("div",null,f({isActive:l,onConfirm:h,onCancel:v}))}; | ||
"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)))}; | ||
//# sourceMappingURL=react-router-prompt.cjs.production.min.js.map |
@@ -1,2 +0,2 @@ | ||
import React, { useState, useEffect, useCallback, useContext } from 'react'; | ||
import React, { useState, useContext, useEffect, useCallback } from 'react'; | ||
import { UNSAFE_NavigationContext } from 'react-router-dom'; | ||
@@ -402,2 +402,19 @@ | ||
var ConfirmContext = /*#__PURE__*/React.createContext(null); | ||
var ConfirmContextProvider = function ConfirmContextProvider(_ref) { | ||
var children = _ref.children; | ||
var _useState = useState(false), | ||
resolve = _useState[0], | ||
setResolve = _useState[1]; | ||
return React.createElement(ConfirmContext.Provider, { | ||
value: { | ||
resolve: resolve, | ||
setResolve: setResolve | ||
} | ||
}, children); | ||
}; | ||
var noop = function noop() { | ||
@@ -409,3 +426,2 @@ /*No Operation*/ | ||
isActive: false, | ||
hasConfirmed: false, | ||
proceed: noop, | ||
@@ -420,2 +436,5 @@ cancel: noop | ||
var _ref = useContext(ConfirmContext) || {}, | ||
setResolve = _ref.setResolve; | ||
useEffect(function () { | ||
@@ -432,28 +451,49 @@ if (confirm.isActive) { | ||
}, [confirm]); | ||
var onConfirm = useCallback(function () { | ||
var promise = new Promise(function (resolve, reject) { | ||
setConfirm(function (prevState) { | ||
return _extends({}, prevState, { | ||
isActive: true, | ||
proceed: resolve, | ||
cancel: reject | ||
}); | ||
}); | ||
}); | ||
return promise.then(function () { | ||
setConfirm(_extends({}, confirm, { | ||
isActive: false, | ||
hasConfirmed: true | ||
})); | ||
return true; | ||
}, function () { | ||
setConfirm(_extends({}, confirm, { | ||
isActive: false | ||
})); | ||
return false; | ||
}); | ||
}, [confirm]); | ||
var resetConfirmation = useCallback(function () { | ||
setConfirm(initialConfirmState); | ||
}, []); | ||
var onConfirm = /*#__PURE__*/function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() { | ||
var promise; | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.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 () { | ||
setResolve == null ? void 0 : setResolve(true); | ||
setConfirm(_extends({}, confirm, { | ||
isActive: false | ||
})); | ||
return true; | ||
}, function () { | ||
setConfirm(_extends({}, confirm, { | ||
isActive: false | ||
})); | ||
setResolve == null ? void 0 : setResolve(false); | ||
return false; | ||
})); | ||
case 2: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return function onConfirm() { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
return _extends({}, confirm, { | ||
@@ -516,9 +556,11 @@ onConfirm: onConfirm, | ||
var _useConfirm = useConfirm(), | ||
onConfirm = _useConfirm.onConfirm, | ||
resetConfirmation = _useConfirm.resetConfirmation, | ||
isActive = _useConfirm.isActive, | ||
proceed = _useConfirm.proceed, | ||
cancel = _useConfirm.cancel, | ||
onConfirm = _useConfirm.onConfirm, | ||
hasConfirmed = _useConfirm.hasConfirmed, | ||
resetConfirmation = _useConfirm.resetConfirmation; | ||
cancel = _useConfirm.cancel; | ||
var _ref2 = useContext(ConfirmContext) || {}, | ||
resolve = _ref2.resolve; | ||
var blocker = useCallback( | ||
@@ -528,3 +570,3 @@ /*#__PURE__*/ | ||
function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(tx) { | ||
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(tx) { | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
@@ -555,6 +597,6 @@ while (1) { | ||
return function (_x) { | ||
return _ref2.apply(this, arguments); | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}(), [resetConfirmation, onConfirm]); | ||
useBlocker(blocker, when && !hasConfirmed); | ||
useBlocker(blocker, when && !resolve); | ||
return React.createElement("div", null, children({ | ||
@@ -567,3 +609,7 @@ isActive: isActive, | ||
export default ReactRouterPrompt; | ||
var Main = function Main(props) { | ||
return React.createElement(ConfirmContextProvider, null, React.createElement(ReactRouterPrompt, Object.assign({}, props))); | ||
}; | ||
export default Main; | ||
//# sourceMappingURL=react-router-prompt.esm.js.map |
{ | ||
"name": "react-router-prompt", | ||
"version": "0.2.1", | ||
"version": "0.2.2-beta", | ||
"description": "React Router Navigation Prompt for v6", | ||
@@ -27,6 +27,5 @@ "main": "dist/index.js", | ||
"test": "tsdx test", | ||
"lint": "tsdx lint", | ||
"lint": "tsdx lint src", | ||
"size": "size-limit", | ||
"prepublishOnly": "yarn build", | ||
"example": "cd example && yarn start" | ||
"prepublishOnly": "yarn build" | ||
}, | ||
@@ -33,0 +32,0 @@ "husky": { |
@@ -1,2 +0,3 @@ | ||
import { useState, useEffect, useCallback } from "react"; | ||
import { useState, useEffect, useCallback, useContext } from "react"; | ||
import { ConfirmContext } from "../ConfirmContext"; | ||
@@ -9,3 +10,2 @@ const noop = () => { | ||
isActive: false, | ||
hasConfirmed: false, | ||
proceed: noop, | ||
@@ -17,3 +17,2 @@ cancel: noop, | ||
isActive: boolean; | ||
hasConfirmed: boolean; | ||
proceed: (value: unknown) => void; | ||
@@ -30,2 +29,3 @@ cancel: (value: unknown) => void; | ||
const [confirm, setConfirm] = useState<InitialStateType>(initialConfirmState); | ||
const { setResolve } = useContext(ConfirmContext) || {}; | ||
@@ -44,3 +44,7 @@ useEffect(() => { | ||
const onConfirm = useCallback(() => { | ||
const resetConfirmation = useCallback(() => { | ||
setConfirm(initialConfirmState); | ||
}, []); | ||
const onConfirm = async (): Promise<boolean> => { | ||
const promise = new Promise((resolve, reject) => { | ||
@@ -57,3 +61,4 @@ setConfirm((prevState: InitialStateType) => ({ | ||
() => { | ||
setConfirm({ ...confirm, isActive: false, hasConfirmed: true }); | ||
setResolve?.(true); | ||
setConfirm({ ...confirm, isActive: false }); | ||
return true; | ||
@@ -63,11 +68,8 @@ }, | ||
setConfirm({ ...confirm, isActive: false }); | ||
setResolve?.(false); | ||
return false; | ||
} | ||
); | ||
}, [confirm]); | ||
}; | ||
const resetConfirmation = useCallback(() => { | ||
setConfirm(initialConfirmState); | ||
}, []); | ||
return { | ||
@@ -74,0 +76,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
87150
11.86%18
12.5%1301
8.15%