fix(plugin): prevent theme flash in embedded workbenches
This commit is contained in:
4 files changed
+49
-4
No files matched your search
@@ -124,7 +124,7 @@ async function loadWorkbench() {
|
|||||||
const bytes = Uint8Array.from(atob(asset.dataBase64), (character) => character.charCodeAt(0));
|
const bytes = Uint8Array.from(atob(asset.dataBase64), (character) => character.charCodeAt(0));
|
||||||
const html = await inlineLocalUiAssets(new TextDecoder().decode(bytes), props.plugin.manifest.id);
|
const html = await inlineLocalUiAssets(new TextDecoder().decode(bytes), props.plugin.manifest.id);
|
||||||
if (disposed || generation !== loadGeneration) return;
|
if (disposed || generation !== loadGeneration) return;
|
||||||
source.value = pluginSandboxDocument(html, props.plugin.manifest.permissions);
|
source.value = pluginSandboxDocument(html, props.plugin.manifest.permissions, currentBridgeTheme());
|
||||||
await nextTick();
|
await nextTick();
|
||||||
if (disposed || generation !== loadGeneration) return;
|
if (disposed || generation !== loadGeneration) return;
|
||||||
createBridge();
|
createBridge();
|
||||||
|
|||||||
@@ -297,6 +297,18 @@ describe("PluginHostBridge", () => {
|
|||||||
expect(document).toContain("var(--color-background");
|
expect(document).toContain("var(--color-background");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("bootstraps the initial theme before the plugin document paints", () => {
|
||||||
|
const document = pluginSandboxDocument("<html><head></head><body></body></html>", [], {
|
||||||
|
appearance: "dark",
|
||||||
|
tokens: { "--color-background": "rgb(19 20 22)", "--color-destructive": "rgb(243 98 95)" },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(document).toContain('root.dataset.dbxTheme = theme.appearance === "dark" ? "dark" : "light"');
|
||||||
|
expect(document).toContain('root.style.colorScheme = theme.appearance === "dark" ? "dark" : "light"');
|
||||||
|
expect(document).toContain('"--color-background":"rgb(19 20 22)"');
|
||||||
|
expect(document).toContain('"--color-destructive":"rgb(243 98 95)"');
|
||||||
|
});
|
||||||
|
|
||||||
it("forwards the plugin close-tab shortcut to the host", () => {
|
it("forwards the plugin close-tab shortcut to the host", () => {
|
||||||
const target = { postMessage: vi.fn() } as unknown as Window;
|
const target = { postMessage: vi.fn() } as unknown as Window;
|
||||||
const closeTab = vi.fn();
|
const closeTab = vi.fn();
|
||||||
|
|||||||
@@ -217,17 +217,24 @@ export function pluginNetworkOrigins(permissions: readonly string[] | undefined)
|
|||||||
return [...origins];
|
return [...origins];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function pluginSandboxDocument(html: string, permissions?: readonly string[]): string {
|
export function pluginSandboxDocument(html: string, permissions?: readonly string[], theme?: PluginBridgeTheme): string {
|
||||||
const networkOrigins = pluginNetworkOrigins(permissions);
|
const networkOrigins = pluginNetworkOrigins(permissions);
|
||||||
const connectSrc = networkOrigins.length > 0 ? `connect-src ${networkOrigins.join(" ")};` : "connect-src 'none';";
|
const connectSrc = networkOrigins.length > 0 ? `connect-src ${networkOrigins.join(" ")};` : "connect-src 'none';";
|
||||||
const csp = `<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'unsafe-inline' blob:; style-src 'unsafe-inline' blob:; img-src data: blob:; font-src data: blob:; ${connectSrc} media-src data: blob:;">`;
|
const csp = `<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'unsafe-inline' blob:; style-src 'unsafe-inline' blob:; img-src data: blob:; font-src data: blob:; ${connectSrc} media-src data: blob:;">`;
|
||||||
const sdk = `<script>${pluginSdkSource()}</script>`;
|
const sdk = `<script>${pluginSdkSource()}</script>`;
|
||||||
const uiKit = `<style>${pluginUiKitCss()}</style>`;
|
const uiKit = `<style>${pluginUiKitCss()}</style>`;
|
||||||
const injection = `${csp}${uiKit}${sdk}`;
|
const themeBootstrap = pluginThemeBootstrap(theme);
|
||||||
|
const injection = `${csp}${uiKit}${themeBootstrap}${sdk}`;
|
||||||
if (/<head(?:\s[^>]*)?>/i.test(html)) return html.replace(/<head(?:\s[^>]*)?>/i, (head) => `${head}${injection}`);
|
if (/<head(?:\s[^>]*)?>/i.test(html)) return html.replace(/<head(?:\s[^>]*)?>/i, (head) => `${head}${injection}`);
|
||||||
return `<!doctype html><html><head>${injection}</head><body>${html}</body></html>`;
|
return `<!doctype html><html><head>${injection}</head><body>${html}</body></html>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function pluginThemeBootstrap(theme?: PluginBridgeTheme): string {
|
||||||
|
if (!theme) return "";
|
||||||
|
const serializedTheme = JSON.stringify(theme).replace(/</g, "\\u003c");
|
||||||
|
return `<script>(() => { const theme = ${serializedTheme}; const root = document.documentElement; root.dataset.dbxTheme = theme.appearance === "dark" ? "dark" : "light"; root.style.colorScheme = theme.appearance === "dark" ? "dark" : "light"; for (const [name, value] of Object.entries(theme.tokens || {})) { if (/^--[a-z0-9-]+$/i.test(name) && typeof value === "string") root.style.setProperty(name, value); } })();</script>`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minimal official component kit for plugin workbenches. Every class is built
|
* Minimal official component kit for plugin workbenches. Every class is built
|
||||||
* on the DBX design tokens the host pushes through the bridge, so plugin UI
|
* on the DBX design tokens the host pushes through the bridge, so plugin UI
|
||||||
|
|||||||
@@ -93,11 +93,37 @@ const themes = () => ({
|
|||||||
? {
|
? {
|
||||||
"--color-background": "#18181b",
|
"--color-background": "#18181b",
|
||||||
"--color-foreground": "#f4f4f5",
|
"--color-foreground": "#f4f4f5",
|
||||||
|
"--color-card": "#1b1b1f",
|
||||||
|
"--color-card-foreground": "#f4f4f5",
|
||||||
"--color-muted": "#27272a",
|
"--color-muted": "#27272a",
|
||||||
|
"--color-muted-foreground": "#a1a1aa",
|
||||||
"--color-border": "#3f3f46",
|
"--color-border": "#3f3f46",
|
||||||
|
"--color-input": "#3f3f46",
|
||||||
|
"--color-ring": "#71717a",
|
||||||
"--color-primary": "#60a5fa",
|
"--color-primary": "#60a5fa",
|
||||||
|
"--color-primary-foreground": "#18181b",
|
||||||
|
"--color-destructive": "#f3625f",
|
||||||
|
"--color-destructive-foreground": "#18181b",
|
||||||
|
"--radius-md": "6px",
|
||||||
|
"--radius-lg": "8px",
|
||||||
}
|
}
|
||||||
: { "--color-background": "#ffffff", "--color-foreground": "#27272a", "--color-muted": "#f4f4f5", "--color-border": "#e4e4e7", "--color-primary": "#2563eb" },
|
: {
|
||||||
|
"--color-background": "#ffffff",
|
||||||
|
"--color-foreground": "#27272a",
|
||||||
|
"--color-card": "#ffffff",
|
||||||
|
"--color-card-foreground": "#27272a",
|
||||||
|
"--color-muted": "#f4f4f5",
|
||||||
|
"--color-muted-foreground": "#71717a",
|
||||||
|
"--color-border": "#e4e4e7",
|
||||||
|
"--color-input": "#e4e4e7",
|
||||||
|
"--color-ring": "#93c5fd",
|
||||||
|
"--color-primary": "#2563eb",
|
||||||
|
"--color-primary-foreground": "#ffffff",
|
||||||
|
"--color-destructive": "#e7000b",
|
||||||
|
"--color-destructive-foreground": "#ffffff",
|
||||||
|
"--radius-md": "6px",
|
||||||
|
"--radius-lg": "8px",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
function post(frame, message) {
|
function post(frame, message) {
|
||||||
const snapshot = hostMessage(frame.channel, message);
|
const snapshot = hostMessage(frame.channel, message);
|
||||||
|
|||||||
Reference in new issue
Block a user