@just-web/css 0.1.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/esm/class-name.d.ts +12 -0
- package/esm/class-name.d.ts.map +1 -0
- package/esm/class-name.js +6 -0
- package/esm/class-name.js.map +1 -0
- package/esm/css-properties.d.ts +10 -0
- package/esm/css-properties.d.ts.map +1 -0
- package/esm/css-properties.js +2 -0
- package/esm/css-properties.js.map +1 -0
- package/esm/globals.ctx.d.ts +6 -0
- package/esm/globals.ctx.d.ts.map +1 -0
- package/esm/globals.ctx.js +13 -0
- package/esm/globals.ctx.js.map +1 -0
- package/esm/index.d.ts +9 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +9 -0
- package/esm/index.js.map +1 -0
- package/esm/style.d.ts +8 -0
- package/esm/style.d.ts.map +1 -0
- package/esm/style.js +2 -0
- package/esm/style.js.map +1 -0
- package/esm/testing/log-panel.d.ts +5 -0
- package/esm/testing/log-panel.d.ts.map +1 -0
- package/esm/testing/log-panel.js +5 -0
- package/esm/testing/log-panel.js.map +1 -0
- package/esm/testing/toggle-attribute-button.d.ts +5 -0
- package/esm/testing/toggle-attribute-button.d.ts.map +1 -0
- package/esm/testing/toggle-attribute-button.js +19 -0
- package/esm/testing/toggle-attribute-button.js.map +1 -0
- package/esm/theme/class-name.d.ts +42 -0
- package/esm/theme/class-name.d.ts.map +1 -0
- package/esm/theme/class-name.js +56 -0
- package/esm/theme/class-name.js.map +1 -0
- package/esm/theme/data-attribute.d.ts +68 -0
- package/esm/theme/data-attribute.d.ts.map +1 -0
- package/esm/theme/data-attribute.js +78 -0
- package/esm/theme/data-attribute.js.map +1 -0
- package/esm/utils/attribute.d.ts +37 -0
- package/esm/utils/attribute.d.ts.map +1 -0
- package/esm/utils/attribute.js +53 -0
- package/esm/utils/attribute.js.map +1 -0
- package/esm/utils/data-attribute.d.ts +24 -0
- package/esm/utils/data-attribute.d.ts.map +1 -0
- package/esm/utils/data-attribute.js +30 -0
- package/esm/utils/data-attribute.js.map +1 -0
- package/esm/utils/prefers-color-scheme.d.ts +34 -0
- package/esm/utils/prefers-color-scheme.d.ts.map +1 -0
- package/esm/utils/prefers-color-scheme.js +54 -0
- package/esm/utils/prefers-color-scheme.js.map +1 -0
- package/package.json +67 -0
- package/readme.md +15 -0
- package/src/class-name.ts +12 -0
- package/src/css-properties.ts +10 -0
- package/src/globals.ctx.ts +12 -0
- package/src/index.ts +8 -0
- package/src/style.ts +8 -0
- package/src/tailwind.css +1 -0
- package/src/testing/log-panel.tsx +12 -0
- package/src/testing/toggle-attribute-button.tsx +32 -0
- package/src/theme/class-name.ts +70 -0
- package/src/theme/data-attribute.ts +96 -0
- package/src/utils/attribute.ts +60 -0
- package/src/utils/data-attribute.ts +37 -0
- package/src/utils/prefers-color-scheme.ts +57 -0
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
|
12
|
+
//# sourceMappingURL=class-name.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"class-name.d.ts","sourceRoot":"","sources":["../src/class-name.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC9B,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC9B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"class-name.js","sourceRoot":"","sources":["../src/class-name.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Properties } from 'csstype';
|
|
2
|
+
/**
|
|
3
|
+
* Extends CSS properties to include custom properties.
|
|
4
|
+
* Allows for string or number values for standard properties,
|
|
5
|
+
* and string values for custom properties with '--' prefix.
|
|
6
|
+
*/
|
|
7
|
+
export interface CSSProperties extends Properties<string | number> {
|
|
8
|
+
[k: `--${string}`]: string;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=css-properties.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"css-properties.d.ts","sourceRoot":"","sources":["../src/css-properties.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEzC;;;;GAIG;AACH,MAAM,WAAW,aAAc,SAAQ,UAAU,CAAC,MAAM,GAAG,MAAM,CAAC;IACjE,CAAC,CAAC,EAAE,KAAK,MAAM,EAAE,GAAG,MAAM,CAAA;CAC1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"css-properties.js","sourceRoot":"","sources":["../src/css-properties.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"globals.ctx.d.ts","sourceRoot":"","sources":["../src/globals.ctx.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,GAAG;sBACG,MAAM;;;CAUxB,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const ctx = {
|
|
2
|
+
matchMedia(query) {
|
|
3
|
+
return globalThis.matchMedia(query);
|
|
4
|
+
},
|
|
5
|
+
getDocumentElement() {
|
|
6
|
+
return globalThis.document.documentElement;
|
|
7
|
+
},
|
|
8
|
+
_reset() {
|
|
9
|
+
this.matchMedia = globalThis.matchMedia;
|
|
10
|
+
this.getDocumentElement = () => globalThis.document.documentElement;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=globals.ctx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"globals.ctx.js","sourceRoot":"","sources":["../src/globals.ctx.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,GAAG,GAAG;IAClB,UAAU,CAAC,KAAa;QACvB,OAAO,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;IACpC,CAAC;IACD,kBAAkB;QACjB,OAAO,UAAU,CAAC,QAAQ,CAAC,eAAe,CAAA;IAC3C,CAAC;IACD,MAAM;QACL,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,UAAU,CAAA;QACvC,IAAI,CAAC,kBAAkB,GAAG,GAAG,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,eAAe,CAAA;IACpE,CAAC;CACD,CAAA"}
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './class-name.ts';
|
|
2
|
+
export * from './css-properties.ts';
|
|
3
|
+
export * from './style.ts';
|
|
4
|
+
export * from './theme/class-name.ts';
|
|
5
|
+
export * from './theme/data-attribute.ts';
|
|
6
|
+
export * from './utils/attribute.ts';
|
|
7
|
+
export * from './utils/data-attribute.ts';
|
|
8
|
+
export * from './utils/prefers-color-scheme.ts';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,qBAAqB,CAAA;AACnC,cAAc,YAAY,CAAA;AAC1B,cAAc,uBAAuB,CAAA;AACrC,cAAc,2BAA2B,CAAA;AACzC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,iCAAiC,CAAA"}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./class-name.js";
|
|
2
|
+
export * from "./css-properties.js";
|
|
3
|
+
export * from "./style.js";
|
|
4
|
+
export * from "./theme/class-name.js";
|
|
5
|
+
export * from "./theme/data-attribute.js";
|
|
6
|
+
export * from "./utils/attribute.js";
|
|
7
|
+
export * from "./utils/data-attribute.js";
|
|
8
|
+
export * from "./utils/prefers-color-scheme.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
package/esm/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,qBAAqB,CAAA;AACnC,cAAc,YAAY,CAAA;AAC1B,cAAc,uBAAuB,CAAA;AACrC,cAAc,2BAA2B,CAAA;AACzC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,iCAAiC,CAAA"}
|
package/esm/style.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"style.d.ts","sourceRoot":"","sources":["../src/style.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAExD;;GAEG;AACH,MAAM,WAAW,UAAU;IAC1B,KAAK,CAAC,EAAE,aAAa,GAAG,SAAS,CAAA;CACjC"}
|
package/esm/style.js
ADDED
package/esm/style.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"style.js","sourceRoot":"","sources":["../src/style.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log-panel.d.ts","sourceRoot":"","sources":["../../src/testing/log-panel.tsx"],"names":[],"mappings":"AAAA,wBAAgB,QAAQ,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,EAAE,CAAA;CAAE,2CAWxE"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
export function LogPanel({ title, log }) {
|
|
3
|
+
return (_jsxs("div", { className: "bg-neutral-100 p-4 rounded overflow-y-auto", children: [_jsx("h4", { className: "mb-2", children: title }), log.map((entry, i) => (_jsx("pre", { className: "font-mono", children: entry }, i)))] }));
|
|
4
|
+
}
|
|
5
|
+
//# sourceMappingURL=log-panel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log-panel.js","sourceRoot":"","sources":["../../src/testing/log-panel.tsx"],"names":[],"mappings":";AAAA,MAAM,UAAU,QAAQ,CAAC,EAAE,KAAK,EAAE,GAAG,EAAoC;IACxE,OAAO,CACN,eAAK,SAAS,EAAC,4CAA4C,aAC1D,aAAI,SAAS,EAAC,MAAM,YAAE,KAAK,GAAM,EAChC,GAAG,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC,CACtB,cAAa,SAAS,EAAC,WAAW,YAChC,KAAK,IADG,CAAC,CAEL,CACN,CAAC,IACG,CACN,CAAA;AACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toggle-attribute-button.d.ts","sourceRoot":"","sources":["../../src/testing/toggle-attribute-button.tsx"],"names":[],"mappings":"AAEA,eAAO,MAAM,qBAAqB;eAAwC,MAAM;aAAW,MAAM,EAAE;+CA6BlG,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef, useCallback } from 'react';
|
|
3
|
+
export const ToggleAttributeButton = forwardRef(({ attribute, values = ['test-value'] }, ref) => {
|
|
4
|
+
const handleAttributeChange = useCallback((attr) => {
|
|
5
|
+
// Handle both RefObject and function ref cases
|
|
6
|
+
const target = (ref && 'current' in ref ? ref.current : null) ?? document.documentElement;
|
|
7
|
+
const currentValue = target.getAttribute(attr);
|
|
8
|
+
const nextIndex = currentValue ? values.indexOf(currentValue) + 1 : 0;
|
|
9
|
+
const newValue = nextIndex < values.length ? values[nextIndex] : null;
|
|
10
|
+
if (newValue === null) {
|
|
11
|
+
target.removeAttribute(attr);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
target.setAttribute(attr, newValue);
|
|
15
|
+
}
|
|
16
|
+
}, [ref, values]);
|
|
17
|
+
return (_jsxs("button", { className: "bg-cyan-700 text-white px-4 py-2 rounded-md shadow-md active:bg-cyan-800", onClick: () => handleAttributeChange(attribute), children: ["Toggle ", attribute] }, attribute));
|
|
18
|
+
});
|
|
19
|
+
//# sourceMappingURL=toggle-attribute-button.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toggle-attribute-button.js","sourceRoot":"","sources":["../../src/testing/toggle-attribute-button.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAE/C,MAAM,CAAC,MAAM,qBAAqB,GAAG,UAAU,CAC9C,CAAC,EAAE,SAAS,EAAE,MAAM,GAAG,CAAC,YAAY,CAAC,EAAE,EAAE,GAAG,EAAE,EAAE;IAC/C,MAAM,qBAAqB,GAAG,WAAW,CACxC,CAAC,IAAY,EAAE,EAAE;QAChB,+CAA+C;QAC/C,MAAM,MAAM,GAAG,CAAC,GAAG,IAAI,SAAS,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,QAAQ,CAAC,eAAe,CAAA;QACzF,MAAM,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAA;QAC9C,MAAM,SAAS,GAAG,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QACrE,MAAM,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAE,CAAC,CAAC,CAAC,IAAI,CAAA;QAEtE,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACvB,MAAM,CAAC,eAAe,CAAC,IAAI,CAAC,CAAA;QAC7B,CAAC;aAAM,CAAC;YACP,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;QACpC,CAAC;IACF,CAAC,EACD,CAAC,GAAG,EAAE,MAAM,CAAC,CACb,CAAA;IAED,OAAO,CACN,kBAEC,SAAS,EAAC,0EAA0E,EACpF,OAAO,EAAE,GAAG,EAAE,CAAC,qBAAqB,CAAC,SAAS,CAAC,wBAEvC,SAAS,KAJZ,SAAS,CAKN,CACT,CAAA;AACF,CAAC,CACD,CAAA"}
|
|
@@ -0,0 +1,42 @@
|
|
|
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;
|
|
42
|
+
//# sourceMappingURL=class-name.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"class-name.d.ts","sourceRoot":"","sources":["../../src/theme/class-name.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;IACnF,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CAAA;IACvC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAC7B,GAAG,MAAM,MAAM,GAAG,SAAS,CAK3B;AAED,wBAAgB,uBAAuB,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;IACvF,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,KAAK,IAAI,CAAA;IAC5C,YAAY,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CAAA;IACvC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAC7B,oBAmBA"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { findKey } from 'type-plus';
|
|
2
|
+
import { ctx } from "../globals.ctx.js";
|
|
3
|
+
import { observeAttributes } from "../utils/attribute.js";
|
|
4
|
+
/**
|
|
5
|
+
* Gets the current theme by checking element class names against a themes map.
|
|
6
|
+
*
|
|
7
|
+
* @param options - Configuration options
|
|
8
|
+
* @param options.themes - Record mapping theme keys to their class name values
|
|
9
|
+
* @param options.defaultTheme - Fallback theme key if no matching class is found
|
|
10
|
+
* @param options.element - Element to check classes on (defaults to document.documentElement)
|
|
11
|
+
* @returns The matching theme key or defaultTheme if no match found
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const themes = {
|
|
16
|
+
* light: 'theme-light',
|
|
17
|
+
* dark: 'theme-dark'
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Get current theme from document.documentElement
|
|
21
|
+
* const theme = getThemeByClassName({
|
|
22
|
+
* themes,
|
|
23
|
+
* defaultTheme: 'light'
|
|
24
|
+
* })
|
|
25
|
+
*
|
|
26
|
+
* // Get theme from specific element
|
|
27
|
+
* const theme = getThemeByClassName({
|
|
28
|
+
* themes,
|
|
29
|
+
* element: myElement,
|
|
30
|
+
* defaultTheme: 'light'
|
|
31
|
+
* })
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function getThemeByClassName(options) {
|
|
35
|
+
const element = options.element ?? ctx.getDocumentElement();
|
|
36
|
+
const className = element.className;
|
|
37
|
+
const theme = findKey(options.themes, (theme) => className.includes(options.themes[theme]));
|
|
38
|
+
return theme ?? options.defaultTheme;
|
|
39
|
+
}
|
|
40
|
+
export function observeThemeByClassName(options) {
|
|
41
|
+
return observeAttributes({
|
|
42
|
+
class: (value) => {
|
|
43
|
+
if (value === null) {
|
|
44
|
+
options.handler(options.defaultTheme);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
for (const name in options.themes) {
|
|
48
|
+
if (value.includes(options.themes[name])) {
|
|
49
|
+
options.handler(name);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
}, options.element);
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=class-name.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"class-name.js","sourceRoot":"","sources":["../../src/theme/class-name.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,UAAU,mBAAmB,CAAwC,OAI1E;IACA,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,GAAG,CAAC,kBAAkB,EAAE,CAAA;IAC3D,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAA;IACnC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAE,CAAC,CAAC,CAAA;IAC5F,OAAO,KAAK,IAAI,OAAO,CAAC,YAAY,CAAA;AACrC,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAwC,OAK9E;IACA,OAAO,iBAAiB,CACvB;QACC,KAAK,EAAE,CAAC,KAAoB,EAAE,EAAE;YAC/B,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACpB,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,YAAsB,CAAC,CAAA;gBAC/C,OAAM;YACP,CAAC;YAED,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;gBACnC,IAAI,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAE,CAAC,EAAE,CAAC;oBAC3C,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;oBACrB,MAAK;gBACN,CAAC;YACF,CAAC;QACF,CAAC;KACD,EACD,OAAO,CAAC,OAAO,CACf,CAAA;AACF,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
* @returns The matching theme key, or defaultTheme if no match found
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const themes = {
|
|
13
|
+
* light: 'light',
|
|
14
|
+
* dark: 'dark',
|
|
15
|
+
* system: 'system'
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* // Get theme from data-theme attribute
|
|
19
|
+
* const theme = getThemeByDataAttribute({
|
|
20
|
+
* themes,
|
|
21
|
+
* defaultTheme: 'system',
|
|
22
|
+
* attributeName: 'data-theme'
|
|
23
|
+
* })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function getThemeByDataAttribute<Themes extends Record<string, string>>(options: {
|
|
27
|
+
themes: Themes;
|
|
28
|
+
defaultTheme?: keyof Themes | undefined;
|
|
29
|
+
attributeName: `data-${string}`;
|
|
30
|
+
element?: Element | undefined;
|
|
31
|
+
}): keyof Themes | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Observes changes to a theme data attribute and calls a handler when it changes.
|
|
34
|
+
*
|
|
35
|
+
* @param options - Configuration options
|
|
36
|
+
* @param options.themes - Record mapping theme keys to their data attribute values
|
|
37
|
+
* @param options.handler - Callback function called with the new theme value or null when removed
|
|
38
|
+
* @param options.defaultTheme - Fallback theme key if attribute value doesn't match any theme
|
|
39
|
+
* @param options.attributeName - Name of the data attribute to observe (must start with 'data-')
|
|
40
|
+
* @returns A MutationObserver that can be disconnected to stop observing
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const themes = {
|
|
45
|
+
* light: 'light',
|
|
46
|
+
* dark: 'dark'
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* // Observe data-theme attribute changes
|
|
50
|
+
* const observer = observeThemeByDataAttributes({
|
|
51
|
+
* themes,
|
|
52
|
+
* handler: (theme) => console.log('Theme changed to:', theme),
|
|
53
|
+
* defaultTheme: 'light',
|
|
54
|
+
* attributeName: 'data-theme'
|
|
55
|
+
* })
|
|
56
|
+
*
|
|
57
|
+
* // Stop observing
|
|
58
|
+
* observer.disconnect()
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function observeThemeByDataAttributes<Themes extends Record<string, string>>(options: {
|
|
62
|
+
attributeName: `data-${string}`;
|
|
63
|
+
themes: Themes;
|
|
64
|
+
handler: (value: string | null) => void;
|
|
65
|
+
defaultTheme?: keyof Themes | undefined;
|
|
66
|
+
element?: Element | undefined;
|
|
67
|
+
}): MutationObserver;
|
|
68
|
+
//# sourceMappingURL=data-attribute.d.ts.map
|
|
@@ -0,0 +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,QAAQ,MAAM,EAAE,CAAA;IAC/B,MAAM,EAAE,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,oBAmBA"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { findKey } from 'type-plus';
|
|
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
|
+
export function getThemeByDataAttribute(options) {
|
|
29
|
+
const value = getDataAttribute(options.attributeName, options.element);
|
|
30
|
+
const theme = findKey(options.themes, (theme) => options.themes[theme] === value);
|
|
31
|
+
return theme ?? options.defaultTheme;
|
|
32
|
+
}
|
|
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
|
+
export function observeThemeByDataAttributes(options) {
|
|
63
|
+
return observeDataAttributes({
|
|
64
|
+
[options.attributeName]: (value) => {
|
|
65
|
+
if (value === null) {
|
|
66
|
+
options.handler(options.defaultTheme ?? null);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
for (const name in options.themes) {
|
|
70
|
+
if (options.themes[name] === value) {
|
|
71
|
+
options.handler(name);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
}, options.element);
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=data-attribute.js.map
|
|
@@ -0,0 +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,gBAAgB,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;IAEtE,MAAM,KAAK,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,CAAA;AACrC,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,4BAA4B,CAAwC,OAMnF;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,MAAK;gBACN,CAAC;YACF,CAAC;QACF,CAAC;KACD,EACD,OAAO,CAAC,OAAO,CACf,CAAA;AACF,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
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): T | 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;
|
|
37
|
+
//# sourceMappingURL=attribute.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ctx } from "../globals.ctx.js";
|
|
2
|
+
/**
|
|
3
|
+
* Gets the value of an attribute from an element.
|
|
4
|
+
*
|
|
5
|
+
* @param qualifiedName - The name of the attribute to get
|
|
6
|
+
* @param element - The element to get the attribute from. Defaults to `document.documentElement`
|
|
7
|
+
* @returns The attribute value cast to type T, or null if the attribute doesn't exist
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* // Get theme from document root
|
|
12
|
+
* const theme = getAttribute('data-theme')
|
|
13
|
+
*
|
|
14
|
+
* // Get data-testid from a specific element
|
|
15
|
+
* const testId = getAttribute('data-testid', element)
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function getAttribute(qualifiedName, element = ctx.getDocumentElement()) {
|
|
19
|
+
return element?.getAttribute(qualifiedName);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Observes attributes changes on an element and calls corresponding handlers.
|
|
23
|
+
*
|
|
24
|
+
* @param handlers - An object mapping attribute names to handler functions.
|
|
25
|
+
* @param element - The element to observe. Defaults to `document.documentElement`.
|
|
26
|
+
* @returns {MutationObserver} The observer instance, which can be used to disconnect the observer.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const observer = observeAttributes({
|
|
31
|
+
* 'data-theme': (attr, value) => console.log(`Theme changed to: ${value}`),
|
|
32
|
+
* 'class': (attr, value) => console.log(`class changed to: ${value}`)
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Later, to stop observing:
|
|
36
|
+
* observer.disconnect();
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function observeAttributes(handlers, element = ctx.getDocumentElement()) {
|
|
40
|
+
const observer = new MutationObserver((mutations) => {
|
|
41
|
+
for (const mutation of mutations) {
|
|
42
|
+
const attribute = mutation.attributeName;
|
|
43
|
+
const value = element.getAttribute(attribute);
|
|
44
|
+
handlers[attribute]?.(value);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
observer.observe(element, {
|
|
48
|
+
attributes: true,
|
|
49
|
+
attributeFilter: Object.keys(handlers),
|
|
50
|
+
});
|
|
51
|
+
return observer;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=attribute.js.map
|
|
@@ -0,0 +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+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"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare function getDataAttribute<T extends `data-${string}`>(qualifiedName: T, element?: Element | undefined): T | 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;
|
|
24
|
+
//# sourceMappingURL=data-attribute.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ctx } from "../globals.ctx.js";
|
|
2
|
+
import { getAttribute, observeAttributes } from "./attribute.js";
|
|
3
|
+
export function getDataAttribute(qualifiedName, element = ctx.getDocumentElement()) {
|
|
4
|
+
return getAttribute(qualifiedName, element);
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Observes changes to `data-*` attributes on an element and calls corresponding handlers.
|
|
8
|
+
*
|
|
9
|
+
* @param options - Configuration options
|
|
10
|
+
* @param options.handlers - An object mapping `data-*` attribute names to handler functions.
|
|
11
|
+
* @param options.element - The element to observe. Defaults to `document.documentElement`
|
|
12
|
+
* @returns {MutationObserver} The observer instance, which can be used to disconnect the observer
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const observer = observeDataAttributes({
|
|
17
|
+
* handlers: {
|
|
18
|
+
* 'data-theme': (value) => console.log(`Theme changed to: ${value}`),
|
|
19
|
+
* 'data-mode': (value) => console.log(`Mode changed to: ${value}`)
|
|
20
|
+
* }
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Later, to stop observing:
|
|
24
|
+
* observer.disconnect();
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function observeDataAttributes(handlers, element) {
|
|
28
|
+
return observeAttributes(handlers, element);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=data-attribute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-attribute.js","sourceRoot":"","sources":["../../src/utils/data-attribute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAEhE,MAAM,UAAU,gBAAgB,CAC/B,aAAgB,EAChB,UAA+B,GAAG,CAAC,kBAAkB,EAAE;IAEvD,OAAO,YAAY,CAAC,aAAa,EAAE,OAAO,CAAC,CAAA;AAC5C,CAAC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,qBAAqB,CACpC,QAA8C,EAC9C,OAA6B;IAE7B,OAAO,iBAAiB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;AAC5C,CAAC"}
|