import { LitElement, html, nothing} from 'lit';
import type {TemplateResult} from 'lit';
import { state, customElement, property } from 'lit/decorators';
import { getScrollbarWidth } from 'kailib'
import { DIALOG_STYLES } from './styles';
import { SCROLLBAR_STYLES } from './scrollbar';
@customElement('lit-modal')
export class LitModal extends LitElement{
static get styles() {
return [DIALOG_STYLES, SCROLLBAR_STYLES];
};
static get properties() {
return { open: { type: Boolean, reflect: true } };
}
@state() closeBtnText: TemplateResult | string = "Close";
@property({type: Boolean}) open: boolean = false;
@property({type: Boolean}) useCancelBtn: boolean = true;
_onHideEvents: Function[] = [];
_onShowEvents: Function[] = [];
_onConfirmEvents: Function[] = [];
render(){
return html`
`
}
// **** Actions ****
private _show(){
document.body.style.paddingRight = getScrollbarWidth() + "px";
document.body.style.overflow = 'hidden';
document.addEventListener("keydown", this._onKeypress.bind(this));
this.open = true;
}
private _hide(dispatchEvent: boolean = true){
document.body.style.paddingRight = "initial";
document.body.style.overflow = 'initial';
document.removeEventListener("keydown", this._onKeypress);
if(dispatchEvent){
this._onHideEvents.forEach(f => f());
}
}
public showDialog(){
this._show();
}
public closeDialog(dispatchEvent: boolean = true){
this.open = false;
this._hide(dispatchEvent);
}
// **** Events ****
private _onClick(e: Event){
const el = e.target as HTMLElement;
if(el.closest('.confirm')){
this._onConfirmEvents.forEach(f => f());
}
else if(el.closest('.dialog-hide')){
this.closeDialog();
}
}
private _onKeypress(e: KeyboardEvent){
if(e.key === "Escape"){
this.closeDialog()
}
}
public onHide(f: Function){
this._onHideEvents.push(f);
}
public offHide(f: Function){
this._onHideEvents = this._onHideEvents.filter(ef => ef !== f);
}
public onShow(f: Function){
this._onShowEvents.push(f);
}
public offShow(f: Function){
this._onShowEvents = this._onShowEvents.filter(ef => ef !== f);
}
public onConfirm(f) {
this._onConfirmEvents.push(f);
}
public offConfirm(f) {
this._onConfirmEvents = this._onConfirmEvents.filter(ef => ef !== f);
}
}
declare global {
interface HTMLElementTagNameMap {
'lit-modal': LitModal;
}
}