11import { useMemo } from 'react' ;
22import { useEffect , useRef } from 'react' ;
3+ import { useColorScheme } from './components/openui/scheme' ;
34import { genUICatalog , type GenUIDesignSpec } from './genUIContract' ;
45import type { GenUIHtmlDocumentSource } from './genUITypes' ;
56
@@ -220,7 +221,7 @@ function inspectionBootstrap(): string {
220221 * retained in the source record but are deliberately disabled by this static
221222 * renderer until a host has negotiated the controlled bridge capability.
222223 */
223- export function sanitizeGenUIHtmlSource ( source : GenUIHtmlDocumentSource , specification ?: GenUIDesignSpec , hostStyleResourceCss = '' , scriptEnabled = false , inspectionEnabled = false , inlineScriptEnabled = false ) : GenUIHtmlSanitizeResult {
224+ export function sanitizeGenUIHtmlSource ( source : GenUIHtmlDocumentSource , specification ?: GenUIDesignSpec , hostStyleResourceCss = '' , scriptEnabled = false , inspectionEnabled = false , inlineScriptEnabled = false , colorScheme : 'light' | 'dark' = 'light' ) : GenUIHtmlSanitizeResult {
224225 const diagnostics : string [ ] = [ ] ;
225226 const allowControlledScript = scriptEnabled && ! inspectionEnabled ;
226227 const scriptNonce = allowControlledScript || inspectionEnabled ? createCspNonce ( ) : undefined ;
@@ -307,7 +308,7 @@ export function sanitizeGenUIHtmlSource(source: GenUIHtmlDocumentSource, specifi
307308 }
308309 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 } '` ) ;
309310 return {
310- document : `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">${ parsed . head . innerHTML } </head><body>${ parsed . body . innerHTML } </body></html>` ,
311+ document : `<!doctype html><html data-openbitfun-appearance-mode=" ${ colorScheme } " ><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">${ parsed . head . innerHTML } </head><body>${ parsed . body . innerHTML } </body></html>` ,
311312 diagnostics,
312313 } ;
313314}
@@ -346,8 +347,10 @@ export default function GenUIHtmlDocument({ source, specification, designSpecId,
346347 inspection ?: GenUIHtmlInspection ;
347348} ) {
348349 const frameRef = useRef < HTMLIFrameElement > ( null ) ;
350+ const themeRef = useRef < HTMLDivElement > ( null ) ;
351+ const colorScheme = useColorScheme ( themeRef ) ;
349352 const hostStyleResourceCss = useMemo ( ( ) => {
350- if ( ! specification ?. styleResources . length || typeof document === 'undefined' ) return '' ;
353+ if ( typeof document === 'undefined' ) return '' ;
351354 const computed = getComputedStyle ( document . documentElement ) ;
352355 const tokens : string [ ] = [ ] ;
353356 for ( let index = 0 ; index < computed . length ; index ++ ) {
@@ -358,14 +361,11 @@ export default function GenUIHtmlDocument({ source, specification, designSpecId,
358361 // into the opaque document's inline stylesheet.
359362 if ( value && ! / [ { } ; ] / . test ( value ) ) tokens . push ( `${ name } :${ value } ;` ) ;
360363 }
361- const base = tokens . length
362- ? `: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);}`
363- : '' ;
364- return base ;
365- } , [ specification ] ) ;
364+ 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);}` ;
365+ } , [ colorScheme ] ) ;
366366 const inspectionEnabled = Boolean ( inspection ) ;
367367 const canRunScript = Boolean ( scriptEnabled && bridge && source . script ?. trim ( ) && ! inspectionEnabled ) ;
368- const sanitized = useMemo ( ( ) => sanitizeGenUIHtmlSource ( source , specification , hostStyleResourceCss , canRunScript , inspectionEnabled , false ) , [ source , specification , hostStyleResourceCss , canRunScript , inspectionEnabled ] ) ;
368+ const sanitized = useMemo ( ( ) => sanitizeGenUIHtmlSource ( source , specification , hostStyleResourceCss , canRunScript , inspectionEnabled , false , colorScheme ) , [ source , specification , hostStyleResourceCss , canRunScript , inspectionEnabled , colorScheme ] ) ;
369369 useEffect ( ( ) => {
370370 if ( ! canRunScript || ! bridge ) return ;
371371 const frame = frameRef . current ;
@@ -485,7 +485,7 @@ export default function GenUIHtmlDocument({ source, specification, designSpecId,
485485 window . clearInterval ( retryTimer ) ;
486486 } ;
487487 } , [ inspection ] ) ;
488- return < div className = "gen-ui-html-document" data-gen-ui-html-document = "true" data-gen-ui-source-kind = { source . kind } data-gen-ui-sanitize-diagnostics = { sanitized . diagnostics . length } >
488+ return < div ref = { themeRef } className = "gen-ui-html-document" data-gen-ui-html-document = "true" data-gen-ui-source-kind = { source . kind } data-gen-ui-sanitize-diagnostics = { sanitized . diagnostics . length } >
489489 < iframe
490490 title = "Generated HTML document"
491491 className = "gen-ui-html-document__frame"
0 commit comments