Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions packages/caplet-sandbox-prototype/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@ocap/caplet-sandbox-prototype",
"version": "0.0.0",
"private": true,
"description": "Prototype for sandboxed iframe + Preact UI architecture for caplets",
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/ocap-kernel.git"
},
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write && yarn constraints --fix && yarn lint:dependencies",
"lint:misc": "prettier --no-error-on-unmatched-pattern '**/*.json' '**/*.md' '**/*.html' '!**/CHANGELOG.old.md' '**/*.yml' '!.yarnrc.yml' '!merged-packages/**' --ignore-path ../../.gitignore --log-level error",
"test": "vitest run --config vitest.config.ts",
"test:dev": "yarn test --mode development",
"test:dev:quiet": "yarn test:dev --reporter @ocap/repo-tools/vitest-reporters/silent",
"build:docs": "typedoc",
"test:clean": "yarn test --no-cache --coverage.clean",
"test:verbose": "yarn test --reporter verbose",
"test:watch": "vitest --config vitest.config.ts"
},
"dependencies": {
"preact": "^10.26.4"
},
"devDependencies": {
"@metamask/eslint-config": "^15.0.0",
"@metamask/eslint-config-nodejs": "^15.0.0",
"@metamask/eslint-config-typescript": "^15.0.0",
"@ocap/repo-tools": "workspace:^",
"@preact/preset-vite": "^2.10.2",
"@typescript-eslint/eslint-plugin": "^8.29.0",
"@typescript-eslint/parser": "^8.29.0",
"@typescript-eslint/utils": "^8.29.0",
"@vitest/eslint-plugin": "^1.6.5",
"depcheck": "^1.4.7",
"eslint": "^9.23.0",
"eslint-config-prettier": "^10.1.1",
"eslint-import-resolver-typescript": "^4.3.1",
"eslint-plugin-import-x": "^4.10.0",
"eslint-plugin-jsdoc": "^50.6.9",
"eslint-plugin-n": "^17.17.0",
"eslint-plugin-prettier": "^5.2.6",
"eslint-plugin-promise": "^7.2.1",
"jsdom": "^27.4.0",
"prettier": "^3.5.3",
"rimraf": "^6.0.1",
"turbo": "^2.5.6",
"typescript": "~5.8.2",
"typescript-eslint": "^8.29.0",
"vite": "^7.3.0",
"vitest": "^4.0.16"
},
"engines": {
"node": "^20.11 || >=22"
},
"exports": {
"./package.json": "./package.json"
}
}
56 changes: 56 additions & 0 deletions packages/caplet-sandbox-prototype/src/caplet/Slot.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { useEffect, useRef } from 'preact/hooks';

type SlotProps = {
widgetId: string;
widgetUrl: string;
style?: preact.JSX.CSSProperties;
};

/**
* Slot component for embedding caplet-backed widgets.
* Creates a nested sandboxed iframe that communicates with window.top.
*
* @param props - Slot properties.
* @param props.widgetId - Unique identifier for the widget caplet.
* @param props.widgetUrl - URL of the widget iframe content.
* @param props.style - Optional CSS styles for the container.
* @returns Slot container element.
*/
export function Slot({
widgetId,
widgetUrl,
style,
}: SlotProps): preact.JSX.Element {
const containerRef = useRef<HTMLDivElement>(null);

useEffect(() => {
if (!containerRef.current) {
return undefined;
}

const iframe = document.createElement('iframe');
iframe.sandbox.add('allow-scripts');
iframe.sandbox.add('allow-same-origin'); // Required for dev server
iframe.src = `${widgetUrl}?capletId=${encodeURIComponent(widgetId)}`;
iframe.style.width = '100%';
iframe.style.height = '100%';
iframe.style.border = 'none';

containerRef.current.appendChild(iframe);

return () => {
iframe.remove();
};
}, [widgetId, widgetUrl]);

return (
<div
ref={containerRef}
style={{
width: '100%',
minHeight: '100px',
...style,
}}
/>
);
}
38 changes: 38 additions & 0 deletions packages/caplet-sandbox-prototype/src/caplet/bootstrap.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { render } from 'preact';

import { initBridge } from './caplet-bridge.ts';

/**
* Gets the capletId from URL query parameters.
*
* @returns The capletId or throws if not found.
*/
function getCapletIdFromUrl(): string {
const params = new URLSearchParams(window.location.search);
const capletId = params.get('capletId');
if (!capletId) {
throw new Error('Missing capletId in URL query parameters');
}
return capletId;
}

/**
* Bootstraps a caplet by initializing the bridge and rendering the app.
*
* @param AppComponent - The Preact component to render.
*/
export async function bootstrapCaplet(
AppComponent: () => preact.JSX.Element,
): Promise<void> {
const capletId = getCapletIdFromUrl();
const bridge = initBridge(capletId);

await bridge.waitForInit();

const appElement = document.getElementById('app');
if (!appElement) {
throw new Error('App element not found');
}

render(<AppComponent />, appElement);
}
Loading
Loading