2
2
import ' @liting-yes/vue-repl/style.css'
3
3
import type { PreviewUpdateFlag , Store } from ' @liting-yes/vue-repl'
4
4
import { ExtendEditorContainer , Preview , ReplStore , defaultMainFile , importMapFile } from ' @liting-yes/vue-repl'
5
- import { computed , onMounted , provide , ref , toRef } from ' vue'
6
- import { useClipboard , useElementHover } from ' @vueuse/core'
5
+ import { computed , onMounted , provide , ref } from ' vue'
6
+ import { useClipboard , useElementHover , useMutationObserver } from ' @vueuse/core'
7
7
import Copy from ' ./icons/Copy.vue'
8
8
import Copied from ' ./icons/Copied.vue'
9
9
import UnfoldLess from ' ./icons/UnfoldLess.vue'
@@ -49,7 +49,6 @@ const props = withDefaults(defineProps<Props>(), {
49
49
useCode: ' ' ,
50
50
},
51
51
}),
52
- theme: ' light' ,
53
52
hideMessageToggle: true ,
54
53
hideMessage: false ,
55
54
})
@@ -85,10 +84,24 @@ if (props.importMap) {
85
84
86
85
store .setFiles (files )
87
86
87
+ const theme = ref <' dark' | ' light' >()
88
+ const themeComputed = computed (() => props .theme ?? theme .value )
88
89
onMounted (() => {
89
90
if (props .clearConsole )
90
91
// eslint-disable-next-line no-console
91
92
console .clear ()
93
+
94
+ const rootEl = document ?.querySelector (' html' )
95
+ if (rootEl ) {
96
+ useMutationObserver (rootEl , (mutations ) => {
97
+ mutations .forEach ((mutation ) => {
98
+ if (mutation .attributeName === ' class' )
99
+ theme .value = rootEl .classList ?.contains (' dark' ) ? ' dark' : ' light'
100
+ })
101
+ }, {
102
+ attributes: true ,
103
+ })
104
+ }
92
105
})
93
106
94
107
const previewUpdateFlag = ref <PreviewUpdateFlag >(' UPDATING' )
@@ -102,7 +115,7 @@ provide('store', store)
102
115
provide (' autoresize' , props .autoResize )
103
116
provide (' clear-console' , props .clearConsole )
104
117
provide (' preview-options' , props .previewOptions )
105
- provide (' theme' , toRef ( props , ' theme ' ) )
118
+ provide (' theme' , themeComputed )
106
119
107
120
const { copy, copied } = useClipboard ({ source: store .state .activeFile .code , legacy: true })
108
121
0 commit comments