-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathmodal_service.tsx
224 lines (201 loc) · 6.86 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* 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, EuiConfirmModalProps } from '@elastic/eui';
import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { Subject } from 'rxjs';
import { I18nStart } from '../../i18n';
import { MountPoint } from '../../types';
import { OverlayRef } from '../types';
import { MountWrapper } from '../../utils';
/**
* 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;
}
}
/**
* @public
*/
export interface OverlayModalConfirmOptions {
title?: string;
cancelButtonText?: string;
confirmButtonText?: string;
className?: string;
closeButtonAriaLabel?: string;
'data-test-subj'?: string;
defaultFocusedButton?: EuiConfirmModalProps['defaultFocusedButton'];
buttonColor?: EuiConfirmModalProps['buttonColor'];
/**
* Sets the max-width of the modal.
* Set to `true` to use the default (`euiBreakpoints 'm'`),
* set to `false` to not restrict the width,
* set to a number for a custom width in px,
* set to a string for a custom width in custom measurement.
*/
maxWidth?: boolean | number | string;
}
/**
* APIs to open and manage modal dialogs.
*
* @public
*/
export interface OverlayModalStart {
/**
* Opens a modal panel with the given mount point inside. You can use
* `close()` on the returned OverlayRef to close the modal.
*
* @param mount {@link MountPoint} - Mounts the children inside the modal
* @param options {@link OverlayModalOpenOptions} - options for the modal
* @return {@link OverlayRef} A reference to the opened modal.
*/
open(mount: MountPoint, options?: OverlayModalOpenOptions): OverlayRef;
/**
* Opens a confirmation modal with the given text or mountpoint as a message.
* Returns a Promise resolving to `true` if user confirmed or `false` otherwise.
*
* @param message {@link MountPoint} - string or mountpoint to be used a the confirm message body
* @param options {@link OverlayModalConfirmOptions} - options for the confirm modal
*/
openConfirm(message: MountPoint | string, options?: OverlayModalConfirmOptions): Promise<boolean>;
}
/**
* @public
*/
export interface OverlayModalOpenOptions {
className?: string;
closeButtonAriaLabel?: string;
'data-test-subj'?: string;
maxWidth?: boolean | number | string;
}
interface StartDeps {
i18n: I18nStart;
targetDomElement: Element;
}
/** @internal */
export class ModalService {
private activeModal: ModalRef | null = null;
private targetDomElement: Element | null = null;
public start({ i18n, 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(
<i18n.Context>
<EuiModal {...options} onClose={() => modal.close()}>
<MountWrapper mount={mount} className="kbnOverlayMountWrapper" />
</EuiModal>
</i18n.Context>,
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(
<i18n.Context>
<EuiConfirmModal {...props} />
</i18n.Context>,
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;
}
}