Files
dees-catalog/ts_web/elements/dees-modal.ts

404 lines
12 KiB
TypeScript
Raw Normal View History

2024-01-18 02:08:19 +01:00
import * as colors from './00colors.js';
2024-01-15 19:42:15 +01:00
import * as plugins from './00plugins.js';
2025-06-26 18:37:49 +00:00
import { zIndexLayers, zIndexRegistry } from './00zindex.js';
2024-01-18 02:08:19 +01:00
2023-09-13 01:37:02 +02:00
import { demoFunc } from './dees-modal.demo.js';
import {
customElement,
html,
DeesElement,
property,
type TemplateResult,
cssManager,
css,
type CSSResult,
unsafeCSS,
unsafeHTML,
state,
} from '@design.estate/dees-element';
import * as domtools from '@design.estate/dees-domtools';
import { DeesWindowLayer } from './dees-windowlayer.js';
2025-06-26 15:57:27 +00:00
import './dees-icon.js';
2023-09-13 01:37:02 +02:00
declare global {
interface HTMLElementTagNameMap {
'dees-modal': DeesModal;
}
}
@customElement('dees-modal')
export class DeesModal extends DeesElement {
// STATIC
public static demo = demoFunc;
2023-09-13 13:41:56 +02:00
public static async createAndShow(optionsArg: {
heading: string;
content: TemplateResult;
menuOptions: plugins.tsclass.website.IMenuItem<DeesModal>[];
2025-06-26 15:51:05 +00:00
width?: 'small' | 'medium' | 'large' | 'fullscreen' | number;
maxWidth?: number;
minWidth?: number;
2025-06-26 15:57:27 +00:00
showCloseButton?: boolean;
showHelpButton?: boolean;
onHelp?: () => void | Promise<void>;
mobileFullscreen?: boolean;
2023-09-13 13:41:56 +02:00
}) {
2023-09-13 01:37:02 +02:00
const body = document.body;
const modal = new DeesModal();
modal.heading = optionsArg.heading;
modal.content = optionsArg.content;
2023-09-13 13:41:56 +02:00
modal.menuOptions = optionsArg.menuOptions;
2025-06-26 15:51:05 +00:00
if (optionsArg.width) modal.width = optionsArg.width;
if (optionsArg.maxWidth) modal.maxWidth = optionsArg.maxWidth;
if (optionsArg.minWidth) modal.minWidth = optionsArg.minWidth;
2025-06-26 15:57:27 +00:00
if (optionsArg.showCloseButton !== undefined) modal.showCloseButton = optionsArg.showCloseButton;
if (optionsArg.showHelpButton !== undefined) modal.showHelpButton = optionsArg.showHelpButton;
if (optionsArg.onHelp) modal.onHelp = optionsArg.onHelp;
if (optionsArg.mobileFullscreen !== undefined) modal.mobileFullscreen = optionsArg.mobileFullscreen;
2023-09-13 16:46:00 +02:00
modal.windowLayer = await DeesWindowLayer.createAndShow({
blur: true,
});
2023-09-13 01:37:02 +02:00
modal.windowLayer.addEventListener('click', async () => {
await modal.destroy();
});
body.append(modal.windowLayer);
body.append(modal);
2025-06-26 18:37:49 +00:00
// Get z-index for modal (should be above window layer)
modal.modalZIndex = zIndexRegistry.getNextZIndex();
zIndexRegistry.register(modal, modal.modalZIndex);
2025-06-26 15:51:05 +00:00
return modal;
2023-09-13 01:37:02 +02:00
}
// INSTANCE
@property({
type: String,
})
public heading = '';
@state({})
public content: TemplateResult;
2023-09-13 13:41:56 +02:00
@state({})
public menuOptions: plugins.tsclass.website.IMenuItem<DeesModal>[] = [];
2025-06-26 15:51:05 +00:00
@property({ type: String })
public width: 'small' | 'medium' | 'large' | 'fullscreen' | number = 'medium';
@property({ type: Number })
public maxWidth: number;
@property({ type: Number })
public minWidth: number;
2025-06-26 15:57:27 +00:00
@property({ type: Boolean })
public showCloseButton: boolean = true;
@property({ type: Boolean })
public showHelpButton: boolean = false;
@property({ attribute: false })
public onHelp: () => void | Promise<void>;
2025-06-26 18:37:49 +00:00
@property({ type: Boolean })
public mobileFullscreen: boolean = false;
2025-06-26 18:37:49 +00:00
@state()
private modalZIndex: number = 1000;
2025-06-26 15:57:27 +00:00
2023-09-13 01:37:02 +02:00
constructor() {
super();
}
public static styles = [
cssManager.defaultStyles,
css`
:host {
font-family: 'Geist Sans', sans-serif;
2023-09-13 01:37:02 +02:00
color: ${cssManager.bdTheme('#333', '#fff')};
2023-11-29 17:20:32 +01:00
will-change: transform;
2023-09-13 01:37:02 +02:00
}
.modalContainer {
display: flex;
position: fixed;
2023-09-13 16:25:54 +02:00
top: 0px;
left: 0px;
2023-09-13 01:37:02 +02:00
width: 100vw;
height: 100vh;
box-sizing: border-box;
align-items: center;
justify-content: center;
}
.modal {
will-change: transform;
2023-09-13 19:15:53 +02:00
transform: translateY(0px) scale(0.95);
2023-09-13 01:37:02 +02:00
opacity: 0;
min-height: 120px;
max-height: calc(100vh - 40px);
2025-06-24 08:23:24 +00:00
background: ${cssManager.bdTheme('#ffffff', '#111')};
2023-09-13 01:37:02 +02:00
border-radius: 8px;
2025-06-24 08:23:24 +00:00
border: 1px solid ${cssManager.bdTheme('#e0e0e0', '#333')};
2023-09-13 01:37:02 +02:00
transition: all 0.2s;
overflow: hidden;
2025-06-24 08:23:24 +00:00
box-shadow: ${cssManager.bdTheme('0px 2px 10px rgba(0, 0, 0, 0.1)', '0px 2px 5px rgba(0, 0, 0, 0.5)')};
2025-06-26 15:51:05 +00:00
margin: 20px;
display: flex;
flex-direction: column;
2025-06-26 15:51:05 +00:00
}
/* Width variations */
.modal.width-small {
width: 380px;
}
.modal.width-medium {
width: 560px;
}
.modal.width-large {
width: 800px;
}
.modal.width-fullscreen {
width: calc(100vw - 40px);
height: calc(100vh - 40px);
max-height: calc(100vh - 40px);
}
@media (max-width: 768px) {
.modal {
width: calc(100vw - 40px) !important;
max-width: none !important;
}
/* Allow full height on mobile when content needs it */
.modalContainer {
padding: 10px;
}
.modal {
margin: 10px;
max-height: calc(100vh - 20px);
}
/* Full screen mode on mobile */
.modal.mobile-fullscreen {
width: 100vw !important;
height: 100vh !important;
max-height: 100vh !important;
margin: 0;
border-radius: 0;
}
2023-09-13 01:37:02 +02:00
}
.modal.show {
opacity: 1;
2023-09-13 19:15:53 +02:00
transform: translateY(0px) scale(1);
}
.modal.show.predestroy {
opacity: 0;
transform: translateY(10px) scale(1);
2023-09-13 01:37:02 +02:00
}
.modal .heading {
2025-06-26 15:57:27 +00:00
height: 40px;
min-height: 40px;
font-family: 'Geist Sans', sans-serif;
2025-06-26 15:57:27 +00:00
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 12px;
border-bottom: 1px solid ${cssManager.bdTheme('#e0e0e0', '#333')};
position: relative;
flex-shrink: 0;
2025-06-26 15:57:27 +00:00
}
.modal .heading .header-buttons {
display: flex;
align-items: center;
gap: 4px;
position: absolute;
right: 8px;
top: 50%;
transform: translateY(-50%);
}
.modal .heading .header-button {
width: 28px;
height: 28px;
border-radius: 6px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.2s;
background: transparent;
color: ${cssManager.bdTheme('#666', '#999')};
}
.modal .heading .header-button:hover {
background: ${cssManager.bdTheme('rgba(0, 0, 0, 0.08)', 'rgba(255, 255, 255, 0.08)')};
color: ${cssManager.bdTheme('#333', '#fff')};
}
.modal .heading .header-button:active {
background: ${cssManager.bdTheme('rgba(0, 0, 0, 0.12)', 'rgba(255, 255, 255, 0.12)')};
}
.modal .heading .header-button dees-icon {
width: 16px;
height: 16px;
display: block;
}
.modal .heading .heading-text {
flex: 1;
2023-09-13 01:37:02 +02:00
text-align: center;
font-weight: 600;
2025-06-26 15:57:27 +00:00
font-size: 14px;
line-height: 40px;
padding: 0 40px;
2023-09-13 01:37:02 +02:00
}
.modal .content {
padding: 16px;
flex: 1;
overflow-y: auto;
overflow-x: hidden;
2023-09-13 01:37:02 +02:00
}
.modal .bottomButtons {
2024-01-18 02:08:19 +01:00
display: flex;
flex-direction: row;
2025-06-24 08:23:24 +00:00
border-top: 1px solid ${cssManager.bdTheme('#e0e0e0', '#333')};
2024-01-18 02:08:19 +01:00
justify-content: flex-end;
2025-06-26 15:51:05 +00:00
gap: 8px;
padding: 8px;
flex-shrink: 0;
2023-09-13 01:37:02 +02:00
}
.modal .bottomButtons .bottomButton {
2025-06-26 15:51:05 +00:00
padding: 8px 16px;
border-radius: 6px;
2024-01-18 02:08:19 +01:00
line-height: 16px;
2023-09-13 01:37:02 +02:00
text-align: center;
font-size: 14px;
2025-06-26 15:51:05 +00:00
font-weight: 500;
2025-06-24 08:23:24 +00:00
cursor: pointer;
2024-01-18 02:08:19 +01:00
user-select: none;
2025-06-24 08:23:24 +00:00
transition: all 0.2s;
background: ${cssManager.bdTheme('rgba(0, 0, 0, 0.05)', 'rgba(255, 255, 255, 0.05)')};
2025-06-26 15:51:05 +00:00
white-space: nowrap;
2024-01-18 02:08:19 +01:00
}
2023-09-13 01:37:02 +02:00
.modal .bottomButtons .bottomButton:hover {
2024-01-18 02:08:19 +01:00
background: ${cssManager.bdTheme(colors.bright.blue, colors.dark.blue)};
2025-06-24 08:23:24 +00:00
color: #ffffff;
2024-01-18 02:08:19 +01:00
}
.modal .bottomButtons .bottomButton:active {
background: ${cssManager.bdTheme(colors.bright.blueActive, colors.dark.blueActive)};
2025-06-24 08:23:24 +00:00
color: #ffffff;
2023-09-13 01:37:02 +02:00
}
.modal .bottomButtons .bottomButton:last-child {
border-right: none;
}
2025-06-24 08:23:24 +00:00
.modal .bottomButtons .bottomButton.primary {
background: ${cssManager.bdTheme(colors.bright.blue, colors.dark.blue)};
color: #ffffff;
}
.modal .bottomButtons .bottomButton.primary:hover {
background: ${cssManager.bdTheme(colors.bright.blueActive, colors.dark.blueActive)};
}
.modal .bottomButtons .bottomButton.primary:active {
background: ${cssManager.bdTheme(colors.bright.blueMuted, colors.dark.blueMuted)};
}
2023-09-13 01:37:02 +02:00
`,
];
public render(): TemplateResult {
2025-06-26 15:51:05 +00:00
const widthClass = typeof this.width === 'string' ? `width-${this.width}` : '';
const customWidth = typeof this.width === 'number' ? `${this.width}px` : '';
const maxWidthStyle = this.maxWidth ? `${this.maxWidth}px` : '';
const minWidthStyle = this.minWidth ? `${this.minWidth}px` : '';
const mobileFullscreenClass = this.mobileFullscreen ? 'mobile-fullscreen' : '';
2025-06-26 15:51:05 +00:00
2023-09-13 01:37:02 +02:00
return html`
2023-09-13 13:41:56 +02:00
<style>
2025-06-26 15:51:05 +00:00
${customWidth ? `.modal { width: ${customWidth}; }` : ''}
${maxWidthStyle ? `.modal { max-width: ${maxWidthStyle}; }` : ''}
${minWidthStyle ? `.modal { min-width: ${minWidthStyle}; }` : ''}
2023-09-13 13:41:56 +02:00
</style>
2025-06-26 18:37:49 +00:00
<div class="modalContainer" @click=${this.handleOutsideClick} style="z-index: ${this.modalZIndex}">
<div class="modal ${widthClass} ${mobileFullscreenClass}">
2025-06-26 15:57:27 +00:00
<div class="heading">
<div class="heading-text">${this.heading}</div>
<div class="header-buttons">
${this.showHelpButton ? html`
<div class="header-button" @click=${this.handleHelp} title="Help">
<dees-icon .icon=${'lucide:helpCircle'}></dees-icon>
</div>
` : ''}
${this.showCloseButton ? html`
<div class="header-button" @click=${() => this.destroy()} title="Close">
<dees-icon .icon=${'lucide:x'}></dees-icon>
</div>
` : ''}
</div>
</div>
2023-09-13 01:37:02 +02:00
<div class="content">${this.content}</div>
2025-06-26 15:51:05 +00:00
${this.menuOptions.length > 0 ? html`
<div class="bottomButtons">
${this.menuOptions.map(
(actionArg, index) => html`
<div class="bottomButton ${index === this.menuOptions.length - 1 ? 'primary' : ''} ${actionArg.name === 'OK' ? 'ok' : ''}" @click=${() => {
actionArg.action(this);
}}>${actionArg.name}</div>
`
)}
</div>
` : ''}
2023-09-13 01:37:02 +02:00
</div>
</div>
`;
}
private windowLayer: DeesWindowLayer;
public async firstUpdated(_changedProperties: Map<string | number | symbol, unknown>) {
super.firstUpdated(_changedProperties);
const domtools = await this.domtoolsPromise;
await domtools.convenience.smartdelay.delayFor(30);
const modal = this.shadowRoot.querySelector('.modal');
modal.classList.add('show');
}
public async handleOutsideClick(eventArg: MouseEvent) {
eventArg.stopPropagation();
const modalContainer = this.shadowRoot.querySelector('.modalContainer');
if (eventArg.target === modalContainer) {
await this.destroy();
}
}
public async destroy() {
const domtools = await this.domtoolsPromise;
const modal = this.shadowRoot.querySelector('.modal');
2023-09-13 19:15:53 +02:00
modal.classList.add('predestroy');
2023-09-13 01:37:02 +02:00
await domtools.convenience.smartdelay.delayFor(200);
document.body.removeChild(this);
await this.windowLayer.destroy();
2025-06-26 18:37:49 +00:00
// Unregister from z-index registry
zIndexRegistry.unregister(this);
2023-09-13 01:37:02 +02:00
}
2025-06-26 15:57:27 +00:00
private async handleHelp() {
if (this.onHelp) {
await this.onHelp();
}
}
2023-09-13 01:37:02 +02:00
}