@kasperrt/smolter 0.4.3 → 0.4.4
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/.generated/client/hash-history.ts +27 -0
- package/.generated/client/hydration.ts +98 -0
- package/.generated/client/root-data.ts +52 -0
- package/.generated/core/prepare-routes.ts +50 -0
- package/.generated/core/presentation.ts +1 -1
- package/.generated/preact/client.tsx +1 -26
- package/.generated/preact/deferred-hydration.ts +1 -98
- package/.generated/preact/index.ts +6 -1
- package/.generated/preact/navigation.tsx +13 -5
- package/.generated/preact/root-data.ts +1 -45
- package/.generated/preact/router.tsx +2 -48
- package/.generated/react/client.tsx +130 -0
- package/.generated/react/document.tsx +235 -0
- package/.generated/react/fetcher.ts +29 -7
- package/.generated/react/hooks.ts +20 -18
- package/.generated/react/index.ts +17 -1
- package/.generated/react/lazy-route.ts +116 -0
- package/.generated/react/link.ts +1 -38
- package/.generated/react/navigation-state.ts +13 -0
- package/.generated/react/navigation.ts +145 -0
- package/.generated/react/root-context.ts +22 -0
- package/.generated/react/route-hooks.ts +159 -0
- package/.generated/react/route-types.ts +91 -0
- package/.generated/react/router.ts +50 -0
- package/.generated/react/runtime.ts +1 -0
- package/.generated/server/application.tsx +18 -253
- package/.generated/server/dispatch.ts +87 -0
- package/.generated/server/index.ts +2 -0
- package/.generated/server/react.ts +34 -0
- package/.generated/server/render-application.ts +203 -0
- package/.generated/vite/application/lazy-files.ts +2 -1
- package/.generated/vite/application/vite.ts +12 -4
- package/.generated/vite/react.ts +11 -0
- package/CHANGELOG.md +10 -0
- package/README.md +41 -41
- package/dist/client/hash-history.d.ts +2 -0
- package/dist/client/hash-history.js +26 -0
- package/dist/client/hydration.d.ts +10 -0
- package/dist/{preact/deferred-hydration.js → client/hydration.js} +1 -1
- package/dist/client/root-data.d.ts +14 -0
- package/dist/{preact → client}/root-data.js +1 -1
- package/dist/core/prepare-routes.d.ts +7 -0
- package/dist/core/prepare-routes.js +28 -0
- package/dist/core/presentation.js +1 -1
- package/dist/preact/client.js +5 -26
- package/dist/preact/deferred-hydration.d.ts +1 -10
- package/dist/preact/document.js +2 -2
- package/dist/preact/index.d.ts +1 -1
- package/dist/preact/index.js +1 -1
- package/dist/preact/navigation-state.js +1 -1
- package/dist/preact/navigation.js +7 -5
- package/dist/preact/root-data.d.ts +1 -8
- package/dist/preact/route-hooks.js +1 -1
- package/dist/preact/router.d.ts +2 -2
- package/dist/preact/router.js +2 -26
- package/dist/react/client.d.ts +20 -0
- package/dist/react/client.js +86 -0
- package/dist/react/document.d.ts +25 -0
- package/dist/react/document.js +169 -0
- package/dist/react/fetcher.d.ts +8 -4
- package/dist/react/fetcher.js +12 -1
- package/dist/react/form.js +1 -1
- package/dist/react/hooks.d.ts +7 -6
- package/dist/react/index.d.ts +7 -1
- package/dist/react/index.js +6 -2
- package/dist/react/lazy-route.d.ts +22 -0
- package/dist/react/lazy-route.js +57 -0
- package/dist/react/link.d.ts +1 -12
- package/dist/react/navigation-state.d.ts +2 -0
- package/dist/react/navigation-state.js +9 -0
- package/dist/react/navigation.d.ts +33 -0
- package/dist/react/navigation.js +64 -0
- package/dist/react/root-context.d.ts +14 -0
- package/dist/react/root-context.js +10 -0
- package/dist/react/route-hooks.d.ts +34 -0
- package/dist/react/route-hooks.js +89 -0
- package/dist/react/route-types.d.ts +69 -0
- package/dist/react/router.d.ts +10 -0
- package/dist/react/router.js +22 -0
- package/dist/react/runtime.d.ts +1 -1
- package/dist/react/runtime.js +1 -1
- package/dist/server/application.d.ts +4 -8
- package/dist/server/application.js +9 -156
- package/dist/server/dispatch.d.ts +5 -0
- package/dist/server/dispatch.js +50 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +2 -1
- package/dist/server/preact.js +2 -1
- package/dist/server/react.d.ts +10 -0
- package/dist/server/react.js +22 -0
- package/dist/server/render-application.d.ts +31 -0
- package/dist/server/render-application.js +118 -0
- package/dist/vite/application/lazy-files.d.ts +1 -1
- package/dist/vite/application/lazy-files.js +2 -2
- package/dist/vite/application/vite.d.ts +2 -1
- package/dist/vite/application/vite.js +12 -5
- package/dist/vite/react.d.ts +4 -0
- package/dist/vite/react.js +9 -1
- package/jsr.json +10 -6
- package/package.json +18 -10
- package/.generated/core/resource-types.ts +0 -48
- package/.generated/core/resources.ts +0 -446
- package/.generated/react/data.ts +0 -29
- package/.generated/react/resources/components.tsx +0 -416
- package/dist/core/resource-types.d.ts +0 -41
- package/dist/core/resources.d.ts +0 -21
- package/dist/core/resources.js +0 -347
- package/dist/react/data.d.ts +0 -9
- package/dist/react/data.js +0 -3
- package/dist/react/link.js +0 -27
- package/dist/react/resources/components.d.ts +0 -76
- package/dist/react/resources/components.js +0 -250
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
import { Component, createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
-
import { jsx } from "react/jsx-runtime";
|
|
3
|
-
//#region .generated/react/resources/components.tsx
|
|
4
|
-
var RouterContext = createContext(null);
|
|
5
|
-
var RouteContext = createContext(null);
|
|
6
|
-
var FormContext = createContext(null);
|
|
7
|
-
/** Provide a resource router and load initial matches; dispose it when unmounted. */
|
|
8
|
-
function RouterProvider({ children, initialMatches = EMPTY_MATCHES, router }) {
|
|
9
|
-
useEffect(() => {
|
|
10
|
-
for (const match of initialMatches) router.load(match.routeId, match.params ?? {});
|
|
11
|
-
return () => router.dispose();
|
|
12
|
-
}, [initialMatches, router]);
|
|
13
|
-
return /* @__PURE__ */ jsx(RouterContext.Provider, {
|
|
14
|
-
value: router,
|
|
15
|
-
children
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
/** Provide a resource route and render its content, loading state or retry boundary. */
|
|
19
|
-
function Route({ children, params = {}, persistent = false, routeId }) {
|
|
20
|
-
const router = useRouter();
|
|
21
|
-
const stableParams = useStableParams(params);
|
|
22
|
-
const state = useRouteState(routeId, stableParams, true);
|
|
23
|
-
const route = router.getRoute(routeId);
|
|
24
|
-
const retry = useCallback(() => void router.retry(routeId, stableParams), [
|
|
25
|
-
routeId,
|
|
26
|
-
router,
|
|
27
|
-
stableParams
|
|
28
|
-
]);
|
|
29
|
-
const contextValue = useMemo(() => ({
|
|
30
|
-
params: stableParams,
|
|
31
|
-
routeId
|
|
32
|
-
}), [routeId, stableParams]);
|
|
33
|
-
useEffect(() => {
|
|
34
|
-
if (persistent) router.persist(routeId, stableParams);
|
|
35
|
-
}, [
|
|
36
|
-
persistent,
|
|
37
|
-
routeId,
|
|
38
|
-
router,
|
|
39
|
-
stableParams
|
|
40
|
-
]);
|
|
41
|
-
let content = children;
|
|
42
|
-
if (children === void 0 && route?.default) {
|
|
43
|
-
const RouteComponent = route.default;
|
|
44
|
-
content = /* @__PURE__ */ jsx(RouteComponent, {});
|
|
45
|
-
}
|
|
46
|
-
if (state.unexpectedError && route?.ErrorBoundary) {
|
|
47
|
-
const ErrorBoundary = route.ErrorBoundary;
|
|
48
|
-
content = /* @__PURE__ */ jsx(ErrorBoundary, { retry });
|
|
49
|
-
}
|
|
50
|
-
if (!state.unexpectedError && !state.loaderInitialized && route?.HydrateFallback) {
|
|
51
|
-
const HydrateFallback = route.HydrateFallback;
|
|
52
|
-
content = /* @__PURE__ */ jsx(HydrateFallback, {});
|
|
53
|
-
}
|
|
54
|
-
return /* @__PURE__ */ jsx(RouteContext.Provider, {
|
|
55
|
-
value: contextValue,
|
|
56
|
-
children: /* @__PURE__ */ jsx(RenderErrorBoundary, {
|
|
57
|
-
onRetry: retry,
|
|
58
|
-
resetKey: `${routeId}:${JSON.stringify(stableParams)}`,
|
|
59
|
-
...route?.ErrorBoundary ? { fallback: route.ErrorBoundary } : {},
|
|
60
|
-
children: content
|
|
61
|
-
})
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
/** Read the current resource route loader result. */
|
|
65
|
-
function useLoaderData() {
|
|
66
|
-
const route = useCurrentRoute();
|
|
67
|
-
return useRouteLoaderData(route.routeId, route.params);
|
|
68
|
-
}
|
|
69
|
-
/** Read loader data for a registered resource route. */
|
|
70
|
-
function useRouteLoaderData(routeId, params = {}) {
|
|
71
|
-
return useRouteState(routeId, useStableParams(params), true).loaderData;
|
|
72
|
-
}
|
|
73
|
-
/** Read the current resource route action result. */
|
|
74
|
-
function useActionData() {
|
|
75
|
-
const route = useCurrentRoute();
|
|
76
|
-
return useRouteState(route.routeId, route.params, false).actionData;
|
|
77
|
-
}
|
|
78
|
-
/** Read the current page navigation or submission state. */
|
|
79
|
-
function useNavigation() {
|
|
80
|
-
const route = useCurrentRoute();
|
|
81
|
-
return { state: useRouteState(route.routeId, route.params, false).navigationState };
|
|
82
|
-
}
|
|
83
|
-
/** Return the resource route revalidation function. */
|
|
84
|
-
function useRevalidator() {
|
|
85
|
-
const route = useCurrentRoute();
|
|
86
|
-
const router = useRouter();
|
|
87
|
-
const state = useRouteState(route.routeId, route.params, false);
|
|
88
|
-
return useMemo(() => ({
|
|
89
|
-
revalidate: () => router.load(route.routeId, route.params, true),
|
|
90
|
-
state: state.navigationState === "loading" ? "loading" : "idle"
|
|
91
|
-
}), [
|
|
92
|
-
route,
|
|
93
|
-
router,
|
|
94
|
-
state.navigationState
|
|
95
|
-
]);
|
|
96
|
-
}
|
|
97
|
-
/** Submit a resource action from a form without document navigation. */
|
|
98
|
-
function Form({ action, children, input, params }) {
|
|
99
|
-
const currentRoute = useContext(RouteContext);
|
|
100
|
-
const router = useRouter();
|
|
101
|
-
const routeId = action ?? currentRoute?.routeId ?? "";
|
|
102
|
-
const routeParams = useStableParams(params ?? currentRoute?.params ?? {});
|
|
103
|
-
const submit = useCallback((override) => router.submit(routeId, override === void 0 ? input : override, routeParams), [
|
|
104
|
-
input,
|
|
105
|
-
routeId,
|
|
106
|
-
routeParams,
|
|
107
|
-
router
|
|
108
|
-
]);
|
|
109
|
-
if (!routeId) throw new Error("Form requires an action or a parent Route.");
|
|
110
|
-
return /* @__PURE__ */ jsx(FormContext.Provider, {
|
|
111
|
-
value: submit,
|
|
112
|
-
children
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
/** Return a function that submits form data through the current router. */
|
|
116
|
-
function useSubmit() {
|
|
117
|
-
const submit = useContext(FormContext);
|
|
118
|
-
if (!submit) throw new Error("useSubmit must be used inside Form.");
|
|
119
|
-
return submit;
|
|
120
|
-
}
|
|
121
|
-
/** Read or mutate a resource independently of the rendered route. */
|
|
122
|
-
function useFetcher(options = {}) {
|
|
123
|
-
const currentRoute = useContext(RouteContext);
|
|
124
|
-
const router = useRouter();
|
|
125
|
-
const routeId = options.routeId ?? currentRoute?.routeId ?? "";
|
|
126
|
-
const params = useStableParams(options.params ?? currentRoute?.params ?? {});
|
|
127
|
-
const [data, setData] = useState(null);
|
|
128
|
-
const [error, setError] = useState("");
|
|
129
|
-
const [state, setState] = useState("idle");
|
|
130
|
-
const controller = useRef(null);
|
|
131
|
-
useEffect(() => () => {
|
|
132
|
-
controller.current?.abort();
|
|
133
|
-
}, []);
|
|
134
|
-
const run = useCallback(async (mutating, input, target) => {
|
|
135
|
-
controller.current?.abort();
|
|
136
|
-
const nextController = new AbortController();
|
|
137
|
-
const { signal } = nextController;
|
|
138
|
-
const targetId = target.routeId ?? routeId;
|
|
139
|
-
const targetParams = target.params ?? params;
|
|
140
|
-
controller.current = nextController;
|
|
141
|
-
setError("");
|
|
142
|
-
setState(mutating ? "submitting" : "loading");
|
|
143
|
-
const result = await (mutating ? router.fetchAction(targetId, input, targetParams, signal) : router.fetchLoader(targetId, targetParams, signal));
|
|
144
|
-
if (signal.aborted) return result;
|
|
145
|
-
controller.current = null;
|
|
146
|
-
setData(result.data);
|
|
147
|
-
setError(result.error);
|
|
148
|
-
setState("idle");
|
|
149
|
-
if (result.unexpected && currentRoute) router.reportUnexpected(currentRoute.routeId, currentRoute.params);
|
|
150
|
-
return result;
|
|
151
|
-
}, [
|
|
152
|
-
currentRoute,
|
|
153
|
-
params,
|
|
154
|
-
routeId,
|
|
155
|
-
router
|
|
156
|
-
]);
|
|
157
|
-
const submit = useCallback((input, target = {}) => run(true, input, target), [run]);
|
|
158
|
-
const load = useCallback((target = {}) => run(false, void 0, target), [run]);
|
|
159
|
-
const BoundForm = useCallback(({ children, input }) => /* @__PURE__ */ jsx(FormContext.Provider, {
|
|
160
|
-
value: (override) => submit(override === void 0 ? input : override),
|
|
161
|
-
children
|
|
162
|
-
}), [submit]);
|
|
163
|
-
if (!routeId) throw new Error("useFetcher requires a routeId or a parent Route.");
|
|
164
|
-
return [useMemo(() => ({
|
|
165
|
-
data,
|
|
166
|
-
error,
|
|
167
|
-
state
|
|
168
|
-
}), [
|
|
169
|
-
data,
|
|
170
|
-
error,
|
|
171
|
-
state
|
|
172
|
-
]), useMemo(() => ({
|
|
173
|
-
Form: BoundForm,
|
|
174
|
-
load,
|
|
175
|
-
submit
|
|
176
|
-
}), [
|
|
177
|
-
BoundForm,
|
|
178
|
-
load,
|
|
179
|
-
submit
|
|
180
|
-
])];
|
|
181
|
-
}
|
|
182
|
-
/** Read the resource router supplied by its provider. */
|
|
183
|
-
function useRouter() {
|
|
184
|
-
const router = useContext(RouterContext);
|
|
185
|
-
if (!router) throw new Error("RouterProvider is missing.");
|
|
186
|
-
return router;
|
|
187
|
-
}
|
|
188
|
-
function useCurrentRoute() {
|
|
189
|
-
const route = useContext(RouteContext);
|
|
190
|
-
if (!route) throw new Error("This hook requires a parent Route.");
|
|
191
|
-
return route;
|
|
192
|
-
}
|
|
193
|
-
function useRouteState(routeId, params, loadOnMount) {
|
|
194
|
-
const router = useRouter();
|
|
195
|
-
const subscribe = useCallback((listener) => router.subscribe(routeId, params, listener), [
|
|
196
|
-
params,
|
|
197
|
-
routeId,
|
|
198
|
-
router
|
|
199
|
-
]);
|
|
200
|
-
const getSnapshot = useCallback(() => router.getSnapshot(routeId, params), [
|
|
201
|
-
params,
|
|
202
|
-
routeId,
|
|
203
|
-
router
|
|
204
|
-
]);
|
|
205
|
-
const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
206
|
-
useEffect(() => {
|
|
207
|
-
if (loadOnMount) router.load(routeId, params);
|
|
208
|
-
}, [
|
|
209
|
-
loadOnMount,
|
|
210
|
-
params,
|
|
211
|
-
routeId,
|
|
212
|
-
router
|
|
213
|
-
]);
|
|
214
|
-
return state;
|
|
215
|
-
}
|
|
216
|
-
function useStableParams(params) {
|
|
217
|
-
const key = JSON.stringify(Object.entries(params).sort(([left], [right]) => left.localeCompare(right)));
|
|
218
|
-
const ref = useRef({
|
|
219
|
-
key,
|
|
220
|
-
value: params
|
|
221
|
-
});
|
|
222
|
-
if (ref.current.key !== key) ref.current = {
|
|
223
|
-
key,
|
|
224
|
-
value: params
|
|
225
|
-
};
|
|
226
|
-
return ref.current.value;
|
|
227
|
-
}
|
|
228
|
-
var RenderErrorBoundary = class extends Component {
|
|
229
|
-
state = { error: null };
|
|
230
|
-
static getDerivedStateFromError(error) {
|
|
231
|
-
return { error };
|
|
232
|
-
}
|
|
233
|
-
componentDidUpdate(previousProps) {
|
|
234
|
-
if (this.state.error && previousProps.resetKey !== this.props.resetKey) this.setState({ error: null });
|
|
235
|
-
}
|
|
236
|
-
render() {
|
|
237
|
-
if (this.state.error && this.props.fallback) {
|
|
238
|
-
const ErrorBoundary = this.props.fallback;
|
|
239
|
-
return /* @__PURE__ */ jsx(ErrorBoundary, { retry: () => {
|
|
240
|
-
this.setState({ error: null });
|
|
241
|
-
this.props.onRetry();
|
|
242
|
-
} });
|
|
243
|
-
}
|
|
244
|
-
if (this.state.error) throw this.state.error;
|
|
245
|
-
return this.props.children;
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
var EMPTY_MATCHES = [];
|
|
249
|
-
//#endregion
|
|
250
|
-
export { Form, Route, RouterProvider, useActionData, useFetcher, useLoaderData, useNavigation, useRevalidator, useRouteLoaderData, useRouter, useSubmit };
|