From 65ec5aa29405a2405c5be1095aef589a46732b75 Mon Sep 17 00:00:00 2001 From: wgqqqqq Date: Thu, 5 Nov 2026 16:40:02 +0800 Subject: [PATCH] fix(genui): follow OpenBitFun appearance theme --- .../generative-ui/GenUIHtmlDocument.test.tsx | 15 ++++ .../tools/generative-ui/GenUIHtmlDocument.tsx | 20 ++--- .../generative-ui/components/map/MapView.tsx | 3 +- .../components/openui/components.css | 8 +- .../components/openui/map/map.css | 2 +- .../generative-ui/components/openui/scheme.ts | 6 +- .../generative-ui/components/openui/theme.css | 73 +++++++++---------- .../tools/generative-ui/openuiTheme.test.ts | 28 +++++++ 8 files changed, 98 insertions(+), 57 deletions(-) create mode 100644 src/web-ui/src/tools/generative-ui/openuiTheme.test.ts diff --git a/src/web-ui/src/tools/generative-ui/GenUIHtmlDocument.test.tsx b/src/web-ui/src/tools/generative-ui/GenUIHtmlDocument.test.tsx index 7dcaa9de53..06f361ee72 100644 --- a/src/web-ui/src/tools/generative-ui/GenUIHtmlDocument.test.tsx +++ b/src/web-ui/src/tools/generative-ui/GenUIHtmlDocument.test.tsx @@ -8,6 +8,21 @@ describe('GenUI HTML documents', () => { expect(GENUI_HTML_RUNTIME).toEqual(genUICatalog.htmlRuntime); }); + it('passes the active Appearance mode into the isolated document', () => { + const result = sanitizeGenUIHtmlSource( + { kind: 'html-document', html: '
Theme aware
' }, + undefined, + ':root{color-scheme:dark;--openbitfun-color-content-primary:#f3f3f5}', + false, + false, + false, + 'dark', + ); + expect(result.document).toContain(''); + expect(result.document).toContain('color-scheme:dark'); + expect(result.document).toContain('--openbitfun-color-content-primary:#f3f3f5'); + }); + it('keeps HTML/SVG content while removing active and unsafe host paths', () => { const result = sanitizeGenUIHtmlSource({ kind: 'html-document', diff --git a/src/web-ui/src/tools/generative-ui/GenUIHtmlDocument.tsx b/src/web-ui/src/tools/generative-ui/GenUIHtmlDocument.tsx index ad73ad195d..2a8acec4cb 100644 --- a/src/web-ui/src/tools/generative-ui/GenUIHtmlDocument.tsx +++ b/src/web-ui/src/tools/generative-ui/GenUIHtmlDocument.tsx @@ -1,5 +1,6 @@ import { useMemo } from 'react'; import { useEffect, useRef } from 'react'; +import { useColorScheme } from './components/openui/scheme'; import { genUICatalog, type GenUIDesignSpec } from './genUIContract'; import type { GenUIHtmlDocumentSource } from './genUITypes'; @@ -220,7 +221,7 @@ function inspectionBootstrap(): string { * retained in the source record but are deliberately disabled by this static * renderer until a host has negotiated the controlled bridge capability. */ -export function sanitizeGenUIHtmlSource(source: GenUIHtmlDocumentSource, specification?: GenUIDesignSpec, hostStyleResourceCss = '', scriptEnabled = false, inspectionEnabled = false, inlineScriptEnabled = false): GenUIHtmlSanitizeResult { +export function sanitizeGenUIHtmlSource(source: GenUIHtmlDocumentSource, specification?: GenUIDesignSpec, hostStyleResourceCss = '', scriptEnabled = false, inspectionEnabled = false, inlineScriptEnabled = false, colorScheme: 'light' | 'dark' = 'light'): GenUIHtmlSanitizeResult { const diagnostics: string[] = []; const allowControlledScript = scriptEnabled && !inspectionEnabled; const scriptNonce = allowControlledScript || inspectionEnabled ? createCspNonce() : undefined; @@ -307,7 +308,7 @@ export function sanitizeGenUIHtmlSource(source: GenUIHtmlDocumentSource, specifi } if (scriptNonce) policy.setAttribute('content', `default-src 'none'; style-src 'unsafe-inline'; img-src data: blob:; font-src data:; connect-src 'none'; frame-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'; script-src 'nonce-${scriptNonce}'`); return { - document: `${parsed.head.innerHTML}${parsed.body.innerHTML}`, + document: `${parsed.head.innerHTML}${parsed.body.innerHTML}`, diagnostics, }; } @@ -346,8 +347,10 @@ export default function GenUIHtmlDocument({ source, specification, designSpecId, inspection?: GenUIHtmlInspection; }) { const frameRef = useRef(null); + const themeRef = useRef(null); + const colorScheme = useColorScheme(themeRef); const hostStyleResourceCss = useMemo(() => { - if (!specification?.styleResources.length || typeof document === 'undefined') return ''; + if (typeof document === 'undefined') return ''; const computed = getComputedStyle(document.documentElement); const tokens: string[] = []; for (let index = 0; index < computed.length; index++) { @@ -358,14 +361,11 @@ export default function GenUIHtmlDocument({ source, specification, designSpecId, // into the opaque document's inline stylesheet. if (value && !/[{};]/.test(value)) tokens.push(`${name}:${value};`); } - const base = tokens.length - ? `:root{${tokens.join('')}}html,body{margin:0;min-height:100%;color:var(--openbitfun-color-content-primary);background:var(--openbitfun-color-surface-canvas);font-family:var(--openbitfun-type-body-md-font-family);font-size:var(--openbitfun-type-body-md-font-size);line-height:var(--openbitfun-type-body-md-line-height);}` - : ''; - return base; - }, [specification]); + return `:root{color-scheme:${colorScheme};${tokens.join('')}}html,body{margin:0;min-height:100%;color:var(--openbitfun-color-content-primary);background:var(--openbitfun-color-surface-canvas);font-family:var(--openbitfun-type-body-md-font-family);font-size:var(--openbitfun-type-body-md-font-size);line-height:var(--openbitfun-type-body-md-line-height);}`; + }, [colorScheme]); const inspectionEnabled = Boolean(inspection); const canRunScript = Boolean(scriptEnabled && bridge && source.script?.trim() && !inspectionEnabled); - const sanitized = useMemo(() => sanitizeGenUIHtmlSource(source, specification, hostStyleResourceCss, canRunScript, inspectionEnabled, false), [source, specification, hostStyleResourceCss, canRunScript, inspectionEnabled]); + const sanitized = useMemo(() => sanitizeGenUIHtmlSource(source, specification, hostStyleResourceCss, canRunScript, inspectionEnabled, false, colorScheme), [source, specification, hostStyleResourceCss, canRunScript, inspectionEnabled, colorScheme]); useEffect(() => { if (!canRunScript || !bridge) return; const frame = frameRef.current; @@ -485,7 +485,7 @@ export default function GenUIHtmlDocument({ source, specification, designSpecId, window.clearInterval(retryTimer); }; }, [inspection]); - return
+ return