diff --git a/resources/lang/en.json b/resources/lang/en.json index e80e2f6da..7d2c19908 100644 --- a/resources/lang/en.json +++ b/resources/lang/en.json @@ -430,7 +430,8 @@ "teams_Humans Vs Nations": "Humans vs Nations", "starting_gold": "Starting gold", "crowded": "Crowded modifier", - "hard_nations": "Hard Nations" + "hard_nations": "Hard Nations", + "leave_confirmation": "Are you sure you want to leave the lobby?" }, "team_colors": { "red": "Red", diff --git a/src/client/HostLobbyModal.ts b/src/client/HostLobbyModal.ts index 1851b4e56..f82ad5b8e 100644 --- a/src/client/HostLobbyModal.ts +++ b/src/client/HostLobbyModal.ts @@ -406,6 +406,10 @@ export class HostLobbyModal extends BaseModal { ); } + public confirmBeforeClose(): boolean { + return confirm(translateText("host_modal.leave_confirmation")); + } + protected onClose(): void { console.log("Closing host lobby modal"); this.stopLobbyUpdates(); diff --git a/src/client/Navigation.ts b/src/client/Navigation.ts index 4eba4e667..95f720092 100644 --- a/src/client/Navigation.ts +++ b/src/client/Navigation.ts @@ -106,6 +106,13 @@ export function initNavigation() { ) as any; if (openModal && typeof openModal.close === "function") { + // Check confirmation guard before closing + if ( + typeof openModal.confirmBeforeClose === "function" && + !openModal.confirmBeforeClose() + ) { + return; + } // Call leaveLobby or closeAndLeave first if it exists (for lobby modals) if (typeof openModal.leaveLobby === "function") { openModal.leaveLobby(); diff --git a/src/client/components/BaseModal.ts b/src/client/components/BaseModal.ts index 80e40d900..8efc8e258 100644 --- a/src/client/components/BaseModal.ts +++ b/src/client/components/BaseModal.ts @@ -39,6 +39,11 @@ export abstract class BaseModal extends LitElement { if (this.modalEl) { this.modalEl.onClose = () => { if (this.isModalOpen) { + if (!this.confirmBeforeClose()) { + // Re-open the underlying o-modal since it already closed itself + this.modalEl?.open(); + return; + } this.close(); } }; @@ -57,6 +62,9 @@ export abstract class BaseModal extends LitElement { private handleKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape" && this.isModalOpen) { e.preventDefault(); + if (!this.confirmBeforeClose()) { + return; + } this.close(); } }; @@ -93,6 +101,15 @@ export abstract class BaseModal extends LitElement { // Default implementation does nothing } + /** + * Guard called before closing via Escape key or click-outside. + * Override in subclasses to show a confirmation dialog. + * Return false to prevent the modal from closing. + */ + public confirmBeforeClose(): boolean { + return true; + } + /** * Open the modal. Handles both inline and modal element modes. * Subclasses can override onOpen() for custom behavior.