-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
159 lines (138 loc) · 4.09 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
148
149
150
151
152
153
154
155
156
157
158
159
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
/**
* Emit `auto` mode from state
*
* When set to `true`, preferred mode won't be translated into `light` or `dark`.
* This is useful when the fact that `auto` mode was selected needs to be known.
*
* @default undefined
*/
emitAuto?: boolean
}
/**
* 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,
storageKey = 'vueuse-color-scheme',
listenToStorageChanges = true,
storageRef,
emitAuto,
} = 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' && !emitAuto
? 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) {
const resolvedMode = mode === 'auto' ? preferredMode.value : mode
updateHTMLAttrs(selector, attribute, modes[resolvedMode] ?? resolvedMode)
}
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
}