@just-web/css 0.6.1 → 0.8.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.
Files changed (114) hide show
  1. package/cjs/index.cjs +394 -0
  2. package/cjs/index.cjs.map +1 -0
  3. package/cjs/index.d.cts +412 -0
  4. package/cjs/index.d.cts.map +1 -0
  5. package/esm/index.d.mts +412 -0
  6. package/esm/index.d.mts.map +1 -0
  7. package/esm/index.mjs +378 -0
  8. package/esm/index.mjs.map +1 -0
  9. package/package.json +36 -35
  10. package/src/convertors/px_2_num.ts +17 -0
  11. package/src/index.ts +6 -2
  12. package/src/{css-properties/css-properties.ts → properties/css_properties.ts} +6 -2
  13. package/{cjs/css-properties/css-properties.d.ts → src/properties/properties.ts} +9 -4
  14. package/src/{css-properties → properties}/to_dom_style.ts +1 -1
  15. package/src/props/class-name.ts +1 -1
  16. package/src/props/style.ts +3 -3
  17. package/cjs/convertors/px_2_rem.d.ts +0 -21
  18. package/cjs/convertors/px_2_rem.js +0 -17
  19. package/cjs/convertors/rem_2_px.d.ts +0 -21
  20. package/cjs/convertors/rem_2_px.js +0 -17
  21. package/cjs/css-properties/css-properties.js +0 -9
  22. package/cjs/css-properties/to_dom_style.d.ts +0 -19
  23. package/cjs/css-properties/to_dom_style.js +0 -14
  24. package/cjs/globals.ctx.d.ts +0 -5
  25. package/cjs/globals.ctx.js +0 -18
  26. package/cjs/index.d.ts +0 -12
  27. package/cjs/index.js +0 -137
  28. package/cjs/package.json +0 -1
  29. package/cjs/props/class-name.d.ts +0 -11
  30. package/cjs/props/class-name.js +0 -1
  31. package/cjs/props/style.d.ts +0 -7
  32. package/cjs/props/style.js +0 -1
  33. package/cjs/tailwind.css +0 -1
  34. package/cjs/testing/log-panel.d.ts +0 -4
  35. package/cjs/testing/log-panel.js +0 -19
  36. package/cjs/testing/toggle-attribute-button.d.ts +0 -4
  37. package/cjs/testing/toggle-attribute-button.js +0 -28
  38. package/cjs/theme/class-name.d.ts +0 -41
  39. package/cjs/theme/class-name.js +0 -32
  40. package/cjs/theme/data-attribute.d.ts +0 -83
  41. package/cjs/theme/data-attribute.js +0 -33
  42. package/cjs/utils/attribute.d.ts +0 -36
  43. package/cjs/utils/attribute.js +0 -25
  44. package/cjs/utils/data-attribute.d.ts +0 -23
  45. package/cjs/utils/data-attribute.js +0 -15
  46. package/cjs/utils/get-css-prop-values.d.ts +0 -16
  47. package/cjs/utils/get-css-prop-values.js +0 -13
  48. package/cjs/utils/prefers-color-scheme.d.ts +0 -33
  49. package/cjs/utils/prefers-color-scheme.js +0 -29
  50. package/esm/convertors/px_2_rem.d.ts +0 -22
  51. package/esm/convertors/px_2_rem.d.ts.map +0 -1
  52. package/esm/convertors/px_2_rem.js +0 -26
  53. package/esm/convertors/px_2_rem.js.map +0 -1
  54. package/esm/convertors/rem_2_px.d.ts +0 -22
  55. package/esm/convertors/rem_2_px.d.ts.map +0 -1
  56. package/esm/convertors/rem_2_px.js +0 -26
  57. package/esm/convertors/rem_2_px.js.map +0 -1
  58. package/esm/css-properties/css-properties.d.ts +0 -27
  59. package/esm/css-properties/css-properties.d.ts.map +0 -1
  60. package/esm/css-properties/css-properties.js +0 -20
  61. package/esm/css-properties/css-properties.js.map +0 -1
  62. package/esm/css-properties/to_dom_style.d.ts +0 -20
  63. package/esm/css-properties/to_dom_style.d.ts.map +0 -1
  64. package/esm/css-properties/to_dom_style.js +0 -27
  65. package/esm/css-properties/to_dom_style.js.map +0 -1
  66. package/esm/globals.ctx.d.ts +0 -6
  67. package/esm/globals.ctx.d.ts.map +0 -1
  68. package/esm/globals.ctx.js +0 -13
  69. package/esm/globals.ctx.js.map +0 -1
  70. package/esm/index.d.ts +0 -13
  71. package/esm/index.d.ts.map +0 -1
  72. package/esm/index.js +0 -13
  73. package/esm/index.js.map +0 -1
  74. package/esm/props/class-name.d.ts +0 -12
  75. package/esm/props/class-name.d.ts.map +0 -1
  76. package/esm/props/class-name.js +0 -6
  77. package/esm/props/class-name.js.map +0 -1
  78. package/esm/props/style.d.ts +0 -8
  79. package/esm/props/style.d.ts.map +0 -1
  80. package/esm/props/style.js +0 -2
  81. package/esm/props/style.js.map +0 -1
  82. package/esm/testing/log-panel.d.ts +0 -5
  83. package/esm/testing/log-panel.d.ts.map +0 -1
  84. package/esm/testing/log-panel.js +0 -5
  85. package/esm/testing/log-panel.js.map +0 -1
  86. package/esm/testing/toggle-attribute-button.d.ts +0 -5
  87. package/esm/testing/toggle-attribute-button.d.ts.map +0 -1
  88. package/esm/testing/toggle-attribute-button.js +0 -19
  89. package/esm/testing/toggle-attribute-button.js.map +0 -1
  90. package/esm/theme/class-name.d.ts +0 -42
  91. package/esm/theme/class-name.d.ts.map +0 -1
  92. package/esm/theme/class-name.js +0 -56
  93. package/esm/theme/class-name.js.map +0 -1
  94. package/esm/theme/data-attribute.d.ts +0 -84
  95. package/esm/theme/data-attribute.d.ts.map +0 -1
  96. package/esm/theme/data-attribute.js +0 -27
  97. package/esm/theme/data-attribute.js.map +0 -1
  98. package/esm/utils/attribute.d.ts +0 -37
  99. package/esm/utils/attribute.d.ts.map +0 -1
  100. package/esm/utils/attribute.js +0 -53
  101. package/esm/utils/attribute.js.map +0 -1
  102. package/esm/utils/data-attribute.d.ts +0 -24
  103. package/esm/utils/data-attribute.d.ts.map +0 -1
  104. package/esm/utils/data-attribute.js +0 -30
  105. package/esm/utils/data-attribute.js.map +0 -1
  106. package/esm/utils/get-css-prop-values.d.ts +0 -17
  107. package/esm/utils/get-css-prop-values.d.ts.map +0 -1
  108. package/esm/utils/get-css-prop-values.js +0 -8
  109. package/esm/utils/get-css-prop-values.js.map +0 -1
  110. package/esm/utils/prefers-color-scheme.d.ts +0 -34
  111. package/esm/utils/prefers-color-scheme.d.ts.map +0 -1
  112. package/esm/utils/prefers-color-scheme.js +0 -54
  113. package/esm/utils/prefers-color-scheme.js.map +0 -1
  114. package/src/tailwind.css +0 -3
@@ -1,17 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.px2rem = px2rem;
7
- function px2rem(px, options) {
8
- const {
9
- base = 16,
10
- precision = 4
11
- } = options ?? {};
12
- if (typeof px === "string") {
13
- px = px.replace(/px$/, "");
14
- px = Number.parseFloat(px);
15
- }
16
- return Number((px / base).toFixed(precision));
17
- }
@@ -1,21 +0,0 @@
1
- /**
2
- * Converts rem values to pixel units.
3
- *
4
- * @param rem - The rem value to convert. Can be a number or string (e.g. '1rem' or '1')
5
- * @param options - Optional configuration
6
- * @param options.base - Base pixel value to calculate pixels from. Defaults to 16
7
- * @param options.precision - Number of decimal places in the output. Defaults to 4
8
- * @returns The converted value as a string with 'px' units
9
- *
10
- * @example
11
- * ```ts
12
- * rem2px(1) // '16.0000'
13
- * rem2px('2rem') // '32.0000'
14
- * rem2px(1, { base: 20 }) // '20.0000'
15
- * rem2px(0.8125, { precision: 2 }) // '13.00'
16
- * ```
17
- */
18
- export declare function rem2px(rem: number | string, options?: {
19
- base?: number | undefined;
20
- precision?: number | undefined;
21
- }): number;
@@ -1,17 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.rem2px = rem2px;
7
- function rem2px(rem, options) {
8
- const {
9
- base = 16,
10
- precision = 4
11
- } = options ?? {};
12
- if (typeof rem === "string") {
13
- rem = rem.replace(/rem$/, "");
14
- rem = Number.parseFloat(rem);
15
- }
16
- return Number((rem * base).toFixed(precision));
17
- }
@@ -1,9 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.defineCSSProperties = defineCSSProperties;
7
- function defineCSSProperties(style) {
8
- return style;
9
- }
@@ -1,19 +0,0 @@
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;
@@ -1,14 +0,0 @@
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
- }
@@ -1,5 +0,0 @@
1
- export declare const ctx: {
2
- matchMedia(query: string): MediaQueryList;
3
- getDocumentElement(): HTMLElement;
4
- _reset(): void;
5
- };
@@ -1,18 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.ctx = void 0;
7
- const ctx = exports.ctx = {
8
- matchMedia(query) {
9
- return globalThis.matchMedia(query);
10
- },
11
- getDocumentElement() {
12
- return globalThis.document.documentElement;
13
- },
14
- _reset() {
15
- this.matchMedia = globalThis.matchMedia;
16
- this.getDocumentElement = () => globalThis.document.documentElement;
17
- }
18
- };
package/cjs/index.d.ts DELETED
@@ -1,12 +0,0 @@
1
- export * from './convertors/px_2_rem.ts';
2
- export * from './convertors/rem_2_px.ts';
3
- export * from './css-properties/css-properties.ts';
4
- export * from './css-properties/to_dom_style.ts';
5
- export * from './props/class-name.ts';
6
- export * from './props/style.ts';
7
- export * from './theme/class-name.ts';
8
- export * from './theme/data-attribute.ts';
9
- export * from './utils/attribute.ts';
10
- export * from './utils/data-attribute.ts';
11
- export * from './utils/get-css-prop-values.ts';
12
- export * from './utils/prefers-color-scheme.ts';
package/cjs/index.js DELETED
@@ -1,137 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var _px_2_rem = require("./convertors/px_2_rem.ts");
7
- Object.keys(_px_2_rem).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _px_2_rem[key]) return;
10
- Object.defineProperty(exports, key, {
11
- enumerable: true,
12
- get: function () {
13
- return _px_2_rem[key];
14
- }
15
- });
16
- });
17
- var _rem_2_px = require("./convertors/rem_2_px.ts");
18
- Object.keys(_rem_2_px).forEach(function (key) {
19
- if (key === "default" || key === "__esModule") return;
20
- if (key in exports && exports[key] === _rem_2_px[key]) return;
21
- Object.defineProperty(exports, key, {
22
- enumerable: true,
23
- get: function () {
24
- return _rem_2_px[key];
25
- }
26
- });
27
- });
28
- var _cssProperties = require("./css-properties/css-properties.ts");
29
- Object.keys(_cssProperties).forEach(function (key) {
30
- if (key === "default" || key === "__esModule") return;
31
- if (key in exports && exports[key] === _cssProperties[key]) return;
32
- Object.defineProperty(exports, key, {
33
- enumerable: true,
34
- get: function () {
35
- return _cssProperties[key];
36
- }
37
- });
38
- });
39
- var _to_dom_style = require("./css-properties/to_dom_style.ts");
40
- Object.keys(_to_dom_style).forEach(function (key) {
41
- if (key === "default" || key === "__esModule") return;
42
- if (key in exports && exports[key] === _to_dom_style[key]) return;
43
- Object.defineProperty(exports, key, {
44
- enumerable: true,
45
- get: function () {
46
- return _to_dom_style[key];
47
- }
48
- });
49
- });
50
- var _className = require("./props/class-name.ts");
51
- Object.keys(_className).forEach(function (key) {
52
- if (key === "default" || key === "__esModule") return;
53
- if (key in exports && exports[key] === _className[key]) return;
54
- Object.defineProperty(exports, key, {
55
- enumerable: true,
56
- get: function () {
57
- return _className[key];
58
- }
59
- });
60
- });
61
- var _style = require("./props/style.ts");
62
- Object.keys(_style).forEach(function (key) {
63
- if (key === "default" || key === "__esModule") return;
64
- if (key in exports && exports[key] === _style[key]) return;
65
- Object.defineProperty(exports, key, {
66
- enumerable: true,
67
- get: function () {
68
- return _style[key];
69
- }
70
- });
71
- });
72
- var _className2 = require("./theme/class-name.ts");
73
- Object.keys(_className2).forEach(function (key) {
74
- if (key === "default" || key === "__esModule") return;
75
- if (key in exports && exports[key] === _className2[key]) return;
76
- Object.defineProperty(exports, key, {
77
- enumerable: true,
78
- get: function () {
79
- return _className2[key];
80
- }
81
- });
82
- });
83
- var _dataAttribute = require("./theme/data-attribute.ts");
84
- Object.keys(_dataAttribute).forEach(function (key) {
85
- if (key === "default" || key === "__esModule") return;
86
- if (key in exports && exports[key] === _dataAttribute[key]) return;
87
- Object.defineProperty(exports, key, {
88
- enumerable: true,
89
- get: function () {
90
- return _dataAttribute[key];
91
- }
92
- });
93
- });
94
- var _attribute = require("./utils/attribute.ts");
95
- Object.keys(_attribute).forEach(function (key) {
96
- if (key === "default" || key === "__esModule") return;
97
- if (key in exports && exports[key] === _attribute[key]) return;
98
- Object.defineProperty(exports, key, {
99
- enumerable: true,
100
- get: function () {
101
- return _attribute[key];
102
- }
103
- });
104
- });
105
- var _dataAttribute2 = require("./utils/data-attribute.ts");
106
- Object.keys(_dataAttribute2).forEach(function (key) {
107
- if (key === "default" || key === "__esModule") return;
108
- if (key in exports && exports[key] === _dataAttribute2[key]) return;
109
- Object.defineProperty(exports, key, {
110
- enumerable: true,
111
- get: function () {
112
- return _dataAttribute2[key];
113
- }
114
- });
115
- });
116
- var _getCssPropValues = require("./utils/get-css-prop-values.ts");
117
- Object.keys(_getCssPropValues).forEach(function (key) {
118
- if (key === "default" || key === "__esModule") return;
119
- if (key in exports && exports[key] === _getCssPropValues[key]) return;
120
- Object.defineProperty(exports, key, {
121
- enumerable: true,
122
- get: function () {
123
- return _getCssPropValues[key];
124
- }
125
- });
126
- });
127
- var _prefersColorScheme = require("./utils/prefers-color-scheme.ts");
128
- Object.keys(_prefersColorScheme).forEach(function (key) {
129
- if (key === "default" || key === "__esModule") return;
130
- if (key in exports && exports[key] === _prefersColorScheme[key]) return;
131
- Object.defineProperty(exports, key, {
132
- enumerable: true,
133
- get: function () {
134
- return _prefersColorScheme[key];
135
- }
136
- });
137
- });
package/cjs/package.json DELETED
@@ -1 +0,0 @@
1
- { "type": "commonjs" }
@@ -1,11 +0,0 @@
1
- /**
2
- * Note that `className` could be specific to ReactJS.
3
- * So this type may be misplaced in this package.
4
- */
5
- /**
6
- * Interface for component props that include a className property.
7
- * The className property accepts a string value for CSS class names.
8
- */
9
- export interface ClassNameProps {
10
- className?: string | undefined;
11
- }
@@ -1 +0,0 @@
1
- "use strict";
@@ -1,7 +0,0 @@
1
- import type { CSSProperties } from '../css-properties/css-properties.ts';
2
- /**
3
- * Interface for component props that include a style property.
4
- */
5
- export interface StyleProps {
6
- style?: CSSProperties | undefined;
7
- }
@@ -1 +0,0 @@
1
- "use strict";
package/cjs/tailwind.css DELETED
@@ -1 +0,0 @@
1
- @import "tailwindcss";@custom-variant dark (&:where(.dark, .dark *));
@@ -1,4 +0,0 @@
1
- export declare function LogPanel({ title, log }: {
2
- title: string;
3
- log: string[];
4
- }): import("react").JSX.Element;
@@ -1,19 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.LogPanel = LogPanel;
7
- function LogPanel({
8
- title,
9
- log
10
- }) {
11
- return /* @__PURE__ */React.createElement("div", {
12
- className: "bg-neutral-100 dark:bg-neutral-900 p-4 rounded overflow-y-auto"
13
- }, /* @__PURE__ */React.createElement("h4", {
14
- className: "mb-2"
15
- }, title), log.map((entry, i) => /* @__PURE__ */React.createElement("pre", {
16
- key: i,
17
- className: "font-mono"
18
- }, entry)));
19
- }
@@ -1,4 +0,0 @@
1
- export declare const ToggleAttributeButton: import("react").ForwardRefExoticComponent<{
2
- attribute: string;
3
- values?: string[];
4
- } & import("react").RefAttributes<HTMLElement>>;
@@ -1,28 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.ToggleAttributeButton = void 0;
7
- var _react = require("react");
8
- const ToggleAttributeButton = exports.ToggleAttributeButton = (0, _react.forwardRef)(({
9
- attribute,
10
- values = ["test-value"]
11
- }, ref) => {
12
- const handleAttributeChange = (0, _react.useCallback)(attr => {
13
- const target = (ref && "current" in ref ? ref.current : null) ?? document.documentElement;
14
- const currentValue = target.getAttribute(attr);
15
- const nextIndex = currentValue ? values.indexOf(currentValue) + 1 : 0;
16
- const newValue = nextIndex < values.length ? values[nextIndex] : null;
17
- if (newValue === null) {
18
- target.removeAttribute(attr);
19
- } else {
20
- target.setAttribute(attr, newValue);
21
- }
22
- }, [ref, values]);
23
- return /* @__PURE__ */React.createElement("button", {
24
- key: attribute,
25
- className: "bg-cyan-700 text-white px-4 py-2 rounded-md shadow-md active:bg-cyan-800",
26
- onClick: () => handleAttributeChange(attribute)
27
- }, "Toggle ", attribute);
28
- });
@@ -1,41 +0,0 @@
1
- /**
2
- * Gets the current theme by checking element class names against a themes map.
3
- *
4
- * @param options - Configuration options
5
- * @param options.themes - Record mapping theme keys to their class name values
6
- * @param options.defaultTheme - Fallback theme key if no matching class is found
7
- * @param options.element - Element to check classes on (defaults to document.documentElement)
8
- * @returns The matching theme key or defaultTheme if no match found
9
- *
10
- * @example
11
- * ```ts
12
- * const themes = {
13
- * light: 'theme-light',
14
- * dark: 'theme-dark'
15
- * }
16
- *
17
- * // Get current theme from document.documentElement
18
- * const theme = getThemeByClassName({
19
- * themes,
20
- * defaultTheme: 'light'
21
- * })
22
- *
23
- * // Get theme from specific element
24
- * const theme = getThemeByClassName({
25
- * themes,
26
- * element: myElement,
27
- * defaultTheme: 'light'
28
- * })
29
- * ```
30
- */
31
- export declare function getThemeByClassName<Themes extends Record<string, string>>(options: {
32
- themes: Themes;
33
- defaultTheme?: keyof Themes | undefined;
34
- element?: Element | undefined;
35
- }): keyof Themes | undefined;
36
- export declare function observeThemeByClassName<Themes extends Record<string, string>>(options: {
37
- themes: Themes;
38
- handler: (value: string | undefined) => void;
39
- defaultTheme?: keyof Themes | undefined;
40
- element?: Element | undefined;
41
- }): MutationObserver;
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getThemeByClassName = getThemeByClassName;
7
- exports.observeThemeByClassName = observeThemeByClassName;
8
- var _typePlus = require("type-plus");
9
- var _globalsCtx = require("../globals.ctx.ts");
10
- var _attribute = require("../utils/attribute.ts");
11
- function getThemeByClassName(options) {
12
- const element = options.element ?? _globalsCtx.ctx.getDocumentElement();
13
- const className = element.className;
14
- const theme = (0, _typePlus.findKey)(options.themes, theme2 => className.includes(options.themes[theme2]));
15
- return theme ?? options.defaultTheme;
16
- }
17
- function observeThemeByClassName(options) {
18
- return (0, _attribute.observeAttributes)({
19
- class: value => {
20
- if (value === null) {
21
- options.handler(options.defaultTheme);
22
- return;
23
- }
24
- for (const name in options.themes) {
25
- if (value.includes(options.themes[name])) {
26
- options.handler(name);
27
- break;
28
- }
29
- }
30
- }
31
- }, options.element);
32
- }
@@ -1,83 +0,0 @@
1
- /**
2
- * Gets the theme based on a data attribute value.
3
- *
4
- * @param options - Configuration options
5
- * @param options.themes - Record mapping theme keys to their data attribute values
6
- * @param options.defaultTheme - Fallback theme key if attribute value doesn't match any theme
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
9
- * @returns The matching theme key, or defaultTheme if no match found
10
- *
11
- * @example
12
- * ```ts
13
- * const themes = {
14
- * light: 'light',
15
- * dark: 'dark',
16
- * system: 'system'
17
- * }
18
- *
19
- * // Get theme from data-theme attribute
20
- * const theme = getThemeByDataAttribute({
21
- * themes,
22
- * defaultTheme: 'system',
23
- * attributeName: 'data-theme'
24
- * })
25
- * ```
26
- */
27
- export declare function getThemeByDataAttribute<Themes extends Record<string, string>>(options: {
28
- attributeName: `data-${string}`;
29
- defaultTheme?: keyof Themes | undefined;
30
- themes: Themes;
31
- element?: Element | undefined;
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;
40
- /**
41
- * Observes changes to a theme data attribute and calls a handler when it changes.
42
- *
43
- * @param options - Configuration options
44
- * @param options.themes - Record mapping theme keys to their data attribute values
45
- * @param options.handler - Callback function called with the new theme value or null when removed
46
- * @param options.defaultTheme - Fallback theme key if attribute value doesn't match any theme
47
- * @param options.attributeName - Name of the data attribute to observe (must start with 'data-')
48
- * @returns A MutationObserver that can be disconnected to stop observing
49
- *
50
- * @example
51
- * ```ts
52
- * const themes = {
53
- * light: 'light',
54
- * dark: 'dark'
55
- * }
56
- *
57
- * // Observe data-theme attribute changes
58
- * const observer = observeThemeByDataAttributes({
59
- * themes,
60
- * handler: (theme) => console.log('Theme changed to:', theme),
61
- * defaultTheme: 'light',
62
- * attributeName: 'data-theme'
63
- * })
64
- *
65
- * // Stop observing
66
- * observer.disconnect()
67
- * ```
68
- */
69
- export declare function observeThemeByDataAttributes<Themes extends Record<string, string>>(options: {
70
- attributeName: `data-${string}`;
71
- themes: Themes;
72
- handler: (value: string | null) => void;
73
- defaultTheme?: keyof Themes | undefined;
74
- element?: Element | undefined;
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;
@@ -1,33 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getThemeByDataAttribute = getThemeByDataAttribute;
7
- exports.observeThemeByDataAttributes = observeThemeByDataAttributes;
8
- var _typePlus = require("type-plus");
9
- var _dataAttribute = require("../utils/data-attribute.ts");
10
- function getThemeByDataAttribute(options) {
11
- const value = (0, _dataAttribute.getDataAttribute)(options.attributeName, options.element) ?? void 0;
12
- const theme = (0, _typePlus.findKey)(options.themes, theme2 => options.themes[theme2] === value);
13
- return theme ?? options.defaultTheme ?? (options.allowCustom ? value : void 0);
14
- }
15
- function observeThemeByDataAttributes(options) {
16
- return (0, _dataAttribute.observeDataAttributes)({
17
- [options.attributeName]: value => {
18
- if (value === null) {
19
- options.handler(options.defaultTheme ?? null);
20
- return;
21
- }
22
- for (const name in options.themes) {
23
- if (options.themes[name] === value) {
24
- options.handler(name);
25
- return;
26
- }
27
- }
28
- if (options.allowCustom) {
29
- options.handler(value);
30
- }
31
- }
32
- }, options.element);
33
- }
@@ -1,36 +0,0 @@
1
- /**
2
- * Gets the value of an attribute from an element.
3
- *
4
- * @param qualifiedName - The name of the attribute to get
5
- * @param element - The element to get the attribute from. Defaults to `document.documentElement`
6
- * @returns The attribute value cast to type T, or null if the attribute doesn't exist
7
- *
8
- * @example
9
- * ```ts
10
- * // Get theme from document root
11
- * const theme = getAttribute('data-theme')
12
- *
13
- * // Get data-testid from a specific element
14
- * const testId = getAttribute('data-testid', element)
15
- * ```
16
- */
17
- export declare function getAttribute<T extends string>(qualifiedName: T, element?: Element | undefined): string | null;
18
- /**
19
- * Observes attributes changes on an element and calls corresponding handlers.
20
- *
21
- * @param handlers - An object mapping attribute names to handler functions.
22
- * @param element - The element to observe. Defaults to `document.documentElement`.
23
- * @returns {MutationObserver} The observer instance, which can be used to disconnect the observer.
24
- *
25
- * @example
26
- * ```ts
27
- * const observer = observeAttributes({
28
- * 'data-theme': (attr, value) => console.log(`Theme changed to: ${value}`),
29
- * 'class': (attr, value) => console.log(`class changed to: ${value}`)
30
- * });
31
- *
32
- * // Later, to stop observing:
33
- * observer.disconnect();
34
- * ```
35
- */
36
- export declare function observeAttributes<T extends string>(handlers: Record<string, (value: T | null) => void>, element?: Element | undefined): MutationObserver;
@@ -1,25 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getAttribute = getAttribute;
7
- exports.observeAttributes = observeAttributes;
8
- var _globalsCtx = require("../globals.ctx.ts");
9
- function getAttribute(qualifiedName, element = _globalsCtx.ctx.getDocumentElement()) {
10
- return element?.getAttribute(qualifiedName);
11
- }
12
- function observeAttributes(handlers, element = _globalsCtx.ctx.getDocumentElement()) {
13
- const observer = new MutationObserver(mutations => {
14
- for (const mutation of mutations) {
15
- const attribute = mutation.attributeName;
16
- const value = element.getAttribute(attribute);
17
- handlers[attribute]?.(value);
18
- }
19
- });
20
- observer.observe(element, {
21
- attributes: true,
22
- attributeFilter: Object.keys(handlers)
23
- });
24
- return observer;
25
- }
@@ -1,23 +0,0 @@
1
- export declare function getDataAttribute<T extends `data-${string}`>(qualifiedName: T, element?: Element | undefined): string | null;
2
- /**
3
- * Observes changes to `data-*` attributes on an element and calls corresponding handlers.
4
- *
5
- * @param options - Configuration options
6
- * @param options.handlers - An object mapping `data-*` attribute names to handler functions.
7
- * @param options.element - The element to observe. Defaults to `document.documentElement`
8
- * @returns {MutationObserver} The observer instance, which can be used to disconnect the observer
9
- *
10
- * @example
11
- * ```ts
12
- * const observer = observeDataAttributes({
13
- * handlers: {
14
- * 'data-theme': (value) => console.log(`Theme changed to: ${value}`),
15
- * 'data-mode': (value) => console.log(`Mode changed to: ${value}`)
16
- * }
17
- * });
18
- *
19
- * // Later, to stop observing:
20
- * observer.disconnect();
21
- * ```
22
- */
23
- export declare function observeDataAttributes<T extends string, K extends `data-${string}`>(handlers: Record<K, (value: T | null) => void>, element?: Element | undefined): MutationObserver;