elements-kit 0.0.2 → 0.0.3

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,82 @@
1
+ import { a as effect, f as signal, o as effectScope } from "../../signals-Cr7xgAJH.mjs";
2
+ import { useEffect, useMemo, useRef, useSyncExternalStore } from "react";
3
+ //#region src/signals/lib/react.ts
4
+ /**
5
+ * Subscribe to any readable signal — writable or computed — returning its current value.
6
+ *
7
+ * Accepts any zero-argument callable `() => T`, which includes both `Signal<T>` and
8
+ * `Computed<T>`. Using `() => T` instead of `Computed<T>` prevents TypeScript from
9
+ * picking the write overload of `Signal<T>` during type inference.
10
+ *
11
+ * @template T - The type of the signal value.
12
+ * @param value - A writable `Signal<T>` or a derived `Computed<T>`.
13
+ * @returns The current value, updated on every signal change.
14
+ *
15
+ * @example
16
+ * ```tsx
17
+ * const count = signal(0);
18
+ * const double = computed(() => count() * 2);
19
+ *
20
+ * function Display() {
21
+ * const countValue = useSignal(count);
22
+ * const doubleValue = useSignal(double);
23
+ * return <div>{countValue} × 2 = {doubleValue}</div>;
24
+ * }
25
+ * ```
26
+ */
27
+ function useSignal(value) {
28
+ return useSyncExternalStore((callback) => effect(() => {
29
+ value();
30
+ callback();
31
+ }), () => value(), () => value());
32
+ }
33
+ /**
34
+ * Create a signal effect scope tied to a React component's lifetime.
35
+ *
36
+ * All effects registered inside `callback` are grouped into a single scope. The scope — and every effect within it — is automatically stopped when the component unmounts.
37
+ *
38
+ * If your callback returns a `Computed<T>` signal, the hook will always return its current value, updating reactively as dependencies change. If your callback returns `void`, the value will be `undefined`.
39
+ *
40
+ * Returns the current value of the computed signal (or `undefined`).
41
+ *
42
+ * Use this when you want to create multiple related effects at once without individually managing each one's lifecycle. All effects and cleanups inside the callback are automatically cleaned up on unmount.
43
+ *
44
+ * @template T - The type of the computed value (if any).
45
+ * @param callback - A function that registers one or more signal effects, optionally returning a `Computed<T>`.
46
+ * @returns `value` — the current value of the computed signal (or `undefined`).
47
+ *
48
+ * @example
49
+ * ```tsx
50
+ * function Analytics() {
51
+ * useScope(() => {
52
+ * effect(() => console.log("page:", currentPage()));
53
+ * effect(() => console.log("user:", currentUser()));
54
+ * });
55
+ * return null;
56
+ * }
57
+ *
58
+ * // With computed value:
59
+ * function DoubleCounter() {
60
+ * const double = useScope(() => computed(() => count() * 2));
61
+ * return <div>{double}</div>;
62
+ * }
63
+ * ```
64
+ */
65
+ function useScope(callback) {
66
+ const computedRef = useRef(void 0);
67
+ const stopScope = useMemo(() => {
68
+ return effectScope(() => {
69
+ computedRef.current = callback();
70
+ });
71
+ }, [callback]);
72
+ const value = useSignal(computedRef.current ?? fallbackSignal);
73
+ useEffect(() => {
74
+ return () => {
75
+ stopScope();
76
+ };
77
+ }, [stopScope]);
78
+ return value;
79
+ }
80
+ const fallbackSignal = signal(void 0);
81
+ //#endregion
82
+ export { useScope, useSignal };