refactor: introduce modal manager (#18039)

This commit is contained in:
Daniel Dietzler 2025-05-03 00:41:42 +02:00 committed by GitHub
parent 15d431ba6a
commit 62fc5b3c7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 265 additions and 236 deletions

View file

@ -0,0 +1,33 @@
import ConfirmDialog from '$lib/components/shared-components/dialog/confirm-dialog.svelte';
import { mount, unmount, type Component, type ComponentProps } from 'svelte';
type OnCloseData<T> = T extends { onClose: (data: infer R) => void } ? R : never;
// TODO make `props` optional if component only has `onClose`
// type OptionalIfEmpty<T extends object> = keyof T extends never ? undefined : T;
class ModalManager {
open<T extends object, K = OnCloseData<T>>(Component: Component<T>, props: Omit<T, 'onClose'>) {
return new Promise<K>((resolve) => {
let modal: object = {};
const onClose = async (data: K) => {
await unmount(modal);
resolve(data);
};
modal = mount(Component, {
target: document.body,
props: {
...(props as T),
onClose,
},
});
});
}
openDialog(options: Omit<ComponentProps<typeof ConfirmDialog>, 'onClose'>) {
return this.open(ConfirmDialog, options);
}
}
export const modalManager = new ModalManager();