forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-framework-delegate.ts
40 lines (30 loc) · 1.24 KB
/
react-framework-delegate.ts
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
import React from 'react';
import ReactDOM from 'react-dom';
import { FrameworkDelegate } from '@ionic/core';
import { isElementModal, isElementNav } from './utils/helpers';
export function attachViewToDom(parentElement: HTMLElement, reactComponent: any, propsOrData: any, classesToAdd: string[]) {
const wrappingDiv = shouldWrapInIonPage(parentElement) ? document.createElement('ion-page') : document.createElement('div');
if (classesToAdd) {
for (const clazz of classesToAdd) {
wrappingDiv.classList.add(clazz);
}
}
parentElement.appendChild(wrappingDiv);
// mount the React component
const reactElement = React.createElement(reactComponent, propsOrData);
ReactDOM.render(reactElement, wrappingDiv);
return Promise.resolve(wrappingDiv);
}
export function removeViewFromDom(parentElement: HTMLElement, childElement: HTMLElement): Promise<any> {
ReactDOM.unmountComponentAtNode(childElement);
parentElement.removeChild(childElement);
return Promise.resolve();
}
const Delegate: FrameworkDelegate = {
attachViewToDom: attachViewToDom,
removeViewFromDom: removeViewFromDom,
};
export { Delegate }
export function shouldWrapInIonPage(element: HTMLElement) {
return isElementModal(element) || isElementNav(element);
}