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`