-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathflyout_service.tsx
149 lines (131 loc) · 4.56 KB
/
flyout_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
/*
* 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 { EuiFlyout, EuiFlyoutSize } 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 FlyoutRef is a reference to an opened flyout panel. It offers methods to
* close the flyout panel again. If you open a flyout panel you should make
* sure you call `close()` when it should be closed.
* Since a flyout could also be closed by a user or from another flyout being
* opened, you must bind to the `onClose` Promise on the FlyoutRef instance.
* The Promise will resolve whenever the flyout was closed at which point you
* should discard the FlyoutRef.
*
* @public
*/
class FlyoutRef implements OverlayRef {
/**
* An Promise that will resolve once this flyout is closed.
*
* Flyouts can close from user interaction, calling `close()` on the flyout
* reference or another call to `openFlyout()` replacing your flyout.
*/
public readonly onClose: Promise<void>;
private closeSubject = new Subject<void>();
constructor() {
this.onClose = this.closeSubject.toPromise();
}
/**
* Closes the referenced flyout if it's still open which in turn will
* resolve the `onClose` Promise. If the flyout 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;
}
}
/**
* APIs to open and manage fly-out dialogs.
*
* @public
*/
export interface OverlayFlyoutStart {
/**
* Opens a flyout panel with the given mount point inside. You can use
* `close()` on the returned FlyoutRef to close the flyout.
*
* @param mount {@link MountPoint} - Mounts the children inside a flyout panel
* @param options {@link OverlayFlyoutOpenOptions} - options for the flyout
* @return {@link OverlayRef} A reference to the opened flyout panel.
*/
open(mount: MountPoint, options?: OverlayFlyoutOpenOptions): OverlayRef;
}
/**
* @public
*/
export interface OverlayFlyoutOpenOptions {
className?: string;
closeButtonAriaLabel?: string;
ownFocus?: boolean;
'data-test-subj'?: string;
size?: EuiFlyoutSize;
maxWidth?: boolean | number | string;
}
interface StartDeps {
i18n: I18nStart;
targetDomElement: Element;
}
/** @internal */
export class FlyoutService {
private activeFlyout: FlyoutRef | null = null;
private targetDomElement: Element | null = null;
public start({ i18n, targetDomElement }: StartDeps): OverlayFlyoutStart {
this.targetDomElement = targetDomElement;
return {
open: (mount: MountPoint, options: OverlayFlyoutOpenOptions = {}): OverlayRef => {
// If there is an active flyout session close it before opening a new one.
if (this.activeFlyout) {
this.activeFlyout.close();
this.cleanupDom();
}
const flyout = new FlyoutRef();
// If a flyout gets closed through it's FlyoutRef, remove it from the dom
flyout.onClose.then(() => {
if (this.activeFlyout === flyout) {
this.cleanupDom();
}
});
this.activeFlyout = flyout;
render(
<i18n.Context>
<EuiFlyout {...options} onClose={() => flyout.close()}>
<MountWrapper mount={mount} className="kbnOverlayMountWrapper" />
</EuiFlyout>
</i18n.Context>,
this.targetDomElement
);
return flyout;
},
};
}
/**
* 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.activeFlyout = null;
}
}