autosuspense 0.2.0 → 1.0.1
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
|
@@ -1,13 +1,51 @@
|
|
|
1
1
|
# AutoSupense Beta
|
|
2
|
-
AutoSuspense is a
|
|
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
|
-
|
|
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
|
-
|
|
6
|
+
---
|
|
7
|
+
## Why AutoSuspense?:
|
|
8
|
+
|
|
9
|
+
React Suspense solves async rendering, but fallback composition quickly becomes messy:
|
|
10
|
+
```
|
|
11
|
+
<Suspense fallback={<PageSkeleton />}>
|
|
12
|
+
<Header />
|
|
13
|
+
<Suspense fallback={<FeedSkeleton />}>
|
|
14
|
+
<Feed />
|
|
15
|
+
</Suspense>
|
|
16
|
+
</Suspense>
|
|
17
|
+
```
|
|
18
|
+
AutoSuspense removes that boilerplate:
|
|
19
|
+
```
|
|
20
|
+
<AutoSuspense>
|
|
21
|
+
<Page />
|
|
22
|
+
</AutoSuspense>
|
|
23
|
+
```
|
|
24
|
+
Each component defines it's own fallback and autosuspense builds the fallback tree for you.
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
import { Suspend } from "autosuspense";
|
|
28
|
+
import Feed from "./Feed";
|
|
29
|
+
import Sidebar from "./Sidebar";
|
|
30
|
+
|
|
31
|
+
function Page() {
|
|
32
|
+
return (
|
|
33
|
+
<div>
|
|
34
|
+
<Feed />
|
|
35
|
+
<Sidebar />
|
|
36
|
+
</div>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default Suspend(Page, <div>Loading page...</div>);
|
|
41
|
+
```
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Installation:
|
|
45
|
+
```npm install autosuspense```
|
|
46
|
+
|
|
47
|
+
Usage
|
|
48
|
+
1. Add AutoSuspense boundary
|
|
11
49
|
```
|
|
12
50
|
import { AutoSuspense } from "autosuspense";
|
|
13
51
|
|
|
@@ -19,24 +57,107 @@ function App() {
|
|
|
19
57
|
);
|
|
20
58
|
}
|
|
21
59
|
```
|
|
60
|
+
2. Wrap components with Suspend
|
|
61
|
+
```
|
|
62
|
+
import { Suspend } from "autosuspense";
|
|
22
63
|
|
|
23
|
-
|
|
64
|
+
const UserCard = () => {
|
|
65
|
+
const data = resource.read(); // may suspend
|
|
66
|
+
return <div>{data.name}</div>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export default Suspend(UserCard, <div>Loading user...</div>);
|
|
24
70
|
```
|
|
25
|
-
import { useSuspenseFallback } from "autosuspense";
|
|
26
71
|
|
|
27
|
-
|
|
28
|
-
|
|
72
|
+
- Nested components automatically compose
|
|
73
|
+
```
|
|
74
|
+
const Parent = () => <Child />;
|
|
75
|
+
|
|
76
|
+
export default Suspend(Parent, <div>Outer Loader...</div>);
|
|
77
|
+
const Child = () => {
|
|
78
|
+
const data = resource.read();
|
|
79
|
+
return <div>{data}</div>;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
29
84
|
|
|
30
|
-
|
|
31
|
-
|
|
85
|
+
export default Suspend(Child, <div>Inner Loader...</div>);
|
|
86
|
+
```
|
|
87
|
+
👉 Resulting fallback:
|
|
88
|
+
```
|
|
89
|
+
Outer Loader...
|
|
90
|
+
Inner Loader...
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Using Prefabs (Reusable Fallbacks)
|
|
94
|
+
Instead of passing JSX everywhere, define reusable fallbacks once.
|
|
95
|
+
|
|
96
|
+
- Define prefabs at the root
|
|
97
|
+
```
|
|
98
|
+
import { AutoSuspense } from "autosuspense";
|
|
99
|
+
import PageSkeleton from "./skeletons/PageSkeleton";
|
|
100
|
+
import CardSkeleton from "./skeletons/CardSkeleton";
|
|
101
|
+
|
|
102
|
+
function App() {
|
|
103
|
+
return (
|
|
104
|
+
<AutoSuspense
|
|
105
|
+
prefab={{
|
|
106
|
+
page: PageSkeleton,
|
|
107
|
+
card: CardSkeleton,
|
|
108
|
+
}}
|
|
109
|
+
>
|
|
110
|
+
<Page />
|
|
111
|
+
</AutoSuspense>
|
|
112
|
+
);
|
|
32
113
|
}
|
|
114
|
+
🎯 Use prefab keys in components
|
|
115
|
+
import { Suspend } from "autosuspense";
|
|
116
|
+
|
|
117
|
+
function Page() {
|
|
118
|
+
return <Feed />;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export default Suspend(Page, "page");
|
|
122
|
+
function Feed() {
|
|
123
|
+
const data = resource.read();
|
|
124
|
+
return <div>{data}</div>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export default Suspend(Feed, "card");
|
|
128
|
+
```
|
|
129
|
+
- Resulting fallback UI
|
|
130
|
+
```
|
|
131
|
+
PageSkeleton
|
|
132
|
+
CardSkeleton
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Core Idea:
|
|
136
|
+
- Wrap a subtree with ```<AutoSuspense>```.
|
|
137
|
+
- Wrap components with Suspend().
|
|
138
|
+
- Each component declares its fallback.
|
|
139
|
+
- AutoSuspense automatically composes the fallback UI tree without explicit maintaince and wiring.
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
## Fallback Options:
|
|
143
|
+
|
|
144
|
+
You can provide fallbacks in multiple ways:
|
|
145
|
+
|
|
146
|
+
1. JSX element:
|
|
147
|
+
```
|
|
148
|
+
Suspend(Component, <Skeleton />);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
2. Component:
|
|
152
|
+
```
|
|
153
|
+
Suspend(Component, SkeletonComponent);
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
3. String:
|
|
157
|
+
```
|
|
158
|
+
Suspend(Component, SkeletonComponent);
|
|
33
159
|
```
|
|
34
160
|
|
|
35
|
-
|
|
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.
|
|
161
|
+
---
|
|
38
162
|
|
|
39
|
-
|
|
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.
|
|
163
|
+
AutoSuspense aims to make and enhance fallback creation and make it's maintainence easier.
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@ import { Block } from "./prebuilt/Block";
|
|
|
3
3
|
import { Card } from "./prebuilt/Card";
|
|
4
4
|
import { List } from "./prebuilt/List";
|
|
5
5
|
|
|
6
|
-
export const
|
|
6
|
+
export const defaultFallbacks: Record<string, React.ReactElement> = {
|
|
7
7
|
block: <Block />,
|
|
8
8
|
card: <Card />,
|
|
9
9
|
list: <List />,
|
|
@@ -5,18 +5,18 @@ import {
|
|
|
5
5
|
FallbackContext,
|
|
6
6
|
FallbackRegistry,
|
|
7
7
|
} from "../../types/FallbackRegistry";
|
|
8
|
-
import {
|
|
8
|
+
import { defaultFallbacks } from "../prefabs/defaultFallbacks";
|
|
9
9
|
|
|
10
10
|
export const AutoSuspense = ({
|
|
11
11
|
children,
|
|
12
|
-
|
|
12
|
+
fallbacks = defaultFallbacks,
|
|
13
13
|
}: {
|
|
14
14
|
children: React.ReactNode;
|
|
15
|
-
|
|
15
|
+
fallbacks?: Record<string, React.ReactElement | React.ComponentType<any>>;
|
|
16
16
|
}) => {
|
|
17
17
|
const registryRef = React.useRef<FallbackRegistry>({
|
|
18
18
|
nodes: new Map(),
|
|
19
|
-
prebuild: new Map(Object.entries(
|
|
19
|
+
prebuild: new Map(Object.entries(fallbacks)),
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
const fallback = <GeneratedFallback registry={registryRef.current} />;
|