Files
dees-catalog/ts_web/elements/00group-overlay/dees-modal/dees-modal.ts

423 lines
13 KiB
TypeScript
Raw Normal View History

import * as colors from '../../00colors.js';
import * as plugins from '../../00plugins.js';
import { zIndexLayers, zIndexRegistry } from '../../00zindex.js';
import { cssGeistFontFamily } from '../../00fonts.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/dees-windowlayer.js';
import '../../00group-utility/dees-icon/dees-icon.js';
import '../../00group-layout/dees-tile/dees-tile.js';
import { themeDefaultStyles } from '../../00theme.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;
public static demoGroups = ['Overlay'];
2023-09-13 01:37:02 +02:00
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;
contentPadding?: number;
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;
if (optionsArg.contentPadding !== undefined) modal.contentPadding = optionsArg.contentPadding;
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,
})
accessor heading = '';
2023-09-13 01:37:02 +02:00
@state({})
accessor content!: TemplateResult;
2023-09-13 01:37:02 +02:00
2023-09-13 13:41:56 +02:00
@state({})
accessor menuOptions: plugins.tsclass.website.IMenuItem<DeesModal>[] = [];
2023-09-13 13:41:56 +02:00
2025-06-26 15:51:05 +00:00
@property({ type: String })
accessor width: 'small' | 'medium' | 'large' | 'fullscreen' | number = 'medium';
2025-06-26 15:51:05 +00:00
@property({ type: Number })
accessor maxWidth!: number;
2025-06-26 15:51:05 +00:00
@property({ type: Number })
accessor minWidth!: number;
2025-06-26 15:51:05 +00:00
2025-06-26 15:57:27 +00:00
@property({ type: Boolean })
accessor showCloseButton: boolean = true;
2025-06-26 15:57:27 +00:00
@property({ type: Boolean })
accessor showHelpButton: boolean = false;
2025-06-26 15:57:27 +00:00
@property({ attribute: false })
accessor onHelp!: () => void | Promise<void>;
@property({ type: Boolean })
accessor mobileFullscreen: boolean = false;
@property({ type: Number })
accessor contentPadding: number = 16;
2025-06-26 18:37:49 +00:00
@state()
accessor modalZIndex: number = 1000;
2025-06-26 15:57:27 +00:00
2023-09-13 01:37:02 +02:00
constructor() {
super();
}
public static styles = [
themeDefaultStyles,
2023-09-13 01:37:02 +02:00
cssManager.defaultStyles,
css`
/* TODO: Migrate hardcoded values to --dees-* CSS variables */
2023-09-13 01:37:02 +02:00
:host {
2025-06-27 17:32:01 +00:00
font-family: ${cssGeistFontFamily};
color: ${cssManager.bdTheme('hsl(0 0% 9%)', 'hsl(0 0% 95%)')};
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;
}
dees-tile {
will-change: transform, opacity;
transform: translateY(8px) scale(0.98);
2023-09-13 01:37:02 +02:00
opacity: 0;
min-height: 120px;
max-height: calc(100vh - 80px);
transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease;
margin: 40px;
2025-06-27 00:18:36 +00:00
overscroll-behavior: contain;
2025-06-26 15:51:05 +00:00
}
dees-tile::part(outer) {
box-shadow:
0 0 0 1px ${cssManager.bdTheme('hsl(0 0% 0% / 0.03)', 'hsl(0 0% 100% / 0.03)')},
0 8px 40px ${cssManager.bdTheme('hsl(0 0% 0% / 0.12)', 'hsl(0 0% 0% / 0.5)')},
0 2px 8px ${cssManager.bdTheme('hsl(0 0% 0% / 0.06)', 'hsl(0 0% 0% / 0.25)')};
}
2025-06-26 15:51:05 +00:00
/* Width variations */
dees-tile.width-small {
2025-06-26 15:51:05 +00:00
width: 380px;
}
dees-tile.width-medium {
2025-06-26 15:51:05 +00:00
width: 560px;
}
dees-tile.width-large {
2025-06-26 15:51:05 +00:00
width: 800px;
}
dees-tile.width-fullscreen {
2025-06-26 15:51:05 +00:00
width: calc(100vw - 40px);
height: calc(100vh - 40px);
max-height: calc(100vh - 40px);
}
@media (max-width: 768px) {
dees-tile {
2025-06-26 15:51:05 +00:00
width: calc(100vw - 40px) !important;
max-width: none !important;
}
.modalContainer {
padding: 10px;
}
dees-tile {
margin: 10px;
max-height: calc(100vh - 20px);
}
dees-tile.mobile-fullscreen {
width: 100vw !important;
height: 100vh !important;
max-height: 100vh !important;
margin: 0;
}
dees-tile.mobile-fullscreen::part(outer) {
border-radius: 0;
2025-06-27 19:48:32 +00:00
border: none;
}
2023-09-13 01:37:02 +02:00
}
dees-tile.show {
2023-09-13 01:37:02 +02:00
opacity: 1;
transform: translateY(0) scale(1);
2023-09-13 19:15:53 +02:00
}
dees-tile.show.predestroy {
2023-09-13 19:15:53 +02:00
opacity: 0;
transform: translateY(6px) scale(0.98);
transition: transform 0.15s ease-in, opacity 0.15s ease-in;
2023-09-13 01:37:02 +02:00
}
.heading {
height: 36px;
2025-06-26 15:57:27 +00:00
display: flex;
align-items: center;
padding: 0 8px 0 16px;
2025-06-26 15:57:27 +00:00
position: relative;
}
.heading .heading-text {
flex: 1;
font-weight: 500;
font-size: 13px;
letter-spacing: -0.01em;
color: ${cssManager.bdTheme('hsl(0 0% 20%)', 'hsl(0 0% 63.9%)')};
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.heading .header-buttons {
2025-06-26 15:57:27 +00:00
display: flex;
align-items: center;
gap: 2px;
flex-shrink: 0;
margin-left: 8px;
2025-06-26 15:57:27 +00:00
}
.heading .header-button {
width: 24px;
height: 24px;
2025-06-27 19:48:32 +00:00
border-radius: 4px;
2025-06-26 15:57:27 +00:00
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
2025-06-27 19:48:32 +00:00
transition: all 0.15s ease;
2025-06-26 15:57:27 +00:00
background: transparent;
color: ${cssManager.bdTheme('hsl(0 0% 55%)', 'hsl(0 0% 45%)')};
2025-06-26 15:57:27 +00:00
}
.heading .header-button:hover {
background: ${cssManager.bdTheme('hsl(0 0% 93%)', 'hsl(0 0% 12%)')};
color: ${cssManager.bdTheme('hsl(0 0% 20%)', 'hsl(0 0% 90%)')};
2025-06-26 15:57:27 +00:00
}
.heading .header-button:active {
background: ${cssManager.bdTheme('hsl(0 0% 90%)', 'hsl(0 0% 15%)')};
2025-06-26 15:57:27 +00:00
}
.heading .header-button dees-icon {
width: 14px;
height: 14px;
2025-06-26 15:57:27 +00:00
display: block;
}
.content {
overflow-y: auto;
overflow-x: hidden;
2025-06-27 00:18:36 +00:00
overscroll-behavior: contain;
scrollbar-width: thin;
scrollbar-color: ${cssManager.bdTheme('hsl(0 0% 85%)', 'hsl(0 0% 20%)')} transparent;
2023-09-13 01:37:02 +02:00
}
.bottomButtons {
2024-01-18 02:08:19 +01:00
display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: center;
gap: 0;
height: 36px;
width: 100%;
box-sizing: border-box;
2023-09-13 01:37:02 +02:00
}
.bottomButtons .bottomButton {
padding: 0 16px;
height: 100%;
2023-09-13 01:37:02 +02:00
text-align: center;
font-size: 12px;
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-27 19:48:32 +00:00
transition: all 0.15s ease;
background: transparent;
border: none;
border-left: 1px solid ${cssManager.bdTheme('hsl(0 0% 93%)', 'hsl(0 0% 11%)')};
color: ${cssManager.bdTheme('hsl(0 0% 45%)', 'hsl(0 0% 55%)')};
2025-06-26 15:51:05 +00:00
white-space: nowrap;
display: flex;
align-items: center;
}
.bottomButtons .bottomButton:first-child {
border-left: none;
2024-01-18 02:08:19 +01:00
}
.bottomButtons .bottomButton:hover {
background: ${cssManager.bdTheme('hsl(0 0% 95%)', 'hsl(0 0% 10%)')};
color: ${cssManager.bdTheme('hsl(0 0% 15%)', 'hsl(0 0% 90%)')};
2024-01-18 02:08:19 +01:00
}
.bottomButtons .bottomButton:active {
background: ${cssManager.bdTheme('hsl(0 0% 92%)', 'hsl(0 0% 13%)')};
2023-09-13 01:37:02 +02:00
}
2025-06-24 08:23:24 +00:00
.bottomButtons .bottomButton.primary {
color: ${cssManager.bdTheme('hsl(217.2 91.2% 59.8%)', 'hsl(213.1 93.9% 67.8%)')};
font-weight: 600;
2025-06-24 08:23:24 +00:00
}
.bottomButtons .bottomButton.primary:hover {
background: ${cssManager.bdTheme('hsl(217.2 91.2% 59.8% / 0.08)', 'hsl(213.1 93.9% 67.8% / 0.08)')};
color: ${cssManager.bdTheme('hsl(217.2 91.2% 50%)', 'hsl(213.1 93.9% 75%)')};
2025-06-24 08:23:24 +00:00
}
.bottomButtons .bottomButton.primary:active {
background: ${cssManager.bdTheme('hsl(217.2 91.2% 59.8% / 0.12)', 'hsl(213.1 93.9% 67.8% / 0.12)')};
2025-06-24 08:23:24 +00:00
}
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>
${customWidth ? `dees-tile { width: ${customWidth}; }` : ''}
${maxWidthStyle ? `dees-tile { max-width: ${maxWidthStyle}; }` : ''}
${minWidthStyle ? `dees-tile { 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}">
<dees-tile class="${widthClass} ${mobileFullscreenClass}">
<div slot="header" class="heading">
2025-06-26 15:57:27 +00:00
<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>
<div class="content" style="padding: ${this.contentPadding}px;">${this.content}</div>
2025-06-26 15:51:05 +00:00
${this.menuOptions.length > 0 ? html`
<div slot="footer" class="bottomButtons">
2025-06-26 15:51:05 +00:00
${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>
` : ''}
</dees-tile>
2023-09-13 01:37:02 +02:00
</div>
`;
}
private windowLayer!: DeesWindowLayer;
2023-09-13 01:37:02 +02:00
public async firstUpdated(_changedProperties: Map<string | number | symbol, unknown>) {
super.firstUpdated(_changedProperties);
const domtools = await this.domtoolsPromise;
await domtools.convenience.smartdelay.delayFor(30);
const tile = this.shadowRoot!.querySelector('dees-tile');
tile!.classList.add('show');
2023-09-13 01:37:02 +02:00
}
public async handleOutsideClick(eventArg: MouseEvent) {
eventArg.stopPropagation();
const modalContainer = this.shadowRoot!.querySelector('.modalContainer');
2023-09-13 01:37:02 +02:00
if (eventArg.target === modalContainer) {
await this.destroy();
}
}
public async destroy() {
const domtools = await this.domtoolsPromise;
const tile = this.shadowRoot!.querySelector('dees-tile');
tile!.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
}