Files
OpenFrontIO/src/client/Layout.ts
T
Skigim f7598369ed refactor: consolidate platform detection across client components (#3325)
## Description:

This PR consolidates ad hoc platform/environment/viewport detection into
a single shared utility. It is scoped to this refactor only, and serves
as groundwork for the mobile-focused feature work planned for the v31
milestone.

### What changed
- Introduced a shared `Platform` utility centralising:
  - OS detection (with `userAgentData` + UA fallback)
  - Electron environment detection
- Viewport breakpoint helpers (`isMobileWidth`, `isTabletWidth`,
`isDesktopWidth`)
- Replaced duplicated inline checks across client files with the shared
API.
- Normalised Mac detection to derive from the consolidated OS logic
rather than a separate regex.

### Why
- Multiple client files each independently ran `navigator.userAgent`
regexes or copy-pasted `isElectron` logic — this unifies all of that.
- Puts a stable, tested abstraction in place before v31 mobile work
lands, so mobile feature branches have a consistent surface to build
against.

## Please complete the following:

- [x] I have added screenshots for all UI updates (N/A: refactor only,
no visible UI changes)
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file (N/A: no new user-facing strings)
- [x] I have added relevant tests to the test directory (N/A: refactor
only)
- [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:

skigim
2026-03-02 10:12:48 -08:00

87 lines
2.5 KiB
TypeScript

import { Platform } from "./Platform";
export function initLayout() {
// Wait for play-page component to render before setting up hamburger menu
customElements.whenDefined("play-page").then(() => {
const hb = document.getElementById("hamburger-btn");
const sidebar = document.getElementById("sidebar-menu");
const backdrop = document.getElementById("mobile-menu-backdrop");
// Force sidebar visibility style to ensure it's not hidden by other CSS
if (sidebar && Platform.isMobileWidth) {
sidebar.style.display = "flex";
}
if (!hb) {
console.error("Hamburger button not found");
return;
}
// Disable fallback inline handler now that JS is loaded
hb.onclick = null;
if (!sidebar) {
console.error("Sidebar menu not found");
return;
}
if (!backdrop) {
console.error("Mobile menu backdrop not found");
return;
}
const setMenuState = (open: boolean) => {
sidebar.classList.toggle("open", open);
backdrop.classList.toggle("open", open);
document.documentElement.classList.toggle("overflow-hidden", open);
hb.setAttribute("aria-expanded", open ? "true" : "false");
};
const closeMenu = () => setMenuState(false);
const openMenu = () => setMenuState(true);
const toggle = (e: Event) => {
e.stopPropagation();
// Only prevent default if it's a touchstart to avoid ghost clicks
if ((e as any).type === "touchstart") {
(e as Event).preventDefault();
}
const opening = !sidebar.classList.contains("open");
if (opening) {
openMenu();
} else {
closeMenu();
}
};
hb.addEventListener("click", toggle);
backdrop.addEventListener("click", closeMenu);
// Close menu when clicking a menu link or button (Mobile only)
sidebar.addEventListener("click", (e) => {
// On desktop, we want the menu to stay open unless explicitly toggled
if (!Platform.isMobileWidth) return;
// If the click happened on or inside an anchor/button/menu item, close the menu
const clickedElement = (e.target as Element).closest
? (e.target as Element).closest(
'a, button, [role="menuitem"], .nav-menu-item',
)
: null;
if (clickedElement) {
closeMenu();
}
});
// Close on Escape (Mobile only)
document.addEventListener("keydown", (e) => {
if (!Platform.isMobileWidth) return;
if (e.key === "Escape" && sidebar.classList.contains("open")) {
closeMenu();
}
});
});
}