@seahax/elemental 0.5.20 → 0.5.22

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/README.md CHANGED
@@ -30,10 +30,12 @@ import {
30
30
  useRoute,
31
31
  useAsync,
32
32
  useEffect,
33
+ useStyleEffect,
33
34
  useChildEffect,
34
35
  useDisconnectCallback,
35
36
  useElementInternals,
36
37
  useHost,
38
+ useShadow,
37
39
  } from '@seahax/elemental';
38
40
 
39
41
  export const MyComponent = defineComponent((shadow) => {
@@ -109,6 +111,22 @@ export const MyComponent = defineComponent((shadow) => {
109
111
  };
110
112
  });
111
113
 
114
+ // React to reference changes and apply CSS styles to the shadow root.
115
+ useStyleEffect([
116
+ // dependency references
117
+ localStateRef
118
+ ], (localState) => {
119
+ // Reactive code runs when the component is connected to the document,
120
+ // and when any of the dependencies change.
121
+
122
+ // Return a CSS string to be adopted by the shadow root.
123
+ return /* css */ `
124
+ .hello {
125
+ color: ${localState};
126
+ }
127
+ `;
128
+ });
129
+
112
130
  // React to child list changes.
113
131
  useChildEffect(() => {
114
132
  // Reactive code runs when the component is connected to the document,
@@ -1 +1,2 @@
1
+ /** Use the component shadow root. */
1
2
  export declare function useShadow(): ShadowRoot;
@@ -0,0 +1,9 @@
1
+ import { useInternalController as e } from "./useInternalController.js";
2
+ //#region src/hooks/useShadow.ts
3
+ function t() {
4
+ return e().shadow;
5
+ }
6
+ //#endregion
7
+ export { t as useShadow };
8
+
9
+ //# sourceMappingURL=useShadow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useShadow.js","names":[],"sources":["../../src/hooks/useShadow.ts"],"sourcesContent":["import { useInternalController } from './useInternalController.ts';\n\n/** Use the component shadow root. */\nexport function useShadow(): ShadowRoot {\n return useInternalController().shadow;\n}\n"],"mappings":";;AAGA,SAAgB,IAAwB;AACtC,QAAO,GAAuB,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { ReadonlyRef, RefValues } from './useRef.ts';
2
+ /**
3
+ * Add a stylesheet to the component shadow root (`adoptedStyleSheets`) and
4
+ * apply the style text returned by the callback. The stylesheet will be
5
+ * replaced when the dependencies change.
6
+ */
7
+ export declare function useStyleEffect<TDeps extends ReadonlyRef<any>[]>(dependencies: TDeps, callback: (...deps: RefValues<TDeps>) => string): void;
@@ -0,0 +1,16 @@
1
+ import { useEffect as e } from "./useEffect.js";
2
+ import { useShadow as t } from "./useShadow.js";
3
+ //#region src/hooks/useStyleEffect.ts
4
+ function n(n, r) {
5
+ e(n, (...e) => {
6
+ let n = t(), i = new CSSStyleSheet(), a = r(...e);
7
+ return i.replaceSync(a), n.adoptedStyleSheets.push(i), () => {
8
+ let e = n.adoptedStyleSheets.indexOf(i);
9
+ e !== -1 && n.adoptedStyleSheets.splice(e, 1);
10
+ };
11
+ });
12
+ }
13
+ //#endregion
14
+ export { n as useStyleEffect };
15
+
16
+ //# sourceMappingURL=useStyleEffect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStyleEffect.js","names":[],"sources":["../../src/hooks/useStyleEffect.ts"],"sourcesContent":["import { useEffect } from './useEffect.ts';\nimport type { ReadonlyRef, RefValues } from './useRef.ts';\nimport { useShadow } from './useShadow.ts';\n\n/**\n * Add a stylesheet to the component shadow root (`adoptedStyleSheets`) and\n * apply the style text returned by the callback. The stylesheet will be\n * replaced when the dependencies change.\n */\nexport function useStyleEffect<TDeps extends ReadonlyRef<any>[]>(\n dependencies: TDeps,\n callback: (...deps: RefValues<TDeps>) => string,\n): void {\n useEffect(dependencies, (...args) => {\n const shadow = useShadow();\n const sheet = new CSSStyleSheet();\n const styleText = callback(...args);\n\n sheet.replaceSync(styleText);\n shadow.adoptedStyleSheets.push(sheet);\n\n return () => {\n const index = shadow.adoptedStyleSheets.indexOf(sheet);\n if (index !== -1) shadow.adoptedStyleSheets.splice(index, 1);\n };\n });\n}\n"],"mappings":";;;AASA,SAAgB,EACd,GACA,GACM;AACN,GAAU,IAAe,GAAG,MAAS;EACnC,IAAM,IAAS,GAAW,EACpB,IAAQ,IAAI,eAAe,EAC3B,IAAY,EAAS,GAAG,EAAK;AAKnC,SAHA,EAAM,YAAY,EAAU,EAC5B,EAAO,mBAAmB,KAAK,EAAM,QAExB;GACX,IAAM,IAAQ,EAAO,mBAAmB,QAAQ,EAAM;AACtD,GAAI,MAAU,MAAI,EAAO,mBAAmB,OAAO,GAAO,EAAE;;GAE9D"}
package/dist/index.d.ts CHANGED
@@ -14,6 +14,8 @@ export * from './hooks/useHost.ts';
14
14
  export * from './hooks/useParent.ts';
15
15
  export * from './hooks/useRef.ts';
16
16
  export * from './hooks/useRoute.ts';
17
+ export * from './hooks/useShadow.ts';
17
18
  export * from './hooks/useStore.ts';
19
+ export * from './hooks/useStyleEffect.ts';
18
20
  export * from './html.ts';
19
21
  export { html as h } from './html.ts';
package/dist/index.js CHANGED
@@ -15,5 +15,7 @@ import { useForm as p, useFormDisabled as m, useFormResetCallback as h, useFormR
15
15
  import { useParent as _ } from "./hooks/useParent.js";
16
16
  import { useStore as v } from "./hooks/useStore.js";
17
17
  import { useRoute as y } from "./hooks/useRoute.js";
18
- import { html as b } from "./html.js";
19
- export { e as classes, t as createStore, r as defineComponent, i as extendComponentDefinition, b as h, b as html, s as useAsync, l as useAttributes, u as useChildEffect, a as useDisconnectCallback, d as useDocument, o as useEffect, f as useElementInternals, p as useForm, m as useFormDisabled, h as useFormResetCallback, g as useFormRestoreCallback, c as useHost, _ as useParent, n as useRef, y as useRoute, v as useStore };
18
+ import { useShadow as b } from "./hooks/useShadow.js";
19
+ import { useStyleEffect as x } from "./hooks/useStyleEffect.js";
20
+ import { html as S } from "./html.js";
21
+ export { e as classes, t as createStore, r as defineComponent, i as extendComponentDefinition, S as h, S as html, s as useAsync, l as useAttributes, u as useChildEffect, a as useDisconnectCallback, d as useDocument, o as useEffect, f as useElementInternals, p as useForm, m as useFormDisabled, h as useFormResetCallback, g as useFormRestoreCallback, c as useHost, _ as useParent, n as useRef, y as useRoute, b as useShadow, v as useStore, x as useStyleEffect };
package/package.json CHANGED
@@ -21,5 +21,5 @@
21
21
  "publishConfig": {
22
22
  "access": "public"
23
23
  },
24
- "version": "0.5.20"
24
+ "version": "0.5.22"
25
25
  }