@just-web/css 0.4.0 → 0.5.0
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/cjs/theme/data-attribute.d.ts +18 -2
- package/cjs/theme/data-attribute.js +6 -3
- package/cjs/utils/attribute.d.ts +1 -1
- package/cjs/utils/data-attribute.d.ts +1 -1
- package/esm/theme/data-attribute.d.ts +18 -2
- package/esm/theme/data-attribute.d.ts.map +1 -1
- package/esm/theme/data-attribute.js +6 -57
- package/esm/theme/data-attribute.js.map +1 -1
- package/esm/utils/attribute.d.ts +1 -1
- package/esm/utils/attribute.d.ts.map +1 -1
- package/esm/utils/attribute.js.map +1 -1
- package/esm/utils/data-attribute.d.ts +1 -1
- package/esm/utils/data-attribute.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/theme/data-attribute.ts +39 -5
- package/src/utils/attribute.ts +1 -1
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @param options.themes - Record mapping theme keys to their data attribute values
|
|
6
6
|
* @param options.defaultTheme - Fallback theme key if attribute value doesn't match any theme
|
|
7
7
|
* @param options.attributeName - Name of the data attribute to check (must start with 'data-')
|
|
8
|
+
* @param options.allowCustom - Whether to allow custom themes value
|
|
8
9
|
* @returns The matching theme key, or defaultTheme if no match found
|
|
9
10
|
*
|
|
10
11
|
* @example
|
|
@@ -24,11 +25,18 @@
|
|
|
24
25
|
* ```
|
|
25
26
|
*/
|
|
26
27
|
export declare function getThemeByDataAttribute<Themes extends Record<string, string>>(options: {
|
|
27
|
-
themes: Themes;
|
|
28
|
-
defaultTheme?: keyof Themes | undefined;
|
|
29
28
|
attributeName: `data-${string}`;
|
|
29
|
+
defaultTheme?: keyof Themes | undefined;
|
|
30
|
+
themes: Themes;
|
|
30
31
|
element?: Element | undefined;
|
|
31
32
|
}): keyof Themes | undefined;
|
|
33
|
+
export declare function getThemeByDataAttribute<Themes extends Record<string, string>>(options: {
|
|
34
|
+
attributeName: `data-${string}`;
|
|
35
|
+
allowCustom: true | undefined;
|
|
36
|
+
defaultTheme?: keyof Themes | undefined;
|
|
37
|
+
themes: Themes;
|
|
38
|
+
element?: Element | undefined;
|
|
39
|
+
}): string | undefined;
|
|
32
40
|
/**
|
|
33
41
|
* Observes changes to a theme data attribute and calls a handler when it changes.
|
|
34
42
|
*
|
|
@@ -65,3 +73,11 @@ export declare function observeThemeByDataAttributes<Themes extends Record<strin
|
|
|
65
73
|
defaultTheme?: keyof Themes | undefined;
|
|
66
74
|
element?: Element | undefined;
|
|
67
75
|
}): MutationObserver;
|
|
76
|
+
export declare function observeThemeByDataAttributes<Themes extends Record<string, string>>(options: {
|
|
77
|
+
attributeName: `data-${string}`;
|
|
78
|
+
themes: Themes;
|
|
79
|
+
handler: (value: string | null) => void;
|
|
80
|
+
allowCustom: true | undefined;
|
|
81
|
+
defaultTheme?: keyof Themes | undefined;
|
|
82
|
+
element?: Element | undefined;
|
|
83
|
+
}): MutationObserver;
|
|
@@ -8,9 +8,9 @@ exports.observeThemeByDataAttributes = observeThemeByDataAttributes;
|
|
|
8
8
|
var _typePlus = require("type-plus");
|
|
9
9
|
var _dataAttribute = require("../utils/data-attribute.ts");
|
|
10
10
|
function getThemeByDataAttribute(options) {
|
|
11
|
-
const value = (0, _dataAttribute.getDataAttribute)(options.attributeName, options.element);
|
|
11
|
+
const value = (0, _dataAttribute.getDataAttribute)(options.attributeName, options.element) ?? void 0;
|
|
12
12
|
const theme = (0, _typePlus.findKey)(options.themes, theme2 => options.themes[theme2] === value);
|
|
13
|
-
return theme ?? options.defaultTheme;
|
|
13
|
+
return theme ?? options.defaultTheme ?? (options.allowCustom ? value : void 0);
|
|
14
14
|
}
|
|
15
15
|
function observeThemeByDataAttributes(options) {
|
|
16
16
|
return (0, _dataAttribute.observeDataAttributes)({
|
|
@@ -22,9 +22,12 @@ function observeThemeByDataAttributes(options) {
|
|
|
22
22
|
for (const name in options.themes) {
|
|
23
23
|
if (options.themes[name] === value) {
|
|
24
24
|
options.handler(name);
|
|
25
|
-
|
|
25
|
+
return;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
+
if (options.allowCustom) {
|
|
29
|
+
options.handler(value);
|
|
30
|
+
}
|
|
28
31
|
}
|
|
29
32
|
}, options.element);
|
|
30
33
|
}
|
package/cjs/utils/attribute.d.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* const testId = getAttribute('data-testid', element)
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
export declare function getAttribute<T extends string>(qualifiedName: T, element?: Element | undefined):
|
|
17
|
+
export declare function getAttribute<T extends string>(qualifiedName: T, element?: Element | undefined): string | null;
|
|
18
18
|
/**
|
|
19
19
|
* Observes attributes changes on an element and calls corresponding handlers.
|
|
20
20
|
*
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare function getDataAttribute<T extends `data-${string}`>(qualifiedName: T, element?: Element | undefined):
|
|
1
|
+
export declare function getDataAttribute<T extends `data-${string}`>(qualifiedName: T, element?: Element | undefined): string | null;
|
|
2
2
|
/**
|
|
3
3
|
* Observes changes to `data-*` attributes on an element and calls corresponding handlers.
|
|
4
4
|
*
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @param options.themes - Record mapping theme keys to their data attribute values
|
|
6
6
|
* @param options.defaultTheme - Fallback theme key if attribute value doesn't match any theme
|
|
7
7
|
* @param options.attributeName - Name of the data attribute to check (must start with 'data-')
|
|
8
|
+
* @param options.allowCustom - Whether to allow custom themes value
|
|
8
9
|
* @returns The matching theme key, or defaultTheme if no match found
|
|
9
10
|
*
|
|
10
11
|
* @example
|
|
@@ -24,11 +25,18 @@
|
|
|
24
25
|
* ```
|
|
25
26
|
*/
|
|
26
27
|
export declare function getThemeByDataAttribute<Themes extends Record<string, string>>(options: {
|
|
27
|
-
themes: Themes;
|
|
28
|
-
defaultTheme?: keyof Themes | undefined;
|
|
29
28
|
attributeName: `data-${string}`;
|
|
29
|
+
defaultTheme?: keyof Themes | undefined;
|
|
30
|
+
themes: Themes;
|
|
30
31
|
element?: Element | undefined;
|
|
31
32
|
}): keyof Themes | undefined;
|
|
33
|
+
export declare function getThemeByDataAttribute<Themes extends Record<string, string>>(options: {
|
|
34
|
+
attributeName: `data-${string}`;
|
|
35
|
+
allowCustom: true | undefined;
|
|
36
|
+
defaultTheme?: keyof Themes | undefined;
|
|
37
|
+
themes: Themes;
|
|
38
|
+
element?: Element | undefined;
|
|
39
|
+
}): string | undefined;
|
|
32
40
|
/**
|
|
33
41
|
* Observes changes to a theme data attribute and calls a handler when it changes.
|
|
34
42
|
*
|
|
@@ -65,4 +73,12 @@ export declare function observeThemeByDataAttributes<Themes extends Record<strin
|
|
|
65
73
|
defaultTheme?: keyof Themes | undefined;
|
|
66
74
|
element?: Element | undefined;
|
|
67
75
|
}): MutationObserver;
|
|
76
|
+
export declare function observeThemeByDataAttributes<Themes extends Record<string, string>>(options: {
|
|
77
|
+
attributeName: `data-${string}`;
|
|
78
|
+
themes: Themes;
|
|
79
|
+
handler: (value: string | null) => void;
|
|
80
|
+
allowCustom: true | undefined;
|
|
81
|
+
defaultTheme?: keyof Themes | undefined;
|
|
82
|
+
element?: Element | undefined;
|
|
83
|
+
}): MutationObserver;
|
|
68
84
|
//# sourceMappingURL=data-attribute.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-attribute.d.ts","sourceRoot":"","sources":["../../src/theme/data-attribute.ts"],"names":[],"mappings":"AAGA
|
|
1
|
+
{"version":3,"file":"data-attribute.d.ts","sourceRoot":"","sources":["../../src/theme/data-attribute.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;IACvF,aAAa,EAAE,QAAQ,MAAM,EAAE,CAAA;IAC/B,YAAY,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CA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ACvC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAC7B,GAAG,gBAAgB,CAAA"}
|
|
@@ -1,64 +1,10 @@
|
|
|
1
1
|
import { findKey } from 'type-plus';
|
|
2
2
|
import { getDataAttribute, observeDataAttributes } from "../utils/data-attribute.js";
|
|
3
|
-
/**
|
|
4
|
-
* Gets the theme based on a data attribute value.
|
|
5
|
-
*
|
|
6
|
-
* @param options - Configuration options
|
|
7
|
-
* @param options.themes - Record mapping theme keys to their data attribute values
|
|
8
|
-
* @param options.defaultTheme - Fallback theme key if attribute value doesn't match any theme
|
|
9
|
-
* @param options.attributeName - Name of the data attribute to check (must start with 'data-')
|
|
10
|
-
* @returns The matching theme key, or defaultTheme if no match found
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const themes = {
|
|
15
|
-
* light: 'light',
|
|
16
|
-
* dark: 'dark',
|
|
17
|
-
* system: 'system'
|
|
18
|
-
* }
|
|
19
|
-
*
|
|
20
|
-
* // Get theme from data-theme attribute
|
|
21
|
-
* const theme = getThemeByDataAttribute({
|
|
22
|
-
* themes,
|
|
23
|
-
* defaultTheme: 'system',
|
|
24
|
-
* attributeName: 'data-theme'
|
|
25
|
-
* })
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
3
|
export function getThemeByDataAttribute(options) {
|
|
29
|
-
const value = getDataAttribute(options.attributeName, options.element);
|
|
4
|
+
const value = getDataAttribute(options.attributeName, options.element) ?? undefined;
|
|
30
5
|
const theme = findKey(options.themes, (theme) => options.themes[theme] === value);
|
|
31
|
-
return theme ?? options.defaultTheme;
|
|
6
|
+
return theme ?? options.defaultTheme ?? (options.allowCustom ? value : undefined);
|
|
32
7
|
}
|
|
33
|
-
/**
|
|
34
|
-
* Observes changes to a theme data attribute and calls a handler when it changes.
|
|
35
|
-
*
|
|
36
|
-
* @param options - Configuration options
|
|
37
|
-
* @param options.themes - Record mapping theme keys to their data attribute values
|
|
38
|
-
* @param options.handler - Callback function called with the new theme value or null when removed
|
|
39
|
-
* @param options.defaultTheme - Fallback theme key if attribute value doesn't match any theme
|
|
40
|
-
* @param options.attributeName - Name of the data attribute to observe (must start with 'data-')
|
|
41
|
-
* @returns A MutationObserver that can be disconnected to stop observing
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```ts
|
|
45
|
-
* const themes = {
|
|
46
|
-
* light: 'light',
|
|
47
|
-
* dark: 'dark'
|
|
48
|
-
* }
|
|
49
|
-
*
|
|
50
|
-
* // Observe data-theme attribute changes
|
|
51
|
-
* const observer = observeThemeByDataAttributes({
|
|
52
|
-
* themes,
|
|
53
|
-
* handler: (theme) => console.log('Theme changed to:', theme),
|
|
54
|
-
* defaultTheme: 'light',
|
|
55
|
-
* attributeName: 'data-theme'
|
|
56
|
-
* })
|
|
57
|
-
*
|
|
58
|
-
* // Stop observing
|
|
59
|
-
* observer.disconnect()
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
8
|
export function observeThemeByDataAttributes(options) {
|
|
63
9
|
return observeDataAttributes({
|
|
64
10
|
[options.attributeName]: (value) => {
|
|
@@ -69,9 +15,12 @@ export function observeThemeByDataAttributes(options) {
|
|
|
69
15
|
for (const name in options.themes) {
|
|
70
16
|
if (options.themes[name] === value) {
|
|
71
17
|
options.handler(name);
|
|
72
|
-
|
|
18
|
+
return;
|
|
73
19
|
}
|
|
74
20
|
}
|
|
21
|
+
if (options.allowCustom) {
|
|
22
|
+
options.handler(value);
|
|
23
|
+
}
|
|
75
24
|
},
|
|
76
25
|
}, options.element);
|
|
77
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-attribute.js","sourceRoot":"","sources":["../../src/theme/data-attribute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA;
|
|
1
|
+
{"version":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ACD,EACD,OAAO,CAAC,OAAO,CACf,CAAA;AACF,CAAC"}
|
package/esm/utils/attribute.d.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* const testId = getAttribute('data-testid', element)
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
export declare function getAttribute<T extends string>(qualifiedName: T, element?: Element | undefined):
|
|
17
|
+
export declare function getAttribute<T extends string>(qualifiedName: T, element?: Element | undefined): string | null;
|
|
18
18
|
/**
|
|
19
19
|
* Observes attributes changes on an element and calls corresponding handlers.
|
|
20
20
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"attribute.d.ts","sourceRoot":"","sources":["../../src/utils/attribute.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,EAC5C,aAAa,EAAE,CAAC,EAChB,OAAO,GAAE,OAAO,GAAG,SAAoC,
|
|
1
|
+
{"version":3,"file":"attribute.d.ts","sourceRoot":"","sources":["../../src/utils/attribute.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,EAC5C,aAAa,EAAE,CAAC,EAChB,OAAO,GAAE,OAAO,GAAG,SAAoC,iBAGvD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EACjD,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,KAAK,IAAI,CAAC,EACnD,OAAO,GAAE,OAAO,GAAG,SAAoC,oBAcvD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"attribute.js","sourceRoot":"","sources":["../../src/utils/attribute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AAEvC;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,YAAY,CAC3B,aAAgB,EAChB,UAA+B,GAAG,CAAC,kBAAkB,EAAE;IAEvD,OAAO,OAAO,EAAE,YAAY,CAAC,aAAa,
|
|
1
|
+
{"version":3,"file":"attribute.js","sourceRoot":"","sources":["../../src/utils/attribute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AAEvC;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,YAAY,CAC3B,aAAgB,EAChB,UAA+B,GAAG,CAAC,kBAAkB,EAAE;IAEvD,OAAO,OAAO,EAAE,YAAY,CAAC,aAAa,CAAC,CAAA;AAC5C,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,iBAAiB,CAChC,QAAmD,EACnD,UAA+B,GAAG,CAAC,kBAAkB,EAAE;IAEvD,MAAM,QAAQ,GAAG,IAAI,gBAAgB,CAAC,CAAC,SAAS,EAAE,EAAE;QACnD,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YAClC,MAAM,SAAS,GAAG,QAAQ,CAAC,aAAc,CAAA;YACzC,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,SAAS,CAAa,CAAA;YACzD,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,KAAK,CAAC,CAAA;QAC7B,CAAC;IACF,CAAC,CAAC,CAAA;IACF,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE;QACzB,UAAU,EAAE,IAAI;QAChB,eAAe,EAAE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;KACtC,CAAC,CAAA;IACF,OAAO,QAAQ,CAAA;AAChB,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare function getDataAttribute<T extends `data-${string}`>(qualifiedName: T, element?: Element | undefined):
|
|
1
|
+
export declare function getDataAttribute<T extends `data-${string}`>(qualifiedName: T, element?: Element | undefined): string | null;
|
|
2
2
|
/**
|
|
3
3
|
* Observes changes to `data-*` attributes on an element and calls corresponding handlers.
|
|
4
4
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-attribute.d.ts","sourceRoot":"","sources":["../../src/utils/data-attribute.ts"],"names":[],"mappings":"AAGA,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,QAAQ,MAAM,EAAE,EAC1D,aAAa,EAAE,CAAC,EAChB,OAAO,GAAE,OAAO,GAAG,SAAoC,
|
|
1
|
+
{"version":3,"file":"data-attribute.d.ts","sourceRoot":"","sources":["../../src/utils/data-attribute.ts"],"names":[],"mappings":"AAGA,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,QAAQ,MAAM,EAAE,EAC1D,aAAa,EAAE,CAAC,EAChB,OAAO,GAAE,OAAO,GAAG,SAAoC,iBAGvD;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,QAAQ,MAAM,EAAE,EACjF,QAAQ,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,KAAK,IAAI,CAAC,EAC9C,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,oBAG7B"}
|
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@ import { getDataAttribute, observeDataAttributes } from '../utils/data-attribute
|
|
|
8
8
|
* @param options.themes - Record mapping theme keys to their data attribute values
|
|
9
9
|
* @param options.defaultTheme - Fallback theme key if attribute value doesn't match any theme
|
|
10
10
|
* @param options.attributeName - Name of the data attribute to check (must start with 'data-')
|
|
11
|
+
* @param options.allowCustom - Whether to allow custom themes value
|
|
11
12
|
* @returns The matching theme key, or defaultTheme if no match found
|
|
12
13
|
*
|
|
13
14
|
* @example
|
|
@@ -27,16 +28,29 @@ import { getDataAttribute, observeDataAttributes } from '../utils/data-attribute
|
|
|
27
28
|
* ```
|
|
28
29
|
*/
|
|
29
30
|
export function getThemeByDataAttribute<Themes extends Record<string, string>>(options: {
|
|
31
|
+
attributeName: `data-${string}`
|
|
32
|
+
defaultTheme?: keyof Themes | undefined
|
|
30
33
|
themes: Themes
|
|
34
|
+
element?: Element | undefined
|
|
35
|
+
}): keyof Themes | undefined
|
|
36
|
+
export function getThemeByDataAttribute<Themes extends Record<string, string>>(options: {
|
|
37
|
+
attributeName: `data-${string}`
|
|
38
|
+
allowCustom: true | undefined
|
|
31
39
|
defaultTheme?: keyof Themes | undefined
|
|
40
|
+
themes: Themes
|
|
41
|
+
element?: Element | undefined
|
|
42
|
+
}): string | undefined
|
|
43
|
+
export function getThemeByDataAttribute<Themes extends Record<string, string>>(options: {
|
|
32
44
|
attributeName: `data-${string}`
|
|
45
|
+
allowCustom?: boolean | undefined
|
|
46
|
+
defaultTheme?: keyof Themes | undefined
|
|
47
|
+
themes: Themes
|
|
33
48
|
element?: Element | undefined
|
|
34
49
|
}): keyof Themes | undefined {
|
|
35
|
-
const value = getDataAttribute(options.attributeName, options.element)
|
|
36
|
-
|
|
50
|
+
const value = getDataAttribute(options.attributeName, options.element) ?? undefined
|
|
37
51
|
const theme = findKey(options.themes, (theme) => options.themes[theme] === value)
|
|
38
52
|
|
|
39
|
-
return theme ?? options.defaultTheme
|
|
53
|
+
return theme ?? options.defaultTheme ?? (options.allowCustom ? value : undefined)
|
|
40
54
|
}
|
|
41
55
|
|
|
42
56
|
/**
|
|
@@ -74,7 +88,23 @@ export function observeThemeByDataAttributes<Themes extends Record<string, strin
|
|
|
74
88
|
handler: (value: string | null) => void
|
|
75
89
|
defaultTheme?: keyof Themes | undefined
|
|
76
90
|
element?: Element | undefined
|
|
77
|
-
})
|
|
91
|
+
}): MutationObserver
|
|
92
|
+
export function observeThemeByDataAttributes<Themes extends Record<string, string>>(options: {
|
|
93
|
+
attributeName: `data-${string}`
|
|
94
|
+
themes: Themes
|
|
95
|
+
handler: (value: string | null) => void
|
|
96
|
+
allowCustom: true | undefined
|
|
97
|
+
defaultTheme?: keyof Themes | undefined
|
|
98
|
+
element?: Element | undefined
|
|
99
|
+
}): MutationObserver
|
|
100
|
+
export function observeThemeByDataAttributes<Themes extends Record<string, string>>(options: {
|
|
101
|
+
attributeName: `data-${string}`
|
|
102
|
+
themes: Themes
|
|
103
|
+
handler: (value: string | null) => void
|
|
104
|
+
allowCustom?: boolean | undefined
|
|
105
|
+
defaultTheme?: keyof Themes | undefined
|
|
106
|
+
element?: Element | undefined
|
|
107
|
+
}): MutationObserver {
|
|
78
108
|
return observeDataAttributes(
|
|
79
109
|
{
|
|
80
110
|
[options.attributeName]: (value: string | null) => {
|
|
@@ -86,9 +116,13 @@ export function observeThemeByDataAttributes<Themes extends Record<string, strin
|
|
|
86
116
|
for (const name in options.themes) {
|
|
87
117
|
if (options.themes[name] === value) {
|
|
88
118
|
options.handler(name)
|
|
89
|
-
|
|
119
|
+
return
|
|
90
120
|
}
|
|
91
121
|
}
|
|
122
|
+
|
|
123
|
+
if (options.allowCustom) {
|
|
124
|
+
options.handler(value)
|
|
125
|
+
}
|
|
92
126
|
},
|
|
93
127
|
},
|
|
94
128
|
options.element,
|
package/src/utils/attribute.ts
CHANGED