Files
OpenFrontIO/src/client/FlagInputModal.ts
T
yanir 76723e6739 Minor changes to the flag-input-modal (#1975)
## Description:

Noticed things were a bit not centered and title had Modal in it.

<img width="643" height="271" alt="image"
src="https://github.com/user-attachments/assets/15e113b7-9d77-42d8-8eb0-9b55f8cdbd19"
/>
<div>↓</div>

<img width="643" height="271" alt="image"
src="https://github.com/user-attachments/assets/aaa64ede-dd1f-44e3-afde-982bd67a1367"
/>

<div>---------------------------------------------------------------------------------------------------------------------------</div>
<img width="867" height="420" alt="image"
src="https://github.com/user-attachments/assets/af545ff4-3d2b-417d-8a96-c8594ffea754"
/>
<div>↓</div>
<img width="867" height="420" alt="image"
src="https://github.com/user-attachments/assets/d3380df7-1c78-40be-8dd2-162768773af6"
/>


## 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

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

boostry
2025-09-05 18:21:53 +00:00

124 lines
3.7 KiB
TypeScript

import { LitElement, html } from "lit";
import { customElement, query, state } from "lit/decorators.js";
import Countries from "./data/countries.json";
import { translateText } from "./Utils";
@customElement("flag-input-modal")
export class FlagInputModal extends LitElement {
@query("o-modal") private modalEl!: HTMLElement & {
open: () => void;
close: () => void;
};
@state() private search = "";
@state() private isModalOpen = false;
createRenderRoot() {
return this;
}
render() {
return html`
<o-modal alwaysMaximized title=${translateText("flag_input.title")}>
<div class="flex justify-center w-full p-[1rem]">
<input
class="h-[2rem] border-none border border-gray-300
rounded-xl shadow-sm text-2xl text-center focus:outline-none
focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-black
dark:border-gray-300/60 dark:bg-gray-700 dark:text-white"
type="text"
placeholder=${translateText("flag_input.search_flag")}
@change=${this.handleSearch}
@keyup=${this.handleSearch}
/>
</div>
<div
class="flex flex-wrap justify-evenly gap-[1rem] overflow-y-auto overflow-x-hidden h-[90%]"
>
${this.isModalOpen
? Countries.filter(
(country) =>
!country.restricted && this.includedInSearch(country),
).map(
(country) => html`
<button
@click=${() => {
this.setFlag(country.code);
this.close();
}}
class="text-center cursor-pointer border-none bg-none opacity-70
w-[calc(100%/2-15px)] sm:w-[calc(100%/4-15px)]
md:w-[calc(100%/6-15px)] lg:w-[calc(100%/8-15px)]
xl:w-[calc(100%/10-15px)] min-w-[80px]"
>
<img
class="country-flag w-full h-auto"
src="/flags/${country.code}.svg"
@error=${(e: Event) => {
const img = e.currentTarget as HTMLImageElement;
const fallback = "/flags/xx.svg";
if (img.src && !img.src.endsWith(fallback)) {
img.src = fallback;
}
}}
/>
<span class="country-name">${country.name}</span>
</button>
`,
)
: html``}
</div>
</o-modal>
`;
}
private includedInSearch(country: { name: string; code: string }): boolean {
return (
country.name.toLowerCase().includes(this.search.toLowerCase()) ||
country.code.toLowerCase().includes(this.search.toLowerCase())
);
}
private handleSearch(event: Event) {
this.search = (event.target as HTMLInputElement).value;
}
private setFlag(flag: string) {
localStorage.setItem("flag", flag);
this.dispatchEvent(
new CustomEvent("flag-change", {
detail: { flag },
bubbles: true,
composed: true,
}),
);
}
public open() {
this.isModalOpen = true;
this.modalEl?.open();
}
public close() {
this.isModalOpen = false;
this.modalEl?.close();
}
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();
}
};
}