Modal promises
class Modal {
show<R, E = void>(getModal: ModalGetter<R, E>): ModalChainable<R, E> {
// Injects modal into VDOM tree & returns modal chain
}
}
// Another Framework could be in use
export type ModalGetter<R, E> = (resolvers: ModalResolvers<R, E>) => React.ReactNode;
export interface ModalResolvers<R, E> {
resolve(r?: R): void;
reject(e?: E): void;
close: CloseHandler;
}
export interface ModalChainable<R, E> {
id: string;
then(handler: (r?: R) => void): Promise<any>;
catch(handler: (e: E) => void): Promise<any>;
close: CloseHandler;
onClose(handler: CloseHandler): ModalChainable<R, E>;
}
export type CloseHandler = () => void;
/**
* Playground
*/
declare var modal: Modal
modal.show(({ close, reject, resolve }) => (
<div>
<p>Modal</p>
<button onClick={() => resolve(1)}>Resolve</button>
<button onClick={() => reject(2)}>Reject</button>
<button onClick={close}>Close</button>
</div>
))
.onClose(/** just a callback */)
.then(/** 1 */)
.catch(/** 2 */)
What if a Modal would have Promise-like syntax?
8/27/2020