forked from vitest-dev/vitest
/
index.ts
147 lines (127 loc) · 3.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { Ref } from 'vue-demi'
import { computed, ref, watch } from 'vue-demi'
import { tryOnMounted } from '@vueuse/shared'
import type { StorageLike } from '../ssr-handlers'
import { getSSRHandler } from '../ssr-handlers'
import type { StorageOptions } from '../useStorage'
import { useStorage } from '../useStorage'
import { defaultWindow } from '../_configurable'
import { usePreferredDark } from '../usePreferredDark'
export type BasicColorSchema = 'light' | 'dark' | 'auto'
export interface UseColorModeOptions<T extends string = BasicColorSchema> extends StorageOptions<T | BasicColorSchema> {
/**
* CSS Selector for the target element applying to
*
* @default 'html'
*/
selector?: string
/**
* HTML attribute applying the target element
*
* @default 'class'
*/
attribute?: string
/**
* Prefix when adding value to the attribute
*/
modes?: Partial<Record<T | BasicColorSchema, string>>
/**
* A custom handler for handle the updates.
* When specified, the default behavior will be overridded.
*
* @default undefined
*/
onChanged?: (mode: T | BasicColorSchema, defaultHandler:((mode: T | BasicColorSchema) => void)) => void
/**
* Custom storage ref
*
* When provided, `useStorage` will be skipped
*/
storageRef?: Ref<T | BasicColorSchema>
/**
* Key to persist the data into localStorage/sessionStorage.
*
* Pass `null` to disable persistence
*
* @default 'vueuse-color-scheme'
*/
storageKey?: string | null
/**
* Storage object, can be localStorage or sessionStorage
*
* @default localStorage
*/
storage?: StorageLike
}
/**
* Reactive color mode with auto data persistence.
*
* @see https://vueuse.org/useColorMode
* @param options
*/
export function useColorMode<T extends string = BasicColorSchema>(options: UseColorModeOptions<T> = {}) {
const {
selector = 'html',
attribute = 'class',
window = defaultWindow,
storage = getSSRHandler('getDefaultStorage', () => defaultWindow?.localStorage)(),
storageKey = 'vueuse-color-scheme',
listenToStorageChanges = true,
storageRef,
} = options
const modes = {
auto: '',
light: 'light',
dark: 'dark',
...options.modes || {},
} as Record<BasicColorSchema | T, string>
const preferredDark = usePreferredDark({ window })
const preferredMode = computed(() => preferredDark.value ? 'dark' : 'light')
const store = storageRef || (storageKey == null
? ref('auto') as Ref<T | BasicColorSchema>
: useStorage<T | BasicColorSchema>(storageKey, 'auto', storage, { window, listenToStorageChanges }))
const state = computed<T | BasicColorSchema>({
get() {
return store.value === 'auto'
? preferredMode.value
: store.value
},
set(v) {
store.value = v
},
})
const updateHTMLAttrs = getSSRHandler(
'updateHTMLAttrs',
(selector, attribute, value) => {
const el = window?.document.querySelector(selector)
if (!el)
return
if (attribute === 'class') {
const current = value.split(/\s/g)
Object.values(modes)
.flatMap(i => (i || '').split(/\s/g))
.filter(Boolean)
.forEach((v) => {
if (current.includes(v))
el.classList.add(v)
else
el.classList.remove(v)
})
}
else {
el.setAttribute(attribute, value)
}
})
function defaultOnChanged(mode: T | BasicColorSchema) {
updateHTMLAttrs(selector, attribute, modes[mode] ?? mode)
}
function onChanged(mode: T | BasicColorSchema) {
if (options.onChanged)
options.onChanged(mode, defaultOnChanged)
else
defaultOnChanged(mode)
}
watch(state, onChanged, { flush: 'post', immediate: true })
tryOnMounted(() => onChanged(state.value))
return state
}