feat: allow passing JSX children to island + nested islands (#1285)
This commit is contained in:
+40
-22
@@ -420,6 +420,31 @@ if (!supportsUnstableComments) {
|
||||
);
|
||||
}
|
||||
|
||||
function wrapWithMarker(vnode: ComponentChildren, markerText: string) {
|
||||
// Newer versions of preact-render-to-string allow you to render comments
|
||||
if (supportsUnstableComments) {
|
||||
return h(
|
||||
Fragment,
|
||||
null,
|
||||
h(Fragment, {
|
||||
// @ts-ignore unstable property is not typed
|
||||
UNSTABLE_comment: markerText,
|
||||
}),
|
||||
vnode,
|
||||
h(Fragment, {
|
||||
// @ts-ignore unstable property is not typed
|
||||
UNSTABLE_comment: "/" + markerText,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
return h(
|
||||
`!--${markerText}--`,
|
||||
null,
|
||||
vnode,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Set up a preact option hook to track when vnode with custom functions are
|
||||
// created.
|
||||
const ISLANDS: Island[] = [];
|
||||
@@ -440,31 +465,24 @@ options.vnode = (vnode) => {
|
||||
ENCOUNTERED_ISLANDS.add(island);
|
||||
vnode.type = (props) => {
|
||||
ignoreNext = true;
|
||||
|
||||
// Only passing children JSX to islands is supported for now
|
||||
if ("children" in props) {
|
||||
const children = props.children;
|
||||
// @ts-ignore nonono
|
||||
props.children = wrapWithMarker(
|
||||
children,
|
||||
`frsh-slot-${island.id}:children`,
|
||||
);
|
||||
}
|
||||
|
||||
const child = h(originalType, props);
|
||||
ISLAND_PROPS.push(props);
|
||||
|
||||
// Newer versions of preact-render-to-string allow you to render comments
|
||||
if (supportsUnstableComments) {
|
||||
return h(
|
||||
Fragment,
|
||||
null,
|
||||
h(Fragment, {
|
||||
// @ts-ignore unstable property is not typed
|
||||
UNSTABLE_comment: `frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
|
||||
}),
|
||||
child,
|
||||
h(Fragment, {
|
||||
// @ts-ignore unstable property is not typed
|
||||
UNSTABLE_comment: `/frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
return h(
|
||||
`!--frsh-${island.id}:${ISLAND_PROPS.length - 1}--`,
|
||||
null,
|
||||
child,
|
||||
);
|
||||
}
|
||||
return wrapWithMarker(
|
||||
child,
|
||||
`frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
*
|
||||
* The corresponding deserializer is in `src/runtime/deserializer.ts`.
|
||||
*/
|
||||
import { isValidElement, VNode } from "preact";
|
||||
import { KEY } from "../runtime/deserializer.ts";
|
||||
|
||||
interface SerializeResult {
|
||||
@@ -44,6 +45,13 @@ function isSignal(x: any): x is Signal {
|
||||
);
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
function isVNode(x: any): x is VNode {
|
||||
return x !== null && typeof x === "object" && "type" in x && "ref" in x &&
|
||||
"__k" in x &&
|
||||
isValidElement(x);
|
||||
}
|
||||
|
||||
export function serialize(data: unknown): SerializeResult {
|
||||
let requiresDeserializer = false;
|
||||
let hasSignals = false;
|
||||
@@ -108,6 +116,13 @@ export function serialize(data: unknown): SerializeResult {
|
||||
referenceArr.push(currentPath);
|
||||
}
|
||||
return 0;
|
||||
} else if (isVNode(value)) {
|
||||
requiresDeserializer = true;
|
||||
// No need to serialize JSX as we pick that up from
|
||||
// the rendered HTML in the browser.
|
||||
const res = null;
|
||||
parentStack.push(res);
|
||||
return res;
|
||||
} else {
|
||||
seen.set(value, currentPath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user