@seahax/elemental 0.4.0 → 0.4.2
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 +23 -6
- package/dist/hooks/effect.d.ts +6 -0
- package/dist/hooks/effect.js +4 -1
- package/dist/hooks/effect.js.map +1 -1
- package/dist/index.js +11 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Functional, reactive, web component base library.
|
|
4
4
|
|
|
5
|
-
Contains everything you need to build anything from a single component up to a full reactive application, with
|
|
5
|
+
Contains everything you need to build anything from a single component up to a full reactive application, with minimal overhead.
|
|
6
6
|
|
|
7
|
-
- [](https://bundlejs.com/?q=%40seahax%2Felemental%40latest&treeshake=%5B*%5D)
|
|
8
7
|
- Create fully portable web components
|
|
9
8
|
- Direct and safe access to the DOM (no virtual DOM)
|
|
10
9
|
- React-style list rendering using unique keys
|
|
@@ -13,6 +12,11 @@ Contains everything you need to build anything from a single component up to a f
|
|
|
13
12
|
- Client-side routing
|
|
14
13
|
- No Build Tooling
|
|
15
14
|
- No Dependencies
|
|
15
|
+
- Tiny Bundle Size
|
|
16
|
+
|
|
17
|
+
[](https://www.npmjs.com/package/@seahax/elemental) [)
|
|
18
|
+
](https://bundlejs.com/?q=%40seahax%2Felemental%40latest&treeshake=%5B*%5D)
|
|
19
|
+
|
|
16
20
|
|
|
17
21
|
## Define A Web Component
|
|
18
22
|
|
|
@@ -26,13 +30,20 @@ import {
|
|
|
26
30
|
useLoading,
|
|
27
31
|
useEffect,
|
|
28
32
|
useChildEffect,
|
|
33
|
+
useDisconnectEffect,
|
|
29
34
|
useHost,
|
|
30
35
|
h,
|
|
31
36
|
} from '@seahax/elemental';
|
|
32
37
|
|
|
33
38
|
export const MyComponent = defineComponent((shadow) => {
|
|
34
|
-
// This function is
|
|
35
|
-
//
|
|
39
|
+
// This function is called every time the component is connected to the
|
|
40
|
+
// document.
|
|
41
|
+
//
|
|
42
|
+
// For the most part, a component's lifecycle should be handled as if it
|
|
43
|
+
// starts on connect and ends on disconnect, even though the component
|
|
44
|
+
// can be reconnected to the document. Restore internal state on
|
|
45
|
+
// connection from host attributes and properties. Effect hooks are
|
|
46
|
+
// designed to facilitate this pattern.
|
|
36
47
|
|
|
37
48
|
// Create HTML elements and save references to them.
|
|
38
49
|
const myInput = h('input');
|
|
@@ -87,8 +98,9 @@ export const MyComponent = defineComponent((shadow) => {
|
|
|
87
98
|
// and when any of the dependencies change.
|
|
88
99
|
|
|
89
100
|
return () => {
|
|
90
|
-
// Cleanup
|
|
91
|
-
// is disconnected from the
|
|
101
|
+
// Cleanup after dependency refs are changed (before the next effect
|
|
102
|
+
// callback) and after the component is disconnected from the
|
|
103
|
+
// document.
|
|
92
104
|
};
|
|
93
105
|
});
|
|
94
106
|
|
|
@@ -104,6 +116,11 @@ export const MyComponent = defineComponent((shadow) => {
|
|
|
104
116
|
};
|
|
105
117
|
});
|
|
106
118
|
|
|
119
|
+
useDisconnectEffect(() => {
|
|
120
|
+
// Reactive code runs when the component is disconnected from the
|
|
121
|
+
// document.
|
|
122
|
+
});
|
|
123
|
+
|
|
107
124
|
// Use the host element (generally only useful in reusable hooks).
|
|
108
125
|
const host = useHost();
|
|
109
126
|
});
|
package/dist/hooks/effect.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
import type { ReadonlyRef, RefValues } from './ref.ts';
|
|
2
2
|
/** React to observable (reference) changes. */
|
|
3
3
|
export declare function useEffect<const TDeps extends readonly ReadonlyRef<any>[]>(deps: TDeps, callback: (...values: RefValues<TDeps>) => (() => void) | void): void;
|
|
4
|
+
/**
|
|
5
|
+
* React to document disconnection.
|
|
6
|
+
*
|
|
7
|
+
* Alias for: `useEffect([], () => callback)`
|
|
8
|
+
*/
|
|
9
|
+
export declare function useDisconnectEffect(callback: () => void): void;
|
package/dist/hooks/effect.js
CHANGED
|
@@ -11,7 +11,10 @@ function n(n, r) {
|
|
|
11
11
|
t && o.push(() => t());
|
|
12
12
|
}), a.push(s);
|
|
13
13
|
}
|
|
14
|
+
function r(e) {
|
|
15
|
+
n([], () => e);
|
|
16
|
+
}
|
|
14
17
|
//#endregion
|
|
15
|
-
export { n as useEffect };
|
|
18
|
+
export { r as useDisconnectEffect, n as useEffect };
|
|
16
19
|
|
|
17
20
|
//# sourceMappingURL=effect.js.map
|
package/dist/hooks/effect.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effect.js","names":[],"sources":["../../src/hooks/effect.ts"],"sourcesContent":["import { createCallbacks } from '../internal/callbacks.ts';\nimport { useContext } from './context.ts';\nimport type { ReadonlyRef, RefValues } from './ref.ts';\n\n/** React to observable (reference) changes. */\nexport function useEffect<const TDeps extends readonly ReadonlyRef<any>[]>(\n deps: TDeps,\n callback: (...values: RefValues<TDeps>) => (() => void) | void,\n): void {\n const { onNotify, onDisconnect } = useContext();\n const cleanupCallback = createCallbacks();\n const cleanup = (): void => cleanupCallback.runAndClear();\n let values: any[] | undefined;\n\n onNotify.push((): void => {\n const newValues = deps.map((dep) => dep.value);\n if (values?.length === newValues.length && values?.every((value, i) => value === newValues[i])) return;\n values = newValues;\n cleanup();\n const maybeCleanup = callback(...(values as any));\n if (maybeCleanup) cleanupCallback.push(() => maybeCleanup());\n });\n\n onDisconnect.push(cleanup);\n}\n"],"mappings":";;;AAKA,SAAgB,EACd,GACA,GACM;CACN,IAAM,EAAE,aAAU,oBAAiB,GAAY,EACzC,IAAkB,GAAiB,EACnC,UAAsB,EAAgB,aAAa,EACrD;AAWJ,CATA,EAAS,WAAiB;EACxB,IAAM,IAAY,EAAK,KAAK,MAAQ,EAAI,MAAM;AAC9C,MAAI,GAAQ,WAAW,EAAU,UAAU,GAAQ,OAAO,GAAO,MAAM,MAAU,EAAU,GAAG,CAAE;AAEhG,EADA,IAAS,GACT,GAAS;EACT,IAAM,IAAe,EAAS,GAAI,EAAe;AACjD,EAAI,KAAc,EAAgB,WAAW,GAAc,CAAC;GAC5D,EAEF,EAAa,KAAK,EAAQ"}
|
|
1
|
+
{"version":3,"file":"effect.js","names":[],"sources":["../../src/hooks/effect.ts"],"sourcesContent":["import { createCallbacks } from '../internal/callbacks.ts';\nimport { useContext } from './context.ts';\nimport type { ReadonlyRef, RefValues } from './ref.ts';\n\n/** React to observable (reference) changes. */\nexport function useEffect<const TDeps extends readonly ReadonlyRef<any>[]>(\n deps: TDeps,\n callback: (...values: RefValues<TDeps>) => (() => void) | void,\n): void {\n const { onNotify, onDisconnect } = useContext();\n const cleanupCallback = createCallbacks();\n const cleanup = (): void => cleanupCallback.runAndClear();\n let values: any[] | undefined;\n\n onNotify.push((): void => {\n const newValues = deps.map((dep) => dep.value);\n if (values?.length === newValues.length && values?.every((value, i) => value === newValues[i])) return;\n values = newValues;\n cleanup();\n const maybeCleanup = callback(...(values as any));\n if (maybeCleanup) cleanupCallback.push(() => maybeCleanup());\n });\n\n onDisconnect.push(cleanup);\n}\n\n/**\n * React to document disconnection.\n *\n * Alias for: `useEffect([], () => callback)`\n */\nexport function useDisconnectEffect(callback: () => void): void {\n useEffect([], () => callback);\n}\n"],"mappings":";;;AAKA,SAAgB,EACd,GACA,GACM;CACN,IAAM,EAAE,aAAU,oBAAiB,GAAY,EACzC,IAAkB,GAAiB,EACnC,UAAsB,EAAgB,aAAa,EACrD;AAWJ,CATA,EAAS,WAAiB;EACxB,IAAM,IAAY,EAAK,KAAK,MAAQ,EAAI,MAAM;AAC9C,MAAI,GAAQ,WAAW,EAAU,UAAU,GAAQ,OAAO,GAAO,MAAM,MAAU,EAAU,GAAG,CAAE;AAEhG,EADA,IAAS,GACT,GAAS;EACT,IAAM,IAAe,EAAS,GAAI,EAAe;AACjD,EAAI,KAAc,EAAgB,WAAW,GAAc,CAAC;GAC5D,EAEF,EAAa,KAAK,EAAQ;;AAQ5B,SAAgB,EAAoB,GAA4B;AAC9D,GAAU,EAAE,QAAQ,EAAS"}
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { useRef as e } from "./hooks/ref.js";
|
|
2
2
|
import { defineComponent as t } from "./component.js";
|
|
3
|
-
import {
|
|
4
|
-
import { useHost as
|
|
5
|
-
import { useAttributes as
|
|
6
|
-
import { useChildEffect as
|
|
7
|
-
import { useLoading as
|
|
8
|
-
import { createStore as
|
|
9
|
-
import { getRouter as
|
|
10
|
-
import { useStore as
|
|
11
|
-
import { useRoute as
|
|
12
|
-
import { html as
|
|
13
|
-
export {
|
|
3
|
+
import { useDisconnectEffect as n, useEffect as r } from "./hooks/effect.js";
|
|
4
|
+
import { useHost as i } from "./hooks/host.js";
|
|
5
|
+
import { useAttributes as a } from "./hooks/attributes.js";
|
|
6
|
+
import { useChildEffect as o } from "./hooks/child-effect.js";
|
|
7
|
+
import { useLoading as s } from "./hooks/loading.js";
|
|
8
|
+
import { createStore as c } from "./store.js";
|
|
9
|
+
import { getRouter as l } from "./router.js";
|
|
10
|
+
import { useStore as u } from "./hooks/store.js";
|
|
11
|
+
import { useRoute as d } from "./hooks/route.js";
|
|
12
|
+
import { html as f } from "./html.js";
|
|
13
|
+
export { c as createStore, t as defineComponent, l as getRouter, f as h, f as html, a as useAttributes, o as useChildEffect, n as useDisconnectEffect, r as useEffect, i as useHost, s as useLoading, e as useRef, d as useRoute, u as useStore };
|