vueless 1.2.5-beta.5 → 1.2.5-beta.7
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/index.d.ts +2 -1
- package/index.ts +2 -1
- package/package.json +1 -1
- package/utils/helper.ts +32 -0
- package/utils/theme.ts +53 -78
package/index.d.ts
CHANGED
|
@@ -7,13 +7,14 @@ export {
|
|
|
7
7
|
isSSR,
|
|
8
8
|
isCSR,
|
|
9
9
|
setTitle,
|
|
10
|
+
getStored,
|
|
10
11
|
getRandomId,
|
|
11
12
|
createDebounce,
|
|
12
13
|
hasSlotContent
|
|
13
14
|
} from "./utils/helper";
|
|
14
15
|
export { isMac, isPWA, isIOS, isAndroid, isMobileApp, isWindows } from "./utils/platform";
|
|
15
16
|
export { cx, cva, compose, getDefaults, setVuelessConfig, setColor, vuelessConfig } from "./utils/ui";
|
|
16
|
-
export { getTheme, setTheme, resetTheme, normalizeThemeConfig,
|
|
17
|
+
export { getTheme, setTheme, resetTheme, normalizeThemeConfig, cssVar } from "./utils/theme";
|
|
17
18
|
export { getArgTypes, getSlotNames, getSlotsFragment, getSource, getDocsDescription } from "./utils/storybook";
|
|
18
19
|
/* adapters */
|
|
19
20
|
export { default as defaultEnLocale } from "./adapter.locale/locales/en";
|
package/index.ts
CHANGED
|
@@ -13,13 +13,14 @@ export {
|
|
|
13
13
|
isSSR,
|
|
14
14
|
isCSR,
|
|
15
15
|
setTitle,
|
|
16
|
+
getStored,
|
|
16
17
|
getRandomId,
|
|
17
18
|
createDebounce,
|
|
18
19
|
hasSlotContent
|
|
19
20
|
} from "./utils/helper";
|
|
20
21
|
export { isMac, isPWA, isIOS, isAndroid, isMobileApp, isWindows } from "./utils/platform";
|
|
21
22
|
export { cx, cva, compose, getDefaults, setVuelessConfig, setColor, vuelessConfig } from "./utils/ui";
|
|
22
|
-
export { getTheme, setTheme, resetTheme, normalizeThemeConfig,
|
|
23
|
+
export { getTheme, setTheme, resetTheme, normalizeThemeConfig, cssVar } from "./utils/theme";
|
|
23
24
|
export { getArgTypes, getSlotNames, getSlotsFragment, getSource, getDocsDescription } from "./utils/storybook";
|
|
24
25
|
/* adapters */
|
|
25
26
|
export { default as defaultEnLocale } from "./adapter.locale/locales/en";
|
package/package.json
CHANGED
package/utils/helper.ts
CHANGED
|
@@ -184,3 +184,35 @@ export function isEmptyValue(value: object | null | undefined | string | unknown
|
|
|
184
184
|
(typeof value === "object" && !Object.keys(value).length)
|
|
185
185
|
);
|
|
186
186
|
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Converts the given value to a number if possible.
|
|
190
|
+
*
|
|
191
|
+
* @param {unknown} value - The value to be converted to a number. Can be of any data type.
|
|
192
|
+
* @return {number | undefined} The numeric representation of the value if conversion is successful; otherwise, undefined.
|
|
193
|
+
*/
|
|
194
|
+
export function toNumber(value: unknown): number | undefined {
|
|
195
|
+
if (typeof value === "number") {
|
|
196
|
+
return value;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
200
|
+
const number = Number(value);
|
|
201
|
+
|
|
202
|
+
if (!Number.isNaN(number)) {
|
|
203
|
+
return number;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Get a stored value from local storage.
|
|
212
|
+
* @return string | undefined
|
|
213
|
+
*/
|
|
214
|
+
export function getStored(key: string) {
|
|
215
|
+
if (isSSR) return;
|
|
216
|
+
|
|
217
|
+
return localStorage.getItem(key) ?? undefined;
|
|
218
|
+
}
|
package/utils/theme.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { cloneDeep, merge } from "lodash-es";
|
|
2
2
|
|
|
3
3
|
import { vuelessConfig } from "./ui";
|
|
4
|
-
import { isCSR,
|
|
4
|
+
import { isCSR, getStored, getCookie, setCookie, deleteCookie, toNumber } from "./helper";
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
7
|
PX_IN_REM,
|
|
8
|
+
AUTO_MODE_KEY,
|
|
8
9
|
COLOR_MODE_KEY,
|
|
9
10
|
LIGHT_MODE_CLASS,
|
|
10
11
|
DARK_MODE_CLASS,
|
|
@@ -34,7 +35,6 @@ import {
|
|
|
34
35
|
DEFAULT_DISABLED_OPACITY,
|
|
35
36
|
LETTER_SPACING,
|
|
36
37
|
DEFAULT_LETTER_SPACING,
|
|
37
|
-
AUTO_MODE_KEY,
|
|
38
38
|
} from "../constants";
|
|
39
39
|
|
|
40
40
|
import type {
|
|
@@ -75,7 +75,7 @@ function toggleColorModeClass() {
|
|
|
75
75
|
const colorMode = prefersColorSchemeDark.matches ? ColorMode.Dark : ColorMode.Light;
|
|
76
76
|
|
|
77
77
|
setCookie(COLOR_MODE_KEY, colorMode);
|
|
78
|
-
|
|
78
|
+
setCookie(AUTO_MODE_KEY, String(Number(true)));
|
|
79
79
|
|
|
80
80
|
document.documentElement.classList.toggle(DARK_MODE_CLASS, prefersColorSchemeDark.matches);
|
|
81
81
|
document.documentElement.classList.toggle(LIGHT_MODE_CLASS, !prefersColorSchemeDark.matches);
|
|
@@ -92,8 +92,8 @@ function toggleColorModeClass() {
|
|
|
92
92
|
* - `isColorModeAuto` {boolean}: Indicates whether the color mode is set to auto.
|
|
93
93
|
*/
|
|
94
94
|
function setCSRColorMode(mode: `${ColorMode}`): SetColorMode {
|
|
95
|
-
const colorMode = mode ||
|
|
96
|
-
const isCachedAutoMode = !!Number(
|
|
95
|
+
const colorMode = mode || getCookie(COLOR_MODE_KEY) || vuelessConfig.colorMode || ColorMode.Light;
|
|
96
|
+
const isCachedAutoMode = !!Number(getCookie(AUTO_MODE_KEY) ?? 0);
|
|
97
97
|
|
|
98
98
|
const isAutoMode = colorMode === ColorMode.Auto;
|
|
99
99
|
const isSystemDarkMode = isAutoMode && prefersColorSchemeDark && prefersColorSchemeDark?.matches;
|
|
@@ -123,12 +123,14 @@ function setCSRColorMode(mode: `${ColorMode}`): SetColorMode {
|
|
|
123
123
|
currentColorMode = isDarkMode ? ColorMode.Dark : ColorMode.Light;
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
/* Define color mode cookies to be used in both CSR and SSR */
|
|
127
|
+
if (mode || getCookie(AUTO_MODE_KEY) === undefined) {
|
|
127
128
|
setCookie(COLOR_MODE_KEY, currentColorMode);
|
|
128
129
|
setCookie(AUTO_MODE_KEY, String(Number(isAutoMode)));
|
|
129
130
|
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
if (mode !== ColorMode.Auto && prefersColorSchemeDark) {
|
|
132
|
+
prefersColorSchemeDark.removeEventListener("change", toggleColorModeClass);
|
|
133
|
+
}
|
|
132
134
|
}
|
|
133
135
|
|
|
134
136
|
return {
|
|
@@ -164,16 +166,6 @@ export function cssVar(name: string) {
|
|
|
164
166
|
return (isCSR && getComputedStyle(document.documentElement).getPropertyValue(name)) || undefined;
|
|
165
167
|
}
|
|
166
168
|
|
|
167
|
-
/**
|
|
168
|
-
* Get a stored value from local storage.
|
|
169
|
-
* @return string | undefined
|
|
170
|
-
*/
|
|
171
|
-
export function getStored(key: string) {
|
|
172
|
-
if (isSSR) return;
|
|
173
|
-
|
|
174
|
-
return localStorage.getItem(key) ?? undefined;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
169
|
/**
|
|
178
170
|
* Resets all theme data by clearing cookies and localStorage.
|
|
179
171
|
* This removes all stored theme preferences including color mode, colors, text sizes,
|
|
@@ -207,40 +199,6 @@ export function resetTheme() {
|
|
|
207
199
|
});
|
|
208
200
|
}
|
|
209
201
|
|
|
210
|
-
/**
|
|
211
|
-
* Normalizes the provided theme configuration object into a structured format.
|
|
212
|
-
*
|
|
213
|
-
* @param {object} theme - The theme configuration object to normalize.
|
|
214
|
-
* @return {MergedThemeConfig}
|
|
215
|
-
*/
|
|
216
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
217
|
-
export function normalizeThemeConfig(theme: any): MergedThemeConfig {
|
|
218
|
-
return {
|
|
219
|
-
colorMode: theme.colorMode,
|
|
220
|
-
isColorModeAuto: theme.isColorModeAuto,
|
|
221
|
-
primary: theme.primary,
|
|
222
|
-
neutral: theme.neutral,
|
|
223
|
-
text: {
|
|
224
|
-
xs: toNumber(theme.text?.xs),
|
|
225
|
-
sm: toNumber(theme.text?.sm),
|
|
226
|
-
md: toNumber(theme.text?.md),
|
|
227
|
-
lg: toNumber(theme.text?.lg),
|
|
228
|
-
},
|
|
229
|
-
outline: {
|
|
230
|
-
sm: toNumber(theme.outline?.sm),
|
|
231
|
-
md: toNumber(theme.outline?.md),
|
|
232
|
-
lg: toNumber(theme.outline?.lg),
|
|
233
|
-
},
|
|
234
|
-
rounding: {
|
|
235
|
-
sm: toNumber(theme.rounding?.sm),
|
|
236
|
-
md: toNumber(theme.rounding?.md),
|
|
237
|
-
lg: toNumber(theme.rounding?.lg),
|
|
238
|
-
},
|
|
239
|
-
letterSpacing: toNumber(theme.letterSpacing),
|
|
240
|
-
disabledOpacity: toNumber(theme.disabledOpacity),
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
202
|
/**
|
|
245
203
|
* Retrieves the current theme configuration.
|
|
246
204
|
* @return ThemeConfig - current theme configuration
|
|
@@ -363,6 +321,45 @@ export function setTheme(config: ThemeConfig = {}) {
|
|
|
363
321
|
});
|
|
364
322
|
}
|
|
365
323
|
|
|
324
|
+
/**
|
|
325
|
+
* Normalizes the provided theme configuration object into a structured format.
|
|
326
|
+
*
|
|
327
|
+
* @param {object} theme - The theme configuration object to normalize.
|
|
328
|
+
* @return {MergedThemeConfig}
|
|
329
|
+
*/
|
|
330
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
331
|
+
export function normalizeThemeConfig(theme: any): MergedThemeConfig {
|
|
332
|
+
return {
|
|
333
|
+
colorMode: theme.colorMode,
|
|
334
|
+
isColorModeAuto: theme.isColorModeAuto,
|
|
335
|
+
primary: theme.primary,
|
|
336
|
+
neutral: theme.neutral,
|
|
337
|
+
text: {
|
|
338
|
+
xs: toNumber(theme.text?.xs),
|
|
339
|
+
sm: toNumber(theme.text?.sm),
|
|
340
|
+
md: toNumber(theme.text?.md),
|
|
341
|
+
lg: toNumber(theme.text?.lg),
|
|
342
|
+
},
|
|
343
|
+
outline: {
|
|
344
|
+
sm: toNumber(theme.outline?.sm),
|
|
345
|
+
md: toNumber(theme.outline?.md),
|
|
346
|
+
lg: toNumber(theme.outline?.lg),
|
|
347
|
+
},
|
|
348
|
+
rounding: {
|
|
349
|
+
sm: toNumber(theme.rounding?.sm),
|
|
350
|
+
md: toNumber(theme.rounding?.md),
|
|
351
|
+
lg: toNumber(theme.rounding?.lg),
|
|
352
|
+
},
|
|
353
|
+
letterSpacing: toNumber(theme.letterSpacing),
|
|
354
|
+
disabledOpacity: toNumber(theme.disabledOpacity),
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Determines if the provided color mode configuration has a primary color
|
|
360
|
+
* that differs from the default color mode configuration.
|
|
361
|
+
* @return {boolean}
|
|
362
|
+
*/
|
|
366
363
|
function hasPrimaryColor(
|
|
367
364
|
colorModeConfig: Partial<VuelessCssVariables> | undefined,
|
|
368
365
|
defaultColorModeConfig: Partial<VuelessCssVariables>,
|
|
@@ -639,14 +636,6 @@ function getDisabledOpacity(disabledOpacity?: ThemeConfig["disabledOpacity"]) {
|
|
|
639
636
|
return mergedOpacity;
|
|
640
637
|
}
|
|
641
638
|
|
|
642
|
-
/**
|
|
643
|
-
* Converts a primitive value into an object with the primitive value assigned to a key "md".
|
|
644
|
-
* If the provided value is already an object, it returns a deeply cloned copy of that object.
|
|
645
|
-
*/
|
|
646
|
-
function primitiveToObject(value: unknown): object {
|
|
647
|
-
return typeof value === "object" ? cloneDeep(value as object) : { md: value };
|
|
648
|
-
}
|
|
649
|
-
|
|
650
639
|
/**
|
|
651
640
|
* Generate and apply Vueless CSS variables.
|
|
652
641
|
* @return string - Vueless CSS variables string.
|
|
@@ -745,23 +734,9 @@ function setCSSVariables(
|
|
|
745
734
|
}
|
|
746
735
|
|
|
747
736
|
/**
|
|
748
|
-
* Converts the
|
|
749
|
-
*
|
|
750
|
-
* @param {unknown} value - The value to be converted to a number. Can be of any data type.
|
|
751
|
-
* @return {number | undefined} The numeric representation of the value if conversion is successful; otherwise, undefined.
|
|
737
|
+
* Converts a primitive value into an object with the primitive value assigned to a key "md".
|
|
738
|
+
* If the provided value is already an object, it returns a deeply cloned copy of that object.
|
|
752
739
|
*/
|
|
753
|
-
function
|
|
754
|
-
|
|
755
|
-
return value;
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
if (typeof value === "string" && value.trim() !== "") {
|
|
759
|
-
const number = Number(value);
|
|
760
|
-
|
|
761
|
-
if (!Number.isNaN(number)) {
|
|
762
|
-
return number;
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
return;
|
|
740
|
+
function primitiveToObject(value: unknown): object {
|
|
741
|
+
return typeof value === "object" ? cloneDeep(value as object) : { md: value };
|
|
767
742
|
}
|