import { t } from "i18next"; import { LitElement, css, html } from "lit"; import { customElement, property } from "lit/decorators.js"; import { Ref, createRef, ref } from "lit/directives/ref.js"; import { T } from "../translations/Languages"; @customElement( "thermal-dialog" ) export class ThermalDialog extends LitElement { static shadowRootOptions: ShadowRootInit = { ...LitElement.shadowRootOptions, mode: "open" } @property( { type: String, reflect: false } ) public button: string = t(T.close); protected dialogRef: Ref = createRef(); protected closeButtonRef: Ref = createRef(); protected invokerRef: Ref = createRef(); @property( {type: String, reflect: true} ) public label?: string; @property( { type: Object, reflect: true } ) public beforeClose?: () => Promise; constructor() { super(); } setClose() { this.dialogRef.value?.close(); window.document.body.style.removeProperty("overflow-y"); window.document.body.style.removeProperty( "height" ); } setOpen() { this.dialogRef.value?.showModal(); window.document.body.style.overflowY = "hidden"; window.document.body.style.height = "100vh"; } attributeChangedCallback(name: string, _old: string | null, value: string | null): void { super.attributeChangedCallback( name, _old, value ); if ( name === "open") { const val = value === "true"; if ( val ) { this.setOpen(); } else { this.setClose(); } } } connectedCallback(): void { super.connectedCallback(); } static styles = css` .dialog { background: var( --thermal-slate-light ); color: var( --thermal-foreground ); border-radius: var( --thermal-radius ); border-color: var( --thermal-slate ); border-width: 1px; padding: calc( var( --thermal-gap ) * 1.5 ); font-size: var( --thermal-fs-small ); &::backdrop { backdrop-filter: blur(3px); } min-width: 150px; @media ( min-width: 300px ) { min-width: 250px; } @media ( min-width: 600px ) { min-width: 450px; max-width: 700px; } } .dialog-header { display: flex; flex-wrap: nowrap; justify-content: space-between; } .dialog-title { margin: 0; padding: 0; } .dialog-content { padding: var( --thermal-gap ) 0; white-space: normal; } .dialog-footer { width: 100%; display: flex; justify-content: flex-end; gap: 10px; } .dialog-close { margin: 0; padding: 0; border: 0; background: transparent; color: var( --thermal-foreground ); cursor: pointer; width: calc( var( --thermal-gap ) * 1.5); &:hover { color: var( --thermal-primary ); } } `; render() { return html`

${this.label}

` } public async closeFromTheOutside() { if ( this.beforeClose ) { const result = await this.beforeClose(); if ( result ) { this.setClose(); } } else { this.setClose(); } } }