mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
chore(packages): move the packages to root
This commit is contained in:
22
react/src/apis/apis.ts
Normal file
22
react/src/apis/apis.ts
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
import { ModalOptions } from '@ionic/core';
|
||||
|
||||
import { Delegate } from '../react-framework-delegate';
|
||||
import { getOrAppendElement } from '../utils/helpers';
|
||||
|
||||
export function createModal(opts: ModalOptions): Promise<HTMLIonModalElement> {
|
||||
return createOverlayInternal('ion-modal-controller', opts);
|
||||
}
|
||||
|
||||
export function createPopover(opts: ModalOptions): Promise<HTMLIonModalElement> {
|
||||
return createOverlayInternal('ion-popover-controller', opts);
|
||||
}
|
||||
|
||||
|
||||
function createOverlayInternal(controllerTagName: string, opts: any) {
|
||||
opts.delegate = Delegate;
|
||||
const element = getOrAppendElement(controllerTagName) as HTMLIonModalControllerElement;
|
||||
return (element as any).componentOnReady().then(() => {
|
||||
return element.create(opts);
|
||||
});
|
||||
}
|
2
react/src/declarations.d.ts
vendored
Normal file
2
react/src/declarations.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
declare module 'react';
|
||||
declare module 'react-dom';
|
3
react/src/index.ts
Normal file
3
react/src/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export { Delegate } from './react-framework-delegate';
|
||||
export * from './apis/apis';
|
||||
export * from './utils/wc-shim';
|
44
react/src/react-framework-delegate.ts
Normal file
44
react/src/react-framework-delegate.ts
Normal file
@ -0,0 +1,44 @@
|
||||
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);
|
||||
const mountedComponentInstance = ReactDOM.render(reactElement, wrappingDiv);
|
||||
|
||||
return Promise.resolve({
|
||||
element: wrappingDiv,
|
||||
reactElement: reactElement,
|
||||
instance: mountedComponentInstance
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
18
react/src/utils/helpers.ts
Normal file
18
react/src/utils/helpers.ts
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
export function getOrAppendElement(tagName: string): Element {
|
||||
const element = document.querySelector(tagName);
|
||||
if (element) {
|
||||
return element;
|
||||
}
|
||||
const tmp = document.createElement(tagName);
|
||||
document.body.appendChild(tmp);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
export function isElementNav(element: HTMLElement) {
|
||||
return element.tagName.toUpperCase() === 'ION-NAV';
|
||||
}
|
||||
|
||||
export function isElementModal(element: HTMLElement) {
|
||||
return element.classList.contains('modal-wrapper');
|
||||
}
|
24
react/src/utils/wc-shim.ts
Normal file
24
react/src/utils/wc-shim.ts
Normal file
@ -0,0 +1,24 @@
|
||||
export function wc(events = {}, obj = {}) {
|
||||
let storedEl: HTMLElement;
|
||||
|
||||
return function (el: HTMLElement) {
|
||||
|
||||
(Object as any).entries(events).forEach((keyValues: string[]) => {
|
||||
const name = keyValues[0];
|
||||
const value = keyValues[1];
|
||||
const action = (el) ? el.addEventListener : storedEl.removeEventListener;
|
||||
if (typeof value === 'function') {
|
||||
action(name, value);
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (el) {
|
||||
(Object as any).entries(obj).forEach((keyValues: string[]) => {
|
||||
const name = keyValues[0];
|
||||
const value = keyValues[1];
|
||||
(el as any)[name] = value;
|
||||
});
|
||||
}
|
||||
storedEl = el;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user