@smallstack/svelte-ui
Version:
Tiny library for Svelte 5 and DaisyUI, published as multi entry ESM module and as web components.
51 lines (50 loc) • 1.49 kB
JavaScript
import SimpleModal from "./SimpleModal.svelte";
export const ModalOkBtn = {
text: "OK",
color: "primary",
onClick: async ({ closeModal }) => {
closeModal();
}
};
export const ModalCancelBtn = {
text: "Abbrechen",
color: "default",
onClick: async ({ closeModal }) => {
closeModal();
}
};
class ModalService {
modalComponent;
storedModals = [];
registerModalContainer(modalComponent) {
if (!this.modalComponent) {
this.modalComponent = modalComponent;
if (this.storedModals.length > 0)
this.storedModals.forEach(({ component, options }) => {
this.openModal(component, options);
});
}
}
async openSimpleModal(options) {
if (!this.modalComponent)
alert(options.title + ": " + options.message);
else {
if (!options.buttons)
options.buttons = [ModalOkBtn];
await this.openModal(SimpleModal, { data: options, title: options.title });
}
}
async openModal(component, options) {
if (!this.modalComponent) {
this.storedModals.push({ component, options });
}
else {
await this.modalComponent.showModal(component, options);
}
}
/** closes the currently opened modal */
closeModal(data) {
this.modalComponent.closeModal(data);
}
}
export const modalService = new ModalService();