import { LitElement, PropertyValues, html } from "lit"; import { customElement, property, query, state } from "lit/decorators.js"; import { prefersReducedMotion } from "../../utilities/ReducedMotion"; import { translateText } from "../../Utils"; import { documentStylesSheet } from "./SharedStyles"; export type OModalTab = { key: string; label: string }; @customElement("o-modal") export class OModal extends LitElement { static styles = [documentStylesSheet()]; @state() public isModalOpen = false; @query("[data-modal-scroll]") private scrollContainer?: HTMLElement; static openCount = 0; @property({ type: Boolean }) public inline = false; @property({ type: Boolean }) public alwaysMaximized = false; @property({ type: Boolean }) public hideCloseButton = false; @property({ type: String }) public title = ""; @property({ type: Boolean }) public hideHeader = false; @property({ type: String }) public maxWidth = ""; /** A left-aligned panel with an unobscured preview of the game behind it. */ @property({ type: Boolean }) public sidePanel = false; /** A short notice: a card as tall as its content, even on small screens. */ @property({ type: Boolean }) public fitContent = false; @property({ type: Array }) public tabs: OModalTab[] = []; @property({ type: String }) public activeTab = ""; @property({ attribute: false }) public onTabChange?: (key: string) => void; @property({ type: Boolean }) public showScrollToTop = false; @state() private showScrollTopButton = false; private scrollingToTop = false; private lastScrollTop = 0; public onClose?: () => void; public resetScrollToTop(): void { this.scrollingToTop = false; this.showScrollTopButton = false; this.lastScrollTop = 0; } public syncScrollToTop(): void { const scrollTop = this.scrollContainer?.scrollTop ?? 0; this.scrollingToTop = false; this.lastScrollTop = scrollTop; this.showScrollTopButton = this.showScrollToTop && scrollTop > 200; } public open() { if (!this.isModalOpen) { if (!this.inline) { OModal.openCount = OModal.openCount + 1; if (OModal.openCount === 1) document.body.style.overflow = "hidden"; } this.isModalOpen = true; } } public close() { if (this.isModalOpen) { this.isModalOpen = false; this.resetScrollToTop(); this.onClose?.(); if (!this.inline) { OModal.openCount = Math.max(0, OModal.openCount - 1); if (OModal.openCount === 0) document.body.style.overflow = ""; } } } protected updated(changed: PropertyValues): void { super.updated(changed); // A tab opened directly (a link, the last tab used) may sit past the edge // of a scrolled tab row: scroll the row, and only the row, to it. // scrollIntoView would also scroll the modal's content and the page. if (!changed.has("activeTab") || !this.activeTab) return; const row = this.renderRoot.querySelector('[role="tablist"]'); if (row === null || row.scrollWidth <= row.clientWidth) return; const tab = [...row.querySelectorAll('[role="tab"]')].find( (t) => t.dataset.key === this.activeTab, ); if (tab === undefined) return; const rowBox = row.getBoundingClientRect(); const tabBox = tab.getBoundingClientRect(); if (tabBox.left < rowBox.left) { row.scrollLeft += tabBox.left - rowBox.left; } else if (tabBox.right > rowBox.right) { row.scrollLeft += tabBox.right - rowBox.right; } } public getScrollTop(): number { return this.scrollContainer?.scrollTop ?? 0; } public setScrollTop(scrollTop: number): void { if (this.scrollContainer) { this.scrollContainer.scrollTop = scrollTop; } } private handleScroll(e: Event) { if (!this.showScrollToTop) return; const target = e.currentTarget as HTMLElement; const { scrollTop } = target; if (this.scrollingToTop) { if (scrollTop > this.lastScrollTop || scrollTop <= 200) { this.scrollingToTop = false; } else { this.lastScrollTop = scrollTop; return; } } this.lastScrollTop = scrollTop; this.showScrollTopButton = scrollTop > 200; } private handleScrollEnd(e: Event) { if (!this.showScrollToTop) return; this.scrollingToTop = false; this.showScrollTopButton = (e.currentTarget as HTMLElement).scrollTop > 200; } private scrollToTop() { this.scrollingToTop = true; this.showScrollTopButton = false; this.lastScrollTop = this.scrollContainer?.scrollTop ?? 0; // The button unmounts on this click; keep keyboard focus inside the modal. this.scrollContainer?.focus({ preventScroll: true }); this.scrollContainer?.scrollTo({ top: 0, behavior: prefersReducedMotion() ? "auto" : "smooth", }); } disconnectedCallback() { // Ensure global counter is decremented if this modal is removed while open. if (this.isModalOpen && !this.inline) { OModal.openCount = Math.max(0, OModal.openCount - 1); if (OModal.openCount === 0) document.body.style.overflow = ""; } super.disconnectedCallback(); } private handleTabClick(key: string) { this.onTabChange?.(key); } private renderTabs() { // One row that scrolls sideways when the tabs don't fit, instead of // wrapping onto a second row. The auto margins on the first and last tab // centre the row when it fits, and unlike justify-center they never push // a tab off the left edge out of reach. Below sm the tabs are compact // (smaller, less tracked, less padded labels, still 40px tall) so five // fit across a 360px phone; the scroll is left for narrower screens and // longer rows. return html`
${this.tabs.map((tab) => { const active = this.activeTab === tab.key; return html` `; })}
`; } /** Share scrolling and dismissal across pages, centered dialogs, and HUD panels. */ render() { const shouldRender = this.isModalOpen || this.inline; if (!shouldRender) { return html``; } const backdropClass = this.inline ? "relative z-10 w-full h-full flex items-stretch bg-transparent" : this.sidePanel ? "fixed inset-0 z-[9999] flex items-stretch justify-start overflow-hidden p-2 sm:p-4" : "fixed inset-0 z-[9999] bg-black/60 flex items-center justify-center overflow-hidden"; const wrapperClass = this.inline ? "relative flex flex-col w-full h-full m-0 max-w-full max-h-none shadow-none" : this.sidePanel ? "relative flex flex-col w-full h-full max-w-[400px] min-w-0" : this.fitContent ? "relative flex flex-col w-[calc(100%-2rem)] max-h-[calc(100%-2rem)]" : `relative flex flex-col w-full h-full lg:w-[90%] lg:h-auto lg:min-w-[400px] lg:max-w-[900px] lg:m-8 lg:rounded-lg lg:max-h-[calc(100vh-4rem)] ${ this.alwaysMaximized ? "h-auto" : "" }`; const wrapperStyle = !this.inline && this.maxWidth ? `max-width: ${this.maxWidth};` : ""; const hasTabs = this.tabs.length > 0; // An inline page on a compact screen fills it edge to edge (see the `compact` // variant in styles.css), so it drops its frame there. Plain `compact:`, // not `in-[.page-open]:` as in MainLayout: this renders in a shadow root, // which body.page-open can't reach, and an inline modal only shows while // its page is open anyway. const sectionClass = this.sidePanel ? "graphics-panel-surface relative flex-1 min-h-0 flex flex-col text-white rounded-lg border overflow-hidden" : `relative flex-1 min-h-0 flex flex-col text-white bg-black/70 backdrop-blur-xl lg:rounded-2xl lg:border border-white/10 overflow-hidden ${ this.inline ? "compact:rounded-none! compact:border-0!" : "" } ${this.fitContent ? "rounded-2xl border" : ""} ${ // On compact screens body.page-open drops its bottom padding so the // page reaches the bottom of the screen; keep the content above the // home indicator. this.inline ? "compact:pb-[env(safe-area-inset-bottom)]" : "" }`; return html` `; } }