Allow main menu modals to be closed by clicking escape (#1617)

## Description:



Allow the main menu modals to be closed by clicking the Escape key. The
manner by which this change achieves this is by adding a
connectedCallback to add a keydown EventListener, which closes the modal
on clicking Escape.

Relevant issue: #1586 

My earlier PR was only for the in-game modals, as they can access the
Event Bus and receive the CloseViewEvent.
https://github.com/openfrontio/OpenFrontIO/pull/1604

As mentioned, this PR differs in that it does not use the Event Bus
because these are not in-game modals. The main menu modals do not have
access to the event bus.

Affected modals for this PR.
- UserSettingModal.ts
- TerritoryPatternsModal.ts
- SingePlayerModal.ts
- NewsModal.ts
- LanguageModal.ts
- JoinPrivateLobbyModal.ts
- HostLobbyModal.ts
- HelpModal.ts
- FlatInput.ts








## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I have read and accepted the CLA agreement (only required once).

## Please put your Discord username so you can be contacted if a bug or
regression is found:

slyty

---------

Co-authored-by: Antoine <antoine.gannat@gmail.com>
This commit is contained in:
Tyler Hanavan
2025-08-02 04:36:03 -04:00
committed by GitHub
parent bcab4e600e
commit 15d0fd5b64
12 changed files with 137 additions and 6 deletions
+13
View File
@@ -68,8 +68,21 @@ export class FlagInput extends LitElement {
super.connectedCallback();
this.flag = this.getStoredFlag();
this.dispatchFlagEvent();
window.addEventListener("keydown", this.handleKeyDown);
}
disconnectedCallback() {
window.removeEventListener("keydown", this.handleKeyDown);
super.disconnectedCallback();
}
private handleKeyDown = (e: KeyboardEvent) => {
if (e.code === "Escape") {
e.preventDefault();
this.showModal = false;
}
};
createRenderRoot() {
return this;
}
+17
View File
@@ -15,6 +15,23 @@ export class HelpModal extends LitElement {
return this;
}
connectedCallback() {
super.connectedCallback();
window.addEventListener("keydown", this.handleKeyDown);
}
disconnectedCallback() {
window.removeEventListener("keydown", this.handleKeyDown);
super.disconnectedCallback();
}
private handleKeyDown = (e: KeyboardEvent) => {
if (e.code === "Escape") {
e.preventDefault();
this.close();
}
};
render() {
return html`
<o-modal
+17
View File
@@ -56,6 +56,23 @@ export class HostLobbyModal extends LitElement {
private botsUpdateTimer: number | null = null;
private userSettings: UserSettings = new UserSettings();
connectedCallback() {
super.connectedCallback();
window.addEventListener("keydown", this.handleKeyDown);
}
disconnectedCallback() {
window.removeEventListener("keydown", this.handleKeyDown);
super.disconnectedCallback();
}
private handleKeyDown = (e: KeyboardEvent) => {
if (e.code === "Escape") {
e.preventDefault();
this.close();
}
};
render() {
return html`
<o-modal title=${translateText("host_modal.title")}>
+17
View File
@@ -20,6 +20,23 @@ export class JoinPrivateLobbyModal extends LitElement {
private playersInterval: NodeJS.Timeout | null = null;
connectedCallback() {
super.connectedCallback();
window.addEventListener("keydown", this.handleKeyDown);
}
disconnectedCallback() {
window.removeEventListener("keydown", this.handleKeyDown);
super.disconnectedCallback();
}
private handleKeyDown = (e: KeyboardEvent) => {
if (e.code === "Escape") {
e.preventDefault();
this.close();
}
};
render() {
return html`
<o-modal title=${translateText("private_lobby.title")}>
+13
View File
@@ -31,11 +31,24 @@ export class LanguageModal extends LitElement {
}
}
connectedCallback() {
super.connectedCallback();
window.addEventListener("keydown", this.handleKeyDown);
}
disconnectedCallback() {
super.disconnectedCallback();
window.removeEventListener("keydown", this.handleKeyDown);
document.body.style.overflow = "auto";
}
private handleKeyDown = (e: KeyboardEvent) => {
if (e.code === "Escape") {
e.preventDefault();
this.close();
}
};
private selectLanguage = (lang: string) => {
this.dispatchEvent(
new CustomEvent("language-selected", {
+17
View File
@@ -13,6 +13,23 @@ export class NewsModal extends LitElement {
close: () => void;
};
connectedCallback() {
super.connectedCallback();
window.addEventListener("keydown", this.handleKeyDown);
}
disconnectedCallback() {
window.removeEventListener("keydown", this.handleKeyDown);
super.disconnectedCallback();
}
private handleKeyDown = (e: KeyboardEvent) => {
if (e.code === "Escape") {
e.preventDefault();
this.close();
}
};
@property({ type: String }) markdown = "Loading...";
private initialized: boolean = false;
+17
View File
@@ -47,6 +47,23 @@ export class SinglePlayerModal extends LitElement {
private userSettings: UserSettings = new UserSettings();
connectedCallback() {
super.connectedCallback();
window.addEventListener("keydown", this.handleKeyDown);
}
disconnectedCallback() {
window.removeEventListener("keydown", this.handleKeyDown);
super.disconnectedCallback();
}
private handleKeyDown = (e: KeyboardEvent) => {
if (e.code === "Escape") {
e.preventDefault();
this.close();
}
};
render() {
return html`
<o-modal title=${translateText("single_modal.title")}>
+7
View File
@@ -45,6 +45,7 @@ export class TerritoryPatternsModal extends LitElement {
connectedCallback() {
super.connectedCallback();
window.addEventListener("keydown", this.handleKeyDown);
this.selectedPattern = this.userSettings.getSelectedPattern();
this.updateComplete.then(() => {
const containers = this.renderRoot.querySelectorAll(".preview-container");
@@ -59,6 +60,7 @@ export class TerritoryPatternsModal extends LitElement {
}
disconnectedCallback() {
window.removeEventListener("keydown", this.handleKeyDown);
super.disconnectedCallback();
}
@@ -69,6 +71,11 @@ export class TerritoryPatternsModal extends LitElement {
}
private handleKeyDown = (e: KeyboardEvent) => {
if (e.code === "Escape") {
e.preventDefault();
this.close();
}
const key = e.key.toLowerCase();
const nextSequence = [...this.keySequence, key].slice(-5);
this.keySequence = nextSequence;
+5
View File
@@ -51,6 +51,11 @@ export class UserSettingModal extends LitElement {
private handleKeyDown = (e: KeyboardEvent) => {
if (!this.modalEl?.isModalOpen || this.showEasterEggSettings) return;
if (e.code === "Escape") {
e.preventDefault();
this.close();
}
const key = e.key.toLowerCase();
const nextSequence = [...this.keySequence, key].slice(-4);
this.keySequence = nextSequence;
+2 -3
View File
@@ -173,7 +173,7 @@ export function createRenderer(
console.error("player panel not found");
}
playerPanel.g = game;
playerPanel.eventBus = eventBus;
playerPanel.initEventBus(eventBus);
playerPanel.emojiTable = emojiTable;
playerPanel.uiState = uiState;
@@ -182,8 +182,7 @@ export function createRenderer(
console.error("chat modal not found");
}
chatModal.g = game;
chatModal.eventBus = eventBus;
chatModal.initEventBus();
chatModal.initEventBus(eventBus);
const multiTabModal = document.querySelector(
"multi-tab-modal",
+3 -2
View File
@@ -173,8 +173,9 @@ export class ChatModal extends LitElement {
`;
}
initEventBus() {
this.eventBus.on(CloseViewEvent, (e) => {
initEventBus(eventBus: EventBus) {
this.eventBus = eventBus;
eventBus.on(CloseViewEvent, (e) => {
if (!this.hidden) {
this.close();
}
+9 -1
View File
@@ -164,9 +164,17 @@ export class PlayerPanel extends LitElement implements Layer {
private ctModal: ChatModal;
initEventBus(eventBus: EventBus) {
this.eventBus = eventBus;
eventBus.on(CloseViewEvent, (e) => {
if (!this.hidden) {
this.hide();
}
});
}
init() {
this.eventBus.on(MouseUpEvent, () => this.hide());
this.eventBus.on(CloseViewEvent, (e) => {
this.hide();
});