openModal
interface ModalOptions {
attachTo: string | Element;
props: Record<string, string | undefined>;
}
interface ModalResult<T> {
reason: ModalReason | string;
data: T;
}
type AsyncModalResult<T> = Promise<ModalResult<T>>;
function openModal<T>(
callingInstance: MaybeWithFKUIContext,
Component: MaybeComponent,
text: string,
): AsyncModalResult<T>;
function openModal<T>(
callingInstance: MaybeWithFKUIContext,
Component: MaybeComponent,
options?: Partial<ModalOptions>,
): AsyncModalResult<T>;
Provides an API to pragmatically open modal dialogs.
Usage
const result = await openModal(this, MyModalComponent, {
props: {
heading: "Modal title",
content: "Text to display in modal",
},
});
console.log("Modal closed with result", result);
// Modal closed with result { reason: "close", data: undefined }
API for custom modals
The given component must adhere to the following contract:
- When closed, no matter the reason, the modal MUST emit the
closeevent. - When closed, the modal SHOULD include
{ reason: string }as event payload. - When closed, the modal MAY include
{ data: T }as event payload. - The modal MUST be visible by default or use the prop
isOpento control visibility. - The modal SHOULD respect the
contentprop to set the primary text of the modal.
Until the close event has been received the caller will be blocked.
Both reason and data will be available in the resolved promise.