UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

43 lines (35 loc) 1.11 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; /** * This class is a surger to handle window beforeunload event. * * Example . * ```javascript * beforeunload.block(); // block user exit browser. * * doSomething() * * beforeunload.unblock(); // unblock user exit browser. * ``` */ class Beforeunload { constructor() { _defineProperty(this, "onbeforeunload", void 0); } block(options) { if (this.onbeforeunload) return; this.onbeforeunload = e => { // Cancel the event as stated by the standard. e.preventDefault(); // Chrome requires returnValue to be set. e.returnValue = options === null || options === void 0 ? void 0 : options.dialogText; return options === null || options === void 0 ? void 0 : options.dialogText; }; window.addEventListener('beforeunload', this.onbeforeunload); } unblock() { if (!this.onbeforeunload) return; window.removeEventListener('beforeunload', this.onbeforeunload); this.onbeforeunload = undefined; } } const Instance = new Beforeunload(); export default Instance;