Skip to content

Commit 686884a

Browse files
Teaghyhguanantfu
authoredDec 4, 2023
fix(useDark): In Vue 2.6 mode.system is undefined (#3562)
Co-authored-by: hguan <hguan@pisx.com> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 07d3985 commit 686884a

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed
 

‎packages/core/useDark/index.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { computed } from 'vue-demi'
2+
import { defaultWindow } from '../_configurable'
3+
import { usePreferredDark } from '../usePreferredDark'
24
import { useColorMode } from '../useColorMode'
35
import type { BasicColorSchema, UseColorModeOptions } from '../useColorMode'
46

@@ -36,6 +38,7 @@ export function useDark(options: UseDarkOptions = {}) {
3638
const {
3739
valueDark = 'dark',
3840
valueLight = '',
41+
window = defaultWindow,
3942
} = options
4043

4144
const mode = useColorMode({
@@ -52,13 +55,24 @@ export function useDark(options: UseDarkOptions = {}) {
5255
},
5356
})
5457

58+
const system = computed(() => {
59+
if (mode.system) {
60+
return mode.system.value
61+
}
62+
else {
63+
// In Vue 2.6, ref not be extensible, mode.system is undefined
64+
const preferredDark = usePreferredDark({ window })
65+
return preferredDark.value ? 'dark' : 'light'
66+
}
67+
})
68+
5569
const isDark = computed<boolean>({
5670
get() {
5771
return mode.value === 'dark'
5872
},
5973
set(v) {
6074
const modeVal = v ? 'dark' : 'light'
61-
if (mode.system.value === modeVal)
75+
if (system.value === modeVal)
6276
mode.value = 'auto'
6377
else
6478
mode.value = modeVal

0 commit comments

Comments
 (0)
Please sign in to comment.