Skip to content

Commit 2a53417

Browse files
committed
Add onbeforeunload prompt while a swap is active
1 parent 9233a54 commit 2a53417

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/components/swap/SwapNotification.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ export default defineComponent({
6060
),
6161
);
6262
63+
function onUnload(event: BeforeUnloadEvent) {
64+
// Firefox respects the event cancellation to prompt the user
65+
event.preventDefault();
66+
// Chrome requires returnValue to be set
67+
event.returnValue = '';
68+
}
69+
6370
onMounted(() => {
6471
if (activeSwap.value) processSwap();
6572
});
@@ -126,6 +133,8 @@ export default defineComponent({
126133
...activeSwap.value!,
127134
state: SwapState.EXPIRED,
128135
});
136+
} else {
137+
window.addEventListener('beforeunload', onUnload);
129138
}
130139
131140
switch (activeSwap.value!.state) {
@@ -156,13 +165,15 @@ export default defineComponent({
156165
return;
157166
}
158167
case SwapState.COMPLETE:
168+
window.removeEventListener('beforeunload', onUnload);
159169
setTimeout(() => {
160170
// Hide notification after a timeout, if not in the SwapModal.
161171
if (context.root.$route.name === 'swap') return;
162172
setActiveSwap(null);
163173
}, 4 * 1000); // 4 seconds
164174
/* eslint-enable no-fallthrough */
165175
default:
176+
window.removeEventListener('beforeunload', onUnload);
166177
break;
167178
}
168179
}

0 commit comments

Comments
 (0)