Skip to content

Commit 65c271c

Browse files
author
John Schulz
committed
plugin.start now does reactdom.render vs returning react element
export plugin function from public/index
1 parent 3b3eca1 commit 65c271c

2 files changed

Lines changed: 20 additions & 25 deletions

File tree

x-pack/legacy/plugins/integrations_manager/public/index.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
import ReactDOM from 'react-dom';
76
import euiLight from '@elastic/eui/dist/eui_theme_light.json';
87
import euiDark from '@elastic/eui/dist/eui_theme_dark.json';
98
import 'ui/autoload/all';
109
import 'ui/autoload/styles';
1110
import chrome from 'ui/chrome';
1211
import { npSetup, npStart } from 'ui/new_platform';
13-
import { Plugin, PluginInitializerContext, PluginStart } from './plugin';
12+
import { Plugin, PluginInitializerContext } from './plugin';
1413
import { routes } from './routes';
1514

1615
// create './types' later and move there?
@@ -20,35 +19,32 @@ const REACT_APP_ROOT_ID = 'epm__root';
2019
const template = `<div id="${REACT_APP_ROOT_ID}" style="flex-grow: 1; display: flex; flex-direction: column"></div>`;
2120
const getRootEl = () => document.getElementById(REACT_APP_ROOT_ID);
2221

23-
main();
22+
export const plugin = (initializerContext: PluginInitializerContext = {}) =>
23+
new Plugin(initializerContext);
2424

25-
async function main(): Promise<void> {
26-
const initializerContext: PluginInitializerContext = {};
27-
const plugin = new Plugin(initializerContext);
28-
plugin.setup(npSetup.core);
25+
const epmPlugin = plugin();
26+
epmPlugin.setup(npSetup.core);
2927

30-
// @ts-ignore
31-
chrome.setRootTemplate(template);
32-
33-
await waitFor(getRootEl);
28+
// @ts-ignore
29+
chrome.setRootTemplate(template);
3430

3531
const isDarkMode = npStart.core.uiSettings.get('theme:darkMode');
36-
const { root }: PluginStart = plugin.start({
32+
waitForElement(getRootEl).then(element => {
33+
epmPlugin.start({
3734
...npStart.core,
3835
routes,
3936
theme: { eui: isDarkMode ? euiDark : euiLight },
37+
renderTo: element,
4038
});
39+
});
4140

42-
const container = getRootEl();
43-
ReactDOM.render(root, container);
44-
}
45-
46-
function waitFor(fn: () => any) {
41+
function waitForElement(fn: () => HTMLElement | null): Promise<HTMLElement> {
4742
return new Promise(resolve => {
48-
if (fn()) {
49-
resolve();
43+
const element = fn();
44+
if (element) {
45+
resolve(element);
5046
} else {
51-
setTimeout(() => resolve(waitFor(fn)), 10);
47+
setTimeout(() => resolve(waitForElement(fn)), 10);
5248
}
5349
});
5450
}

x-pack/legacy/plugins/integrations_manager/public/plugin.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import React from 'react';
8+
import ReactDOM from 'react-dom';
89
import { HashRouter, Switch } from 'react-router-dom';
910
import { ThemeProvider } from 'styled-components';
1011
import { EuiErrorBoundary } from '@elastic/eui';
@@ -29,6 +30,7 @@ export interface PluginCore {
2930
i18n: I18nStart;
3031
routes: JSX.Element[];
3132
theme: PluginTheme;
33+
renderTo: HTMLElement;
3234
}
3335

3436
export class Plugin {
@@ -38,14 +40,11 @@ export class Plugin {
3840
// called after all plugins are set up
3941
public start(core: PluginCore) {
4042
setClient(core.http.fetch);
41-
42-
return {
43-
root: <Root core={core} />,
44-
};
43+
ReactDOM.render(<App core={core} />, core.renderTo);
4544
}
4645
}
4746

48-
function Root(props: { core: PluginCore }) {
47+
function App(props: { core: PluginCore }) {
4948
const { i18n, routes } = props.core;
5049

5150
return (

0 commit comments

Comments
 (0)