@@ -21,6 +21,7 @@ const props = defineProps<{ show: boolean; ssr: boolean }>()
21
21
const store = inject (injectKeyStore )!
22
22
const clearConsole = inject <Ref <boolean >>(' clear-console' )!
23
23
const theme = inject <Ref <' dark' | ' light' >>(' theme' )!
24
+ const previewTheme = inject <Ref <boolean >>(' preview-theme' )!
24
25
25
26
const previewOptions = inject <Props [' previewOptions' ]>(' preview-options' )
26
27
@@ -49,15 +50,16 @@ watch(
49
50
)
50
51
51
52
// 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
+ })
61
63
62
64
onUnmounted (() => {
63
65
proxy .destroy ()
@@ -88,7 +90,10 @@ function createSandbox() {
88
90
89
91
const importMap = store .getImportMap ()
90
92
const sandboxSrc = srcdoc
91
- .replace (/ <html>/ , ` <html class="${theme .value }"> ` )
93
+ .replace (
94
+ / <html>/ ,
95
+ ` <html class="${previewTheme .value ? theme .value : ' ' }"> ` ,
96
+ )
92
97
.replace (/ <!--IMPORT_MAP-->/ , JSON .stringify (importMap ))
93
98
.replace (
94
99
/ <!-- PREVIEW-OPTIONS-HEAD-HTML -->/ ,
@@ -281,7 +286,12 @@ defineExpose({ reload })
281
286
</script >
282
287
283
288
<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
+ />
285
295
<Message :err =" runtimeError" />
286
296
<Message v-if =" !runtimeError" :warn =" runtimeWarning" />
287
297
</template >
0 commit comments