feat: support conditional island JSX children (#1355)

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
Marvin Hagemeister
2023-08-08 11:35:28 +02:00
committed by GitHub
co-authored by Asher Gomez
parent dff80d3a42
commit 11077227b5
8 changed files with 205 additions and 49 deletions
+37 -5
View File
@@ -97,7 +97,7 @@ interface Marker {
// string representing the actual intended comment value which makes
// a bunch of stuff easier.
text: string;
startNode: Comment;
startNode: Comment | null;
endNode: Comment | null;
}
@@ -190,7 +190,7 @@ function _walkInner(
}
// Remove markers
marker.startNode.remove();
marker.startNode?.remove();
sib = sib.nextSibling;
marker.endNode.remove();
continue;
@@ -203,12 +203,44 @@ function _walkInner(
let child: Node | null = marker.startNode;
while (
(child = child.nextSibling) !== null && child !== marker.endNode
(child = child!.nextSibling) !== null && child !== marker.endNode
) {
children.push(child);
}
const vnode = vnodeStack.pop();
const vnode = vnodeStack[vnodeStack.length - 1];
if (vnode.props.children == null) {
const [id, exportName, n] = comment.slice("/frsh-".length).split(
":",
);
const sel = `#frsh-slot-${id}-${exportName}-${n}-children`;
const template = document.querySelector(sel) as
| HTMLTemplateElement
| null;
if (template !== null) {
markerStack.push({
kind: MarkerKind.Slot,
endNode: null,
startNode: null,
text: "foo",
});
const node = template.content.cloneNode(true);
_walkInner(
islands,
props,
markerStack,
vnodeStack,
node,
);
markerStack.pop();
}
}
vnodeStack.pop();
const parentNode = sib.parentNode! as HTMLElement;
@@ -242,7 +274,7 @@ function _walkInner(
: setTimeout(_render, 0);
// Remove markers
marker.startNode.remove();
marker.startNode?.remove();
sib = sib.nextSibling;
marker.endNode.remove();
continue;
+32 -2
View File
@@ -180,6 +180,8 @@ export async function render<Data>(
// Clear the island props
ISLAND_PROPS = [];
// Clear previous slots
SLOTS_TRACKER.clear();
const ctx = new RenderContext(
crypto.randomUUID(),
@@ -228,6 +230,13 @@ export async function render<Data>(
}),
});
bodyHtml = renderToString(root);
for (const [id, children] of SLOTS_TRACKER.entries()) {
const slotHtml = renderToString(h(Fragment, null, children));
const templateId = id.replace(/:/g, "-");
bodyHtml += `<template id="${templateId}">${slotHtml}</template>`;
}
return bodyHtml;
}
@@ -539,6 +548,15 @@ function wrapWithMarker(vnode: ComponentChildren, markerText: string) {
const ISLANDS: Island[] = [];
const ENCOUNTERED_ISLANDS: Set<Island> = new Set([]);
let ISLAND_PROPS: unknown[] = [];
// Track unused slots
const SLOTS_TRACKER = new Map<string, ComponentChildren>();
function SlotTracker(
props: { id: string; children?: ComponentChildren },
): VNode {
SLOTS_TRACKER.delete(props.id);
// deno-lint-ignore no-explicit-any
return props.children as any;
}
// Keep track of which component rendered which vnode. This allows us
// to detect when an island is rendered within another instead of being
@@ -588,13 +606,25 @@ options.vnode = (vnode) => {
vnode.type = (props) => {
ignoreNext = true;
const id = ISLAND_PROPS.length;
// Only passing children JSX to islands is supported for now
if ("children" in props) {
const children = props.children;
let children = props.children;
const markerText =
`frsh-slot-${island.id}:${island.exportName}:${id}:children`;
// @ts-ignore nonono
props.children = wrapWithMarker(
children,
`frsh-slot-${island.id}:children`,
markerText,
);
SLOTS_TRACKER.set(markerText, children);
children = props.children;
// deno-lint-ignore no-explicit-any
(props as any).children = h(
SlotTracker,
{ id: markerText },
children,
);
}