-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
framework-delegate.ts
56 lines (46 loc) · 1.53 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { FrameworkDelegate } from "@ionic/core/components";
import type { VNode } from "vue";
import { h, Teleport } from "vue";
import {
addTeleportedUserComponent,
removeTeleportedUserComponent,
} from "./components/IonApp";
export const VueDelegate = (
addFn = addTeleportedUserComponent,
removeFn = removeTeleportedUserComponent
): FrameworkDelegate => {
// `h` doesn't provide a type for the component argument
const refMap = new WeakMap<any, VNode>();
// TODO(FW-2969): types
const attachViewToDom = (
parentElement: HTMLElement,
componentOrTagName: any | string,
componentProps: any = {},
classes?: string[]
) => {
const div = document.createElement("div");
classes && div.classList.add(...classes);
parentElement.appendChild(div);
const hostComponent = h(
Teleport,
{ to: div },
h(componentOrTagName, { ...componentProps })
);
/**
* Ionic Framework will use what is returned from `attachViewToDom`
* as the `component` argument in `removeViewFromDom`.
*
* We will store a reference to the div element and the host component,
* so we can later look-up and unmount the correct instance.
*/
refMap.set(div, hostComponent);
addFn(hostComponent);
return Promise.resolve(div);
};
const removeViewFromDom = (_container: any, component: any) => {
const hostComponent = refMap.get(component);
hostComponent && removeFn(hostComponent);
return Promise.resolve();
};
return { attachViewToDom, removeViewFromDom };
};