@scalar/use-hooks 0.4.10 → 0.4.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/useColorMode/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/useColorMode/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { type DarkLightMode } from '@scalar/helpers/theme/color-mode';
|
|
1
2
|
/** Possible color modes */
|
|
2
|
-
type ColorMode = 'light' | 'dark' | 'system';
|
|
3
|
-
|
|
4
|
-
type DarkLightMode = 'light' | 'dark';
|
|
3
|
+
export type ColorMode = 'light' | 'dark' | 'system';
|
|
4
|
+
export type { DarkLightMode };
|
|
5
5
|
/**
|
|
6
6
|
* A composable hook that provides color mode (dark/light) functionality.
|
|
7
7
|
*/
|
|
@@ -24,5 +24,4 @@ export declare function useColorMode(opts?: {
|
|
|
24
24
|
/** Gets the system mode preference. */
|
|
25
25
|
getSystemModePreference: () => DarkLightMode;
|
|
26
26
|
};
|
|
27
|
-
export {};
|
|
28
27
|
//# sourceMappingURL=useColorMode.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useColorMode.d.ts","sourceRoot":"","sources":["../../src/useColorMode/useColorMode.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useColorMode.d.ts","sourceRoot":"","sources":["../../src/useColorMode/useColorMode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAsC,MAAM,kCAAkC,CAAA;AAoBzG,2BAA2B;AAC3B,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,QAAQ,CAAA;AAEnD,YAAY,EAAE,aAAa,EAAE,CAAA;AAE7B;;GAEG;AACH,wBAAgB,YAAY,CAC1B,IAAI,GAAE;IACJ,oCAAoC;IACpC,gBAAgB,CAAC,EAAE,SAAS,CAAA;IAC5B,8BAA8B;IAC9B,iBAAiB,CAAC,EAAE,SAAS,CAAA;CACzB;IA0FJ,yCAAyC;;IAKzC,+CAA+C;;IAE/C,yDAAyD;;IAEzD,qDAAqD;;IAErD,kDAAkD;0BApFvB,SAAS;IAsFpC,uCAAuC;mCA7EL,aAAa;EAgFlD"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { applyColorMode, getSystemColorMode } from '@scalar/helpers/theme/color-mode';
|
|
1
2
|
import { literal, union, validate } from '@scalar/validation';
|
|
2
3
|
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
|
|
3
4
|
const colorMode = ref('dark');
|
|
@@ -38,13 +39,7 @@ export function useColorMode(opts = {}) {
|
|
|
38
39
|
}
|
|
39
40
|
/** Gets the system mode preference. */
|
|
40
41
|
function getSystemModePreference() {
|
|
41
|
-
|
|
42
|
-
return 'light';
|
|
43
|
-
}
|
|
44
|
-
if (typeof window?.matchMedia !== 'function') {
|
|
45
|
-
return 'dark';
|
|
46
|
-
}
|
|
47
|
-
return window?.matchMedia('(prefers-color-scheme: dark)')?.matches ? 'dark' : 'light';
|
|
42
|
+
return getSystemColorMode();
|
|
48
43
|
}
|
|
49
44
|
/** Writable computed ref for dark/light mode with system preference applied */
|
|
50
45
|
const darkLightMode = computed({
|
|
@@ -57,21 +52,15 @@ export function useColorMode(opts = {}) {
|
|
|
57
52
|
set: (value) => setColorMode(value ? 'dark' : 'light'),
|
|
58
53
|
});
|
|
59
54
|
/** Applies the appropriate color mode class to the body. */
|
|
60
|
-
function
|
|
55
|
+
function applyBodyColorMode() {
|
|
61
56
|
if (typeof document === 'undefined' || typeof window === 'undefined') {
|
|
62
57
|
return;
|
|
63
58
|
}
|
|
64
59
|
// Read from the deferred `systemPreference` ref (not `getSystemModePreference()` directly) so
|
|
65
60
|
// the body class agrees with `darkLightMode`/the toggle before `onMounted` resolves the real value.
|
|
66
61
|
const classMode = overrideColorMode ?? (colorMode.value === 'system' ? systemPreference.value : colorMode.value);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
document.body.classList.remove('light-mode');
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
document.body.classList.add('light-mode');
|
|
73
|
-
document.body.classList.remove('dark-mode');
|
|
74
|
-
}
|
|
62
|
+
// An `overrideColorMode` of `system` reaches here unresolved, and has always rendered as light.
|
|
63
|
+
applyColorMode(classMode === 'dark' ? 'dark' : 'light');
|
|
75
64
|
}
|
|
76
65
|
// Priority: overrideColorMode -> localStorage -> initialColorMode
|
|
77
66
|
// Without `window` (SSR/SSG), there is no storage — treat preference as `system` so we do not
|
|
@@ -81,7 +70,7 @@ export function useColorMode(opts = {}) {
|
|
|
81
70
|
colorMode.value = overrideColorMode ?? savedColorMode ?? initialColorMode;
|
|
82
71
|
// Watch for color mode or system preference changes and update the body class. Watching
|
|
83
72
|
// `systemPreference` means resolving it in `onMounted` (or an OS theme change) re-applies the class.
|
|
84
|
-
watch([colorMode, systemPreference],
|
|
73
|
+
watch([colorMode, systemPreference], applyBodyColorMode, { immediate: true });
|
|
85
74
|
const handleChange = () => {
|
|
86
75
|
systemPreference.value = getSystemModePreference();
|
|
87
76
|
};
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"url": "git+https://github.com/scalar/scalar.git",
|
|
11
11
|
"directory": "packages/use-hooks"
|
|
12
12
|
},
|
|
13
|
-
"version": "0.4.
|
|
13
|
+
"version": "0.4.11",
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=22"
|
|
16
16
|
},
|
|
@@ -46,8 +46,9 @@
|
|
|
46
46
|
"cva": "1.0.0-beta.4",
|
|
47
47
|
"tailwind-merge": "3.5.0",
|
|
48
48
|
"vue": "^3.5.40",
|
|
49
|
-
"@scalar/
|
|
50
|
-
"@scalar/validation": "0.6.3"
|
|
49
|
+
"@scalar/helpers": "0.11.3",
|
|
50
|
+
"@scalar/validation": "0.6.3",
|
|
51
|
+
"@scalar/use-toasts": "0.10.5"
|
|
51
52
|
},
|
|
52
53
|
"devDependencies": {
|
|
53
54
|
"@vue/test-utils": "2.4.11",
|