-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
modal_service.tsx
176 lines (156 loc) · 5.63 KB
/
modal_service.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
/* eslint-disable max-classes-per-file */
import { i18n as t } from '@kbn/i18n';
import { EuiModal, EuiConfirmModal } from '@elastic/eui';
import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { Subject } from 'rxjs';
import type { AnalyticsServiceStart } from '@kbn/core-analytics-browser';
import type { ThemeServiceStart } from '@kbn/core-theme-browser';
import type { I18nStart } from '@kbn/core-i18n-browser';
import type { MountPoint, OverlayRef } from '@kbn/core-mount-utils-browser';
import { MountWrapper } from '@kbn/core-mount-utils-browser-internal';
import type {
OverlayModalConfirmOptions,
OverlayModalOpenOptions,
OverlayModalStart,
} from '@kbn/core-overlays-browser';
import { KibanaRenderContextProvider } from '@kbn/react-kibana-context-render';
/**
* A ModalRef is a reference to an opened modal. It offers methods to
* close the modal.
*
* @public
*/
class ModalRef implements OverlayRef {
public readonly onClose: Promise<void>;
private closeSubject = new Subject<void>();
constructor() {
this.onClose = this.closeSubject.toPromise();
}
/**
* Closes the referenced modal if it's still open which in turn will
* resolve the `onClose` Promise. If the modal had already been
* closed this method does nothing.
*/
public close(): Promise<void> {
if (!this.closeSubject.closed) {
this.closeSubject.next();
this.closeSubject.complete();
}
return this.onClose;
}
}
interface StartDeps {
i18n: I18nStart;
theme: ThemeServiceStart;
analytics: AnalyticsServiceStart;
targetDomElement: Element;
}
/** @internal */
export class ModalService {
private activeModal: ModalRef | null = null;
private targetDomElement: Element | null = null;
public start({ analytics, i18n, theme, targetDomElement }: StartDeps): OverlayModalStart {
this.targetDomElement = targetDomElement;
return {
open: (mount: MountPoint, options: OverlayModalOpenOptions = {}): OverlayRef => {
// If there is an active modal, close it before opening a new one.
if (this.activeModal) {
this.activeModal.close();
this.cleanupDom();
}
const modal = new ModalRef();
// If a modal gets closed through it's ModalRef, remove it from the dom
modal.onClose.then(() => {
if (this.activeModal === modal) {
this.cleanupDom();
}
});
this.activeModal = modal;
render(
<KibanaRenderContextProvider analytics={analytics} i18n={i18n} theme={theme}>
<EuiModal {...options} onClose={() => modal.close()}>
<MountWrapper mount={mount} className="kbnOverlayMountWrapper" />
</EuiModal>
</KibanaRenderContextProvider>,
targetDomElement
);
return modal;
},
openConfirm: (message: MountPoint | string, options?: OverlayModalConfirmOptions) => {
// If there is an active modal, close it before opening a new one.
if (this.activeModal) {
this.activeModal.close();
this.cleanupDom();
}
return new Promise((resolve, reject) => {
let resolved = false;
const closeModal = (confirmed: boolean) => {
resolved = true;
modal.close();
resolve(confirmed);
};
const modal = new ModalRef();
modal.onClose.then(() => {
if (this.activeModal === modal) {
this.cleanupDom();
}
// modal.close can be called when opening a new modal/confirm, so we need to resolve the promise in that case.
if (!resolved) {
closeModal(false);
}
});
this.activeModal = modal;
const props = {
...options,
children:
typeof message === 'string' ? (
message
) : (
<MountWrapper mount={message} className="kbnOverlayMountWrapper" />
),
onCancel: () => closeModal(false),
onConfirm: () => closeModal(true),
cancelButtonText:
options?.cancelButtonText ||
t.translate('core.overlays.confirm.cancelButton', {
defaultMessage: 'Cancel',
}),
confirmButtonText:
options?.confirmButtonText ||
t.translate('core.overlays.confirm.okButton', {
defaultMessage: 'Confirm',
}),
};
render(
<KibanaRenderContextProvider analytics={analytics} i18n={i18n} theme={theme}>
<EuiConfirmModal {...props} />
</KibanaRenderContextProvider>,
targetDomElement
);
});
},
};
}
/**
* Using React.Render to re-render into a target DOM element will replace
* the content of the target but won't call unmountComponent on any
* components inside the target or any of their children. So we properly
* cleanup the DOM here to prevent subtle bugs in child components which
* depend on unmounting for cleanup behaviour.
*/
private cleanupDom(): void {
if (this.targetDomElement != null) {
unmountComponentAtNode(this.targetDomElement);
this.targetDomElement.innerHTML = '';
}
this.activeModal = null;
}
}