Open History to see the last commit
1import { getElementContext } from "react-grab/primitives";
2
3import { installPasskeyBridge } from "./PasskeyBridge.ts";
4
5export interface PageWorldHost {
6 /** Absent when this guest has no passkey bridge. */
7 readonly invokePasskey?: Parameters<typeof installPasskeyBridge>[0];
8}
9
10/**
11 * Runs in the page's own JavaScript world: React's fibers and `navigator.credentials` cannot be
12 * reached from the isolated preload. The preload serializes this function into the page, so the
13 * bundler inlines its imports (see `pageWorldFunction` in vite.config.ts), and everything it
14 * returns is page-controlled data.
15 */
16export function installPageWorld(host: PageWorldHost) {
17 if (host.invokePasskey) installPasskeyBridge(host.invokePasskey);
18 return {
19 elementContext: async (element: Element) => {
20 const context = await getElementContext(element);
21 return {
22 selector: context.selector,
23 htmlPreview: context.htmlPreview,
24 componentName: context.componentName,
25 styles: context.styles,
26 // Fibers and elements cannot cross back to the preload, so only plain fields are passed.
27 stack: context.stack?.map((frame) => ({
28 functionName: frame.functionName,
29 fileName: frame.fileName,
30 lineNumber: frame.lineNumber,
31 columnNumber: frame.columnNumber,
32 })),
33 };
34 },
35 };
36}