Skip to content

Commit

Permalink
refactor to use init method
Browse files Browse the repository at this point in the history
  • Loading branch information
nielslyngsoe committed Nov 7, 2024
1 parent 8a3422a commit 715d11a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class UmbBackofficeModalContainerElement extends UmbLitElement {
@state()
_modals: Array<UmbModalContext> = [];

@property({ reflect: true, attribute: 'fill-background' })
@property({ type: Boolean, reflect: true, attribute: 'fill-background' })
fillBackground = false;

private _modalManager?: UmbModalManagerContext;
Expand Down Expand Up @@ -58,28 +58,26 @@ export class UmbBackofficeModalContainerElement extends UmbLitElement {
return;
}

this._modals.forEach(async (modal) => {
if (this._modalElementMap.has(modal.key)) return;
this._modals.forEach(async (modalContext) => {
if (this._modalElementMap.has(modalContext.key)) return;

const modalElement = new UmbModalElement();
modalElement.modalContext = modal;
await modalElement.init(modalContext);

await modalElement.createModalElement();
modalElement.element?.addEventListener('close-end', this.#onCloseEnd.bind(this, modalContext.key));
modalContext.addEventListener('umb:destroy', this.#onCloseEnd.bind(this, modalContext.key));

modalElement.element?.addEventListener('close-end', this.#onCloseEnd.bind(this, modal.key));
modal.addEventListener('umb:destroy', this.#onCloseEnd.bind(this, modal.key));

this._modalElementMap.set(modal.key, modalElement);
this._modalElementMap.set(modalContext.key, modalElement);

// If any of the modals are fillBackground, set the fillBackground property to true
if (modal.backdropBackground) {
if (modalContext.backdropBackground) {
this.fillBackground = true;
this.shadowRoot
?.getElementById('container')
?.style.setProperty('--backdrop-background', modal.backdropBackground);
?.style.setProperty('--backdrop-background', modalContext.backdropBackground);
}

this.requestUpdate();
this.requestUpdate('_modalElementMap');
});
}

Expand Down
22 changes: 8 additions & 14 deletions src/packages/core/modal/component/modal.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ import {
@customElement('umb-modal')
export class UmbModalElement extends UmbLitElement {
#modalContext?: UmbModalContext;
public get modalContext(): UmbModalContext | undefined {
return this.#modalContext;
}
public set modalContext(value: UmbModalContext | undefined) {
if (this.#modalContext === value) return;
this.#modalContext = value;

if (!value) {
this.destroy();
return;
}
}

public element?: UUIModalDialogElement | UUIModalSidebarElement | UUIModalElement;

Expand All @@ -51,8 +39,14 @@ export class UmbModalElement extends UmbLitElement {
this.#modalContext?.reject({ type: 'close' });
};

async createModalElement() {
if (!this.#modalContext) return;
async init(modalContext: UmbModalContext | undefined) {
if (this.#modalContext === modalContext) return;
this.#modalContext = modalContext;

if (!this.#modalContext) {
this.destroy();
return;
}

this.#modalContext.addEventListener('umb:destroy', this.#onContextDestroy);
this.element = await this.#createContainerElement();
Expand Down

0 comments on commit 715d11a

Please sign in to comment.