Skip to content

Commit c830fc4

Browse files
committed
feat: add previewTheme prop
Whether to follow the theme in sandbox
1 parent 1786cbf commit c830fc4

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

src/Repl.vue

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import EditorContainer from './editor/EditorContainer.vue'
88
99
export interface Props {
1010
theme?: 'dark' | 'light'
11+
previewTheme?: boolean
1112
editor: EditorComponentType
1213
store?: Store
1314
autoResize?: boolean
@@ -31,6 +32,7 @@ export interface Props {
3132
3233
const props = withDefaults(defineProps<Props>(), {
3334
theme: 'light',
35+
previewTheme: false,
3436
store: () => useStore(),
3537
autoResize: true,
3638
showCompileOutput: true,
@@ -69,6 +71,7 @@ provide('tsconfig', toRef(props, 'showTsConfig'))
6971
provide('clear-console', toRef(props, 'clearConsole'))
7072
provide('preview-options', props.previewOptions)
7173
provide('theme', toRef(props, 'theme'))
74+
provide('preview-theme', toRef(props, 'previewTheme'))
7275
/**
7376
* Reload the preview iframe
7477
*/

src/output/Preview.vue

+21-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const props = defineProps<{ show: boolean; ssr: boolean }>()
2121
const store = inject(injectKeyStore)!
2222
const clearConsole = inject<Ref<boolean>>('clear-console')!
2323
const theme = inject<Ref<'dark' | 'light'>>('theme')!
24+
const previewTheme = inject<Ref<boolean>>('preview-theme')!
2425
2526
const 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
6264
onUnmounted(() => {
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>

src/output/srcdoc.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
<head>
44
<style>
55
html.dark {
6-
color: white;
7-
background: #1e1e1e;
6+
color-scheme: dark;
87
}
98
body {
109
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,

test/main.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ const App = {
4747
// }, 1000)
4848

4949
// store.vueVersion = '3.4.1'
50+
const theme = ref<'light' | 'dark'>('dark')
51+
window.theme = theme
52+
const previewTheme = ref(false)
53+
window.previewTheme = previewTheme
5054

5155
return () =>
5256
h(Repl, {
5357
store,
54-
theme: 'dark',
58+
theme: theme.value,
59+
previewTheme: previewTheme.value,
5560
editor: MonacoEditor,
5661
// layout: 'vertical',
5762
ssr: true,

0 commit comments

Comments
 (0)