@effect/atom-react 4.0.0-beta.7 → 4.0.0-beta.70
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/dist/Hooks.d.ts +53 -13
- package/dist/Hooks.d.ts.map +1 -1
- package/dist/Hooks.js +64 -12
- package/dist/Hooks.js.map +1 -1
- package/dist/ReactHydration.d.ts +14 -2
- package/dist/ReactHydration.d.ts.map +1 -1
- package/dist/ReactHydration.js +32 -2
- package/dist/ReactHydration.js.map +1 -1
- package/dist/RegistryContext.d.ts +13 -3
- package/dist/RegistryContext.d.ts.map +1 -1
- package/dist/RegistryContext.js +36 -4
- package/dist/RegistryContext.js.map +1 -1
- package/dist/ScopedAtom.d.ts +25 -25
- package/dist/ScopedAtom.d.ts.map +1 -1
- package/dist/ScopedAtom.js +30 -13
- package/dist/ScopedAtom.js.map +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -5
- package/package.json +9 -9
- package/src/Hooks.ts +72 -14
- package/src/ReactHydration.ts +36 -3
- package/src/RegistryContext.ts +36 -4
- package/src/ScopedAtom.ts +43 -26
- package/src/index.ts +5 -5
package/src/ScopedAtom.ts
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `ScopedAtom` module provides a small React integration for creating atom
|
|
3
|
+
* instances that are scoped to a component subtree. A scoped atom bundles a
|
|
4
|
+
* React provider, context, and `use` accessor so each mounted provider owns its
|
|
5
|
+
* own atom instance instead of sharing a single module-level atom.
|
|
6
|
+
*
|
|
7
|
+
* Use `ScopedAtom` when an atom needs to be isolated per feature, route,
|
|
8
|
+
* component instance, test harness, or provider input. The provider may receive
|
|
9
|
+
* an initial `value` that is passed to the atom factory, making it useful for
|
|
10
|
+
* state that should be seeded from React props while still being consumed by the
|
|
11
|
+
* atom hooks in descendants.
|
|
12
|
+
*
|
|
13
|
+
* **Gotchas**
|
|
14
|
+
*
|
|
15
|
+
* - `use` must be called under the matching provider or it throws.
|
|
16
|
+
* - The provider creates the atom once for its lifetime; changing the provider
|
|
17
|
+
* `value` prop after mount does not recreate the atom.
|
|
18
|
+
*
|
|
19
|
+
* @since 4.0.0
|
|
3
20
|
*/
|
|
4
21
|
"use client"
|
|
5
22
|
|
|
@@ -7,39 +24,36 @@ import type * as Atom from "effect/unstable/reactivity/Atom"
|
|
|
7
24
|
import * as React from "react"
|
|
8
25
|
|
|
9
26
|
/**
|
|
10
|
-
* @since 1.0.0
|
|
11
|
-
* @category Type IDs
|
|
12
|
-
*
|
|
13
27
|
* Type identifier for ScopedAtom.
|
|
28
|
+
*
|
|
29
|
+
* @category type IDs
|
|
30
|
+
* @since 4.0.0
|
|
14
31
|
*/
|
|
15
32
|
export type TypeId = "~@effect/atom-react/ScopedAtom"
|
|
16
33
|
|
|
17
34
|
/**
|
|
18
|
-
* @since 1.0.0
|
|
19
|
-
* @category Type IDs
|
|
20
|
-
*
|
|
21
35
|
* Type identifier for ScopedAtom.
|
|
36
|
+
*
|
|
37
|
+
* @category type IDs
|
|
38
|
+
* @since 4.0.0
|
|
22
39
|
*/
|
|
23
40
|
export const TypeId: TypeId = "~@effect/atom-react/ScopedAtom"
|
|
24
41
|
|
|
25
42
|
/**
|
|
26
|
-
* @since 1.0.0
|
|
27
|
-
* @category models
|
|
28
|
-
*
|
|
29
43
|
* Scoped Atom interface with a provider-backed instance.
|
|
30
44
|
*
|
|
31
|
-
*
|
|
45
|
+
* **Example** (Providing and reading a scoped atom)
|
|
46
|
+
*
|
|
32
47
|
* ```ts
|
|
33
|
-
* import * as
|
|
48
|
+
* import * as AtomReact from "@effect/atom-react"
|
|
49
|
+
* import { Atom } from "effect/unstable/reactivity"
|
|
34
50
|
* import * as React from "react"
|
|
35
|
-
* import * as ScopedAtom from "@effect/atom-react/ScopedAtom"
|
|
36
|
-
* import { useAtomValue } from "@effect/atom-react"
|
|
37
51
|
*
|
|
38
|
-
* const Counter =
|
|
52
|
+
* const Counter = AtomReact.make(() => Atom.make(0))
|
|
39
53
|
*
|
|
40
54
|
* function View() {
|
|
41
55
|
* const atom = Counter.use()
|
|
42
|
-
* const value = useAtomValue(atom)
|
|
56
|
+
* const value = AtomReact.useAtomValue(atom)
|
|
43
57
|
* return React.createElement("div", null, value)
|
|
44
58
|
* }
|
|
45
59
|
*
|
|
@@ -47,33 +61,33 @@ export const TypeId: TypeId = "~@effect/atom-react/ScopedAtom"
|
|
|
47
61
|
* return React.createElement(Counter.Provider, null, React.createElement(View))
|
|
48
62
|
* }
|
|
49
63
|
* ```
|
|
64
|
+
*
|
|
65
|
+
* @category models
|
|
66
|
+
* @since 4.0.0
|
|
50
67
|
*/
|
|
51
68
|
export interface ScopedAtom<A extends Atom.Atom<any>, Input = never> {
|
|
52
69
|
readonly [TypeId]: TypeId
|
|
53
70
|
use(): A
|
|
54
|
-
Provider: Input extends never ? React.FC<{ readonly children?: React.ReactNode | undefined }>
|
|
71
|
+
Provider: [Input] extends [never] ? React.FC<{ readonly children?: React.ReactNode | undefined }>
|
|
55
72
|
: React.FC<{ readonly children?: React.ReactNode | undefined; readonly value: Input }>
|
|
56
73
|
Context: React.Context<A>
|
|
57
74
|
}
|
|
58
75
|
|
|
59
76
|
/**
|
|
60
|
-
* @since 1.0.0
|
|
61
|
-
* @category constructors
|
|
62
|
-
*
|
|
63
77
|
* Creates a ScopedAtom from a factory function.
|
|
64
78
|
*
|
|
65
|
-
*
|
|
79
|
+
* **Example** (Creating a scoped atom with input)
|
|
80
|
+
*
|
|
66
81
|
* ```ts
|
|
67
|
-
* import * as
|
|
82
|
+
* import * as AtomReact from "@effect/atom-react"
|
|
83
|
+
* import { Atom } from "effect/unstable/reactivity"
|
|
68
84
|
* import * as React from "react"
|
|
69
|
-
* import * as ScopedAtom from "@effect/atom-react/ScopedAtom"
|
|
70
|
-
* import { useAtomValue } from "@effect/atom-react"
|
|
71
85
|
*
|
|
72
|
-
* const User =
|
|
86
|
+
* const User = AtomReact.make((name: string) => Atom.make(name))
|
|
73
87
|
*
|
|
74
88
|
* function UserName() {
|
|
75
89
|
* const atom = User.use()
|
|
76
|
-
* const value = useAtomValue(atom)
|
|
90
|
+
* const value = AtomReact.useAtomValue(atom)
|
|
77
91
|
* return React.createElement("span", null, value)
|
|
78
92
|
* }
|
|
79
93
|
*
|
|
@@ -85,6 +99,9 @@ export interface ScopedAtom<A extends Atom.Atom<any>, Input = never> {
|
|
|
85
99
|
* )
|
|
86
100
|
* }
|
|
87
101
|
* ```
|
|
102
|
+
*
|
|
103
|
+
* @category constructors
|
|
104
|
+
* @since 4.0.0
|
|
88
105
|
*/
|
|
89
106
|
export const make = <A extends Atom.Atom<any>, Input = never>(
|
|
90
107
|
f: (() => A) | ((input: Input) => A)
|
package/src/index.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* @since
|
|
6
|
+
* @since 4.0.0
|
|
7
7
|
*/
|
|
8
8
|
export * from "./Hooks.ts"
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* @since
|
|
11
|
+
* @since 4.0.0
|
|
12
12
|
*/
|
|
13
13
|
export * from "./RegistryContext.ts"
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* @since
|
|
16
|
+
* @since 4.0.0
|
|
17
17
|
*/
|
|
18
18
|
export * from "./ReactHydration.ts"
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* @since
|
|
21
|
+
* @since 4.0.0
|
|
22
22
|
*/
|
|
23
23
|
export * from "./ScopedAtom.ts"
|