Files
OpenFrontIO/src/client/Navigation.ts
Ryan 70f2abb181 Homepage update & add 3 public lobbies (#3191)
## Description:

Update UI 
check https://homepageupdate.openfront.dev/ 

Improved mobile UI (now fills whole screen for all modals) e.g.:
<img width="432" height="852" alt="image"
src="https://github.com/user-attachments/assets/56de40af-4137-4c57-96b7-3910c9a665b8"
/>

Converted PublicLobby to be "GameModeSelector" to get a nicer 4x4 grid
div, where <GameModeSelector> now handles all the username validation
now (removed redundant code from modals such as matchmaking) also fixed
a bug where someone could have "[XX] X" as thier username (when the
minimum should be 3 chars for their name)

Now visually displays the 3 lobbies ffa/team/special (which is a
continuation from the work done in: #3196 )
<img width="818" height="563" alt="image"
src="https://github.com/user-attachments/assets/a15cd31b-6061-4fb8-83ee-ffde6225cfa7"
/>

updated the background:
<img width="1919" height="807" alt="image"
src="https://github.com/user-attachments/assets/358a7434-51b8-4540-baf2-d1be05053c44"
/>



slightly updated the glassy-look to be less glassy:
<img width="825" height="729" alt="image"
src="https://github.com/user-attachments/assets/1801871b-bbf8-43db-ac53-489337ae80a5"
/>



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

w.o.n
2026-02-18 23:11:01 -06:00

129 lines
4.5 KiB
TypeScript

export function initNavigation() {
const closeMobileSidebar = () => {
const sidebar = document.getElementById("sidebar-menu");
const backdrop = document.getElementById("mobile-menu-backdrop");
if (sidebar?.classList.contains("open")) {
sidebar.classList.remove("open");
backdrop?.classList.remove("open");
document.documentElement.classList.remove("overflow-hidden");
sidebar.setAttribute("aria-hidden", "true");
backdrop?.setAttribute("aria-hidden", "true");
const hb = document.getElementById("hamburger-btn");
if (hb) hb.setAttribute("aria-expanded", "false");
}
};
const showPage = (pageId: string) => {
window.currentPageId = pageId;
// Close mobile sidebar if a nav item was clicked
closeMobileSidebar();
// Hide only the currently visible modal
const visibleModal = document.querySelector(".page-content:not(.hidden)");
if (visibleModal) {
visibleModal.classList.add("hidden");
visibleModal.classList.remove("block");
}
// Handle page-play separately (it's not a page-content element)
const pagePlayEl = document.getElementById("page-play");
if (pageId === "page-play") {
pagePlayEl?.classList.remove("hidden");
} else {
pagePlayEl?.classList.add("hidden");
}
// Show the target page if it's a modal
if (pageId !== "page-play") {
const target = document.getElementById(pageId);
if (target) {
target.classList.remove("hidden");
// Modals need block display explicitly
if (target.classList.contains("page-content")) {
target.classList.add("block");
}
// If the target itself is a modal component with inline attribute, open it
if (
target.hasAttribute("inline") &&
typeof (target as any).open === "function"
) {
(target as any).open();
}
}
}
// Update active state on menu items
document.querySelectorAll(".nav-menu-item").forEach((item) => {
if ((item as HTMLElement).dataset.page === pageId) {
item.classList.add("active");
} else {
item.classList.remove("active");
}
});
// Dispatch CustomEvent to notify listeners of page change
window.dispatchEvent(new CustomEvent("showPage", { detail: pageId }));
};
window.showPage = showPage;
// Use event delegation for navigation items (they may be inside Lit components)
document.addEventListener("click", (e) => {
const target = (e.target as HTMLElement).closest(
".nav-menu-item[data-page]",
);
if (target) {
const pageId = (target as HTMLElement).dataset.page;
if (pageId) showPage(pageId);
}
});
// Wait for main-layout component to render before setting up click handler
customElements.whenDefined("main-layout").then(() => {
// Handle clicks on main container to close open modals (navigate back)
const mainEl = document.querySelector("main");
if (mainEl) {
mainEl.addEventListener("click", (e: Event) => {
const target = e.target as HTMLElement;
const isPlayPageHidden = document
.getElementById("page-play")
?.classList.contains("hidden");
// Only proceed if we are NOT on the play page (meaning a modal page is open)
if (isPlayPageHidden) {
// Close modal if clicking on main element itself, or directly on a page-content element
const isOnMain = target === mainEl;
const isOnPageContent = target.classList.contains("page-content");
if (isOnMain || isOnPageContent) {
// Find the open modal and call its close() method instead of showPage directly
// This ensures proper cleanup (like websocket disconnection)
const openModal = document.querySelector(
".page-content:not(.hidden)",
) as any;
if (openModal && typeof openModal.close === "function") {
// Call leaveLobby or closeAndLeave first if it exists (for lobby modals)
if (typeof openModal.leaveLobby === "function") {
openModal.leaveLobby();
} else if (typeof openModal.closeAndLeave === "function") {
openModal.closeAndLeave();
return; // closeAndLeave already calls close()
}
openModal.close();
} else {
showPage("page-play");
}
}
}
});
}
});
// Ensure Play is the default visible/active page on load.
showPage("page-play");
}