@@ -21,6 +21,7 @@ const props = defineProps<{ show: boolean; ssr: boolean }>()
2121const store = inject (injectKeyStore )!
2222const clearConsole = inject <Ref <boolean >>(' clear-console' )!
2323const theme = inject <Ref <' dark' | ' light' >>(' theme' )!
24+ const previewTheme = inject <Ref <boolean >>(' preview-theme' )!
2425
2526const previewOptions = inject <Props [' previewOptions' ]>(' preview-options' )
2627
@@ -49,15 +50,16 @@ watch(
4950)
5051
5152// reset theme
52- watch (
53- () => theme .value ,
54- (value ) => {
55- const html = sandbox .contentDocument ?.documentElement
56- if (html ) {
57- html .className = value
58- }
59- },
60- )
53+ watch ([theme , previewTheme ], ([theme , previewTheme ]) => {
54+ if (! previewTheme ) return
55+
56+ const html = sandbox .contentDocument ?.documentElement
57+ if (html ) {
58+ html .className = theme
59+ } else {
60+ createSandbox ()
61+ }
62+ })
6163
6264onUnmounted (() => {
6365 proxy .destroy ()
@@ -88,7 +90,10 @@ function createSandbox() {
8890
8991 const importMap = store .getImportMap ()
9092 const sandboxSrc = srcdoc
91- .replace (/ <html>/ , ` <html class="${theme .value }"> ` )
93+ .replace (
94+ / <html>/ ,
95+ ` <html class="${previewTheme .value ? theme .value : ' ' }"> ` ,
96+ )
9297 .replace (/ <!--IMPORT_MAP-->/ , JSON .stringify (importMap ))
9398 .replace (
9499 / <!-- PREVIEW-OPTIONS-HEAD-HTML -->/ ,
@@ -281,7 +286,12 @@ defineExpose({ reload })
281286 </script >
282287
283288<template >
284- <div v-show =" show" ref =" container" class =" iframe-container" :class =" theme" />
289+ <div
290+ v-show =" show"
291+ ref =" container"
292+ class =" iframe-container"
293+ :class =" { [theme]: previewTheme }"
294+ />
285295 <Message :err =" runtimeError" />
286296 <Message v-if =" !runtimeError" :warn =" runtimeWarning" />
287297</template >
0 commit comments