autosuspense 0.2.0 → 1.0.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.
Files changed (2) hide show
  1. package/README.md +95 -21
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,13 +1,49 @@
1
1
  # AutoSupense Beta
2
- AutoSuspense is a small React utility that automatically builds and composes
3
- Suspense fallback UI based on your component tree without manually wiring
4
- nested fallback components.
2
+ AutoSuspense is a lightweight React utility that automatically composes Suspense fallback UI based on your component tree without manually wiring nested ```<Suspense>``` boundaries.
5
3
 
6
- ## How to use:
7
- 1. Install this library
8
- ```npm i autosuspense```
4
+ It lets you define fallback UI at the component level, while a parent ```<AutoSuspense>``` boundary handles rendering everything correctly.
9
5
 
10
- 2. Use it by calling and passing your components normally like ```<Supense>```:
6
+ ## Why AutoSuspense?:
7
+
8
+ React Suspense solves async rendering, but fallback composition quickly becomes messy:
9
+ ```
10
+ <Suspense fallback={<PageSkeleton />}>
11
+ <Header />
12
+ <Suspense fallback={<FeedSkeleton />}>
13
+ <Feed />
14
+ </Suspense>
15
+ </Suspense>
16
+ ```
17
+ AutoSuspense removes that boilerplate:
18
+ ```
19
+ <AutoSuspense>
20
+ <Page />
21
+ </AutoSuspense>
22
+ ```
23
+ Each component defines it's own fallback and autosuspense builds the fallback tree for you.
24
+
25
+ ```
26
+ import { Suspend } from "autosuspense";
27
+ import Feed from "./Feed";
28
+ import Sidebar from "./Sidebar";
29
+
30
+ function Page() {
31
+ return (
32
+ <div>
33
+ <Feed />
34
+ <Sidebar />
35
+ </div>
36
+ );
37
+ }
38
+
39
+ export default Suspend(Page, <div>Loading page...</div>);
40
+ ```
41
+
42
+ ## Installation:
43
+ ```npm install autosuspense```
44
+
45
+ Usage
46
+ 1. Add AutoSuspense boundary
11
47
  ```
12
48
  import { AutoSuspense } from "autosuspense";
13
49
 
@@ -19,24 +55,62 @@ function App() {
19
55
  );
20
56
  }
21
57
  ```
58
+ 2. Wrap components with Suspend
59
+ ```
60
+ import { Suspend } from "autosuspense";
61
+
62
+ const UserCard = () => {
63
+ const data = resource.read(); // may suspend
64
+ return <div>{data.name}</div>;
65
+ };
22
66
 
23
- 3. Inside each AutoSuspense component provide a fallback componet for that particular component via ```useSuspenseFallback``` hook.
67
+ export default Suspend(UserCard, <div>Loading user...</div>);
24
68
  ```
25
- import { useSuspenseFallback } from "autosuspense";
26
69
 
27
- function UserCard() {
28
- useSuspenseFallback(<div>Loading user…</div>);
70
+ 3. Nested components automatically compose
71
+ ```
72
+ const Parent = () => <Child />;
29
73
 
30
- // Component may suspend somewhere below
31
- return <Profile />;
32
- }
74
+ export default Suspend(Parent, <div>Big Loader...</div>);
75
+ const Child = () => {
76
+ const data = resource.read();
77
+ return <div>{data}</div>;
78
+ };
79
+
80
+ export default Suspend(Child, <div>Inner Loader...</div>);
81
+ ```
82
+ 👉 Resulting fallback:
83
+ ```
84
+ Big Loader...
85
+ Inner Loader...
86
+ ```
87
+ No manual fallback nesting required.
88
+
89
+ ## Core Idea:
90
+ - Wrap a subtree with <AutoSuspense>.
91
+ - Wrap components with Suspend().
92
+ - Each component declares its fallback.
93
+ - AutoSuspense automatically composes the fallback UI tree without explicit maintaince and wiring.
94
+
95
+ ## Fallback Options:
96
+
97
+ You can provide fallbacks in multiple ways:
98
+
99
+ 1. JSX element:
100
+ ```
101
+ Suspend(Component, <Skeleton />);
102
+ Component
103
+ Suspend(Component, SkeletonComponent);
104
+ ```
105
+
106
+ 2. Component:
107
+ ```
108
+ Suspend(Component, SkeletonComponent);
33
109
  ```
34
110
 
35
- ### Important Caveats:
36
- 1. This library works on top of existing Suspense blocks and prebuilds your Fallback UI via render tree traversal.
37
- 2. This means that it uses Depth First searching to prebuild so react is only able to show prebuilt Ui when it first encounters a place to suspend. This could mean that rest of your fallback UI in next adjacent components never render.
111
+ 3. String:
112
+ ```
113
+ Suspend(Component, SkeletonComponent);
114
+ ```
38
115
 
39
- ### TBD:
40
- 1. Adding Default Skeletons.
41
- 2. Supporting existing Library skeleton implementations.
42
- 3. Adding a central config & template extension components & file to easily extend and manage your Fallback UI.
116
+ AutoSuspense does not replace Suspense. It enhances fallback composition.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autosuspense",
3
- "version": "0.2.0",
3
+ "version": "1.0.0",
4
4
  "description": "Automatic suspense wiring block for React.",
5
5
  "license": "ISC",
6
6
  "type": "commonjs",