export const TeleportImpl = {
__isTeleport: true,
process(n1, n2, container, anchor, opreators) {
let { mountChildren, patchChildren, move, query } = opreators;
if (!n1) {
const target = (n2.target = query(n2.props.to));
if (target) {
mountChildren(n2.children, target, anchor);
}
} else {
patchChildren(n1, n2, n1.target);
n2.target = n1.target;
if (n2.props.to !== n1.props.to) {
const nextTarget = (n2.target = query(n2.props.to));
n2.children.forEach((child) => move(child, nextTarget, anchor));
}
}
},
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="app" style="width: 100px; height: 100px; overflow: hidden"></div>
<div id="root"></div>
<div id="home"></div>
<script type="module">
import { Teleport, h, Fragment, render } from "./runtime-dom.esm.js";
render(h(Teleport, { to: "#root" }, [h("div", "teleport")]), app);
setTimeout(() => {
render(h(Teleport, { to: "#home" }, [h("div", "teleport")]), app);
}, 2000);
</script>
</body>
</html>
query: hostQuerySelect
move(vnode, container, anchor) {
hostInsert(
vnode.component ? vnode.component.subTree.el : vnode.el,
container,
anchor
);
},