@just-web/css 0.3.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.
@@ -0,0 +1,19 @@
1
+ import type { CSSProperties } from './css-properties.ts';
2
+ /**
3
+ * Converts React-style CSS properties to DOM style properties.
4
+ * This function handles both standard CSS properties and custom properties,
5
+ * ensuring proper formatting for DOM style application.
6
+ *
7
+ * @param style - React-style CSS properties object
8
+ * @returns CSSStyleDeclaration compatible object for DOM style application
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const domStyle = toDOMStyle({
13
+ * backgroundColor: 'red',
14
+ * '--custom-color': '#ff0000'
15
+ * })
16
+ * element.style = domStyle
17
+ * ```
18
+ */
19
+ export declare function toDOMStyle(style: CSSProperties | undefined): Partial<CSSStyleDeclaration> | undefined;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.toDOMStyle = toDOMStyle;
7
+ function toDOMStyle(style) {
8
+ if (style === void 0) return void 0;
9
+ const result = {};
10
+ for (const [key, value] of Object.entries(style)) {
11
+ result[key.startsWith("--") ? key : key.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`)] = value;
12
+ }
13
+ return result;
14
+ }
package/cjs/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './css-properties/css-properties.ts';
2
+ export * from './css-properties/to_dom_style.ts';
2
3
  export * from './props/class-name.ts';
3
4
  export * from './props/style.ts';
4
5
  export * from './theme/class-name.ts';
package/cjs/index.js CHANGED
@@ -14,6 +14,17 @@ Object.keys(_cssProperties).forEach(function (key) {
14
14
  }
15
15
  });
16
16
  });
17
+ var _to_dom_style = require("./css-properties/to_dom_style.ts");
18
+ Object.keys(_to_dom_style).forEach(function (key) {
19
+ if (key === "default" || key === "__esModule") return;
20
+ if (key in exports && exports[key] === _to_dom_style[key]) return;
21
+ Object.defineProperty(exports, key, {
22
+ enumerable: true,
23
+ get: function () {
24
+ return _to_dom_style[key];
25
+ }
26
+ });
27
+ });
17
28
  var _className = require("./props/class-name.ts");
18
29
  Object.keys(_className).forEach(function (key) {
19
30
  if (key === "default" || key === "__esModule") return;
@@ -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
- break;
25
+ return;
26
26
  }
27
27
  }
28
+ if (options.allowCustom) {
29
+ options.handler(value);
30
+ }
28
31
  }
29
32
  }, options.element);
30
33
  }
@@ -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): T | null;
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): T | null;
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
  *
@@ -0,0 +1,20 @@
1
+ import type { CSSProperties } from './css-properties.ts';
2
+ /**
3
+ * Converts React-style CSS properties to DOM style properties.
4
+ * This function handles both standard CSS properties and custom properties,
5
+ * ensuring proper formatting for DOM style application.
6
+ *
7
+ * @param style - React-style CSS properties object
8
+ * @returns CSSStyleDeclaration compatible object for DOM style application
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const domStyle = toDOMStyle({
13
+ * backgroundColor: 'red',
14
+ * '--custom-color': '#ff0000'
15
+ * })
16
+ * element.style = domStyle
17
+ * ```
18
+ */
19
+ export declare function toDOMStyle(style: CSSProperties | undefined): Partial<CSSStyleDeclaration> | undefined;
20
+ //# sourceMappingURL=to_dom_style.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to_dom_style.d.ts","sourceRoot":"","sources":["../../src/css-properties/to_dom_style.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAExD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,aAAa,GAAG,SAAS,GAAG,OAAO,CAAC,mBAAmB,CAAC,GAAG,SAAS,CAUrG"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Converts React-style CSS properties to DOM style properties.
3
+ * This function handles both standard CSS properties and custom properties,
4
+ * ensuring proper formatting for DOM style application.
5
+ *
6
+ * @param style - React-style CSS properties object
7
+ * @returns CSSStyleDeclaration compatible object for DOM style application
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const domStyle = toDOMStyle({
12
+ * backgroundColor: 'red',
13
+ * '--custom-color': '#ff0000'
14
+ * })
15
+ * element.style = domStyle
16
+ * ```
17
+ */
18
+ export function toDOMStyle(style) {
19
+ if (style === undefined)
20
+ return undefined;
21
+ const result = {};
22
+ for (const [key, value] of Object.entries(style)) {
23
+ result[key.startsWith('--') ? key : key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`)] = value;
24
+ }
25
+ return result;
26
+ }
27
+ //# sourceMappingURL=to_dom_style.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to_dom_style.js","sourceRoot":"","sources":["../../src/css-properties/to_dom_style.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,UAAU,CAAC,KAAgC;IAC1D,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,SAAS,CAAA;IAEzC,MAAM,MAAM,GAAG,EAAS,CAAA;IAExB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAClD,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,GAAG,KAAK,CAAA;IACzG,CAAC;IAED,OAAO,MAAM,CAAA;AACd,CAAC"}
package/esm/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './css-properties/css-properties.ts';
2
+ export * from './css-properties/to_dom_style.ts';
2
3
  export * from './props/class-name.ts';
3
4
  export * from './props/style.ts';
4
5
  export * from './theme/class-name.ts';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAA;AAClD,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,uBAAuB,CAAA;AACrC,cAAc,2BAA2B,CAAA;AACzC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,iCAAiC,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAA;AAClD,cAAc,kCAAkC,CAAA;AAChD,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,uBAAuB,CAAA;AACrC,cAAc,2BAA2B,CAAA;AACzC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,iCAAiC,CAAA"}
package/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./css-properties/css-properties.js";
2
+ export * from "./css-properties/to_dom_style.js";
2
3
  export * from "./props/class-name.js";
3
4
  export * from "./props/style.js";
4
5
  export * from "./theme/class-name.js";
package/esm/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAA;AAClD,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,uBAAuB,CAAA;AACrC,cAAc,2BAA2B,CAAA;AACzC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,iCAAiC,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAA;AAClD,cAAc,kCAAkC,CAAA;AAChD,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,uBAAuB,CAAA;AACrC,cAAc,2BAA2B,CAAA;AACzC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,iCAAiC,CAAA"}
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;IACvF,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CAAA;IACvC,aAAa,EAAE,QAAQ,MAAM,EAAE,CAAA;IAC/B,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAC7B,GAAG,MAAM,MAAM,GAAG,SAAS,CAM3B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,4BAA4B,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;IAC5F,aAAa,EAAE,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AAE,MAAM,CAAA;IACd,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,CAAA;IACvC,YAAY,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CAAA;IACvC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAC7B,GAAG,gBAAgB,CAAA;AACpB,wBAAgB,4BAA4B,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;IAC5F,aAAa,EAAE,QAAQ,MAAM,EAAE,CAAA;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,CAAA;IACvC,WAAW,EAAE,IAAI,GAAG,SAAS,CAAA;IAC7B,YAAY,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CAAA;IACvC,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
- break;
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;AAEpF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,uBAAuB,CAAwC,OAK9E;IACA,MAAM,KAAK,GAAG,gBA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GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC,CAAA;IAEjF,OAAO,KAAK,IAAI,OAAO,CAAC,YAAY,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;AAClF,CAAC;AA8CD,MAAM,UAAU,4BAA4B,CAAwC,OAOnF;IACA,OAAO,qBAAqB,CAC3B;QACC,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC,KAAoB,EAAE,EAAE;YACjD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACpB,OAAO,CAAC,OAAO,CAAE,OAAO,CAAC,YAAuB,IAAI,IAAI,CAAC,CAAA;gBACzD,OAAM;YACP,CAAC;YAED,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;gBACnC,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC;oBACpC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;oBACrB,OAAM;gBACP,CAAC;YACF,CAAC;YAED,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;gBACzB,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;YACvB,CAAC;QACF,CAAC;KACD,EACD,OAAO,CAAC,OAAO,CACf,CAAA;AACF,CAAC"}
@@ -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): T | null;
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,GAER,CAAC,GAAG,IAAI,CACvD;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
+ {"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,CAAa,CAAA;AACxD,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,iBAAiB,CAChC,QAAmD,EACnD,UAA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,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): T | null;
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,YAGvD;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"}
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@just-web/css",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "CSS types and utilities",
5
5
  "type": "module",
6
6
  "exports": {
@@ -0,0 +1,30 @@
1
+ import type { CSSProperties } from './css-properties.ts'
2
+
3
+ /**
4
+ * Converts React-style CSS properties to DOM style properties.
5
+ * This function handles both standard CSS properties and custom properties,
6
+ * ensuring proper formatting for DOM style application.
7
+ *
8
+ * @param style - React-style CSS properties object
9
+ * @returns CSSStyleDeclaration compatible object for DOM style application
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const domStyle = toDOMStyle({
14
+ * backgroundColor: 'red',
15
+ * '--custom-color': '#ff0000'
16
+ * })
17
+ * element.style = domStyle
18
+ * ```
19
+ */
20
+ export function toDOMStyle(style: CSSProperties | undefined): Partial<CSSStyleDeclaration> | undefined {
21
+ if (style === undefined) return undefined
22
+
23
+ const result = {} as any
24
+
25
+ for (const [key, value] of Object.entries(style)) {
26
+ result[key.startsWith('--') ? key : key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`)] = value
27
+ }
28
+
29
+ return result
30
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './css-properties/css-properties.ts'
2
+ export * from './css-properties/to_dom_style.ts'
2
3
  export * from './props/class-name.ts'
3
4
  export * from './props/style.ts'
4
5
  export * from './theme/class-name.ts'
@@ -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
- break
119
+ return
90
120
  }
91
121
  }
122
+
123
+ if (options.allowCustom) {
124
+ options.handler(value)
125
+ }
92
126
  },
93
127
  },
94
128
  options.element,
@@ -20,7 +20,7 @@ export function getAttribute<T extends string>(
20
20
  qualifiedName: T,
21
21
  element: Element | undefined = ctx.getDocumentElement(),
22
22
  ) {
23
- return element?.getAttribute(qualifiedName) as T | null
23
+ return element?.getAttribute(qualifiedName)
24
24
  }
25
25
 
26
26
  /**