react-stateshape 0.2.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.
package/dist/index.cjs ADDED
@@ -0,0 +1,320 @@
1
+ let stateshape = require("stateshape");
2
+ let react = require("react");
3
+ let react_jsx_runtime = require("react/jsx-runtime");
4
+
5
+ const RouteContext = (0, react.createContext)(new stateshape.Route(null, { autoStart: false }));
6
+
7
+ function useLinkClick({ target, onClick }) {
8
+ let route = (0, react.useContext)(RouteContext);
9
+ return (0, react.useCallback)((event) => {
10
+ onClick?.(event);
11
+ if (!event.defaultPrevented && (0, stateshape.isRouteEvent)(event) && (!target || target === "_self")) {
12
+ event.preventDefault();
13
+ route.navigate((0, stateshape.getNavigationOptions)(event.currentTarget));
14
+ }
15
+ }, [
16
+ route,
17
+ target,
18
+ onClick
19
+ ]);
20
+ }
21
+
22
+ const A = ({ children, ...props }) => {
23
+ let handleClick = useLinkClick(props);
24
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("a", {
25
+ ...props,
26
+ href: String(props.href),
27
+ onClick: handleClick,
28
+ children
29
+ });
30
+ };
31
+
32
+ const Area = ({ alt, ...props }) => {
33
+ let handleClick = useLinkClick(props);
34
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("area", {
35
+ ...props,
36
+ href: String(props.href),
37
+ onClick: handleClick,
38
+ alt
39
+ });
40
+ };
41
+
42
+ /**
43
+ * A component providing a Route instance to the nested components.
44
+ */
45
+ const RouteProvider = ({ href, children }) => {
46
+ let route = (0, react.useMemo)(() => {
47
+ if (href instanceof stateshape.Route) return href;
48
+ else if (href === void 0 || typeof href === "string") return new stateshape.Route(href);
49
+ else throw new Error("URLProvider's 'href' of unsupported type");
50
+ }, [href]);
51
+ (0, react.useEffect)(() => {
52
+ route.start();
53
+ return () => route.stop();
54
+ }, [route]);
55
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RouteContext.Provider, {
56
+ value: route,
57
+ children
58
+ });
59
+ };
60
+
61
+ const TransientStateContext = (0, react.createContext)(/* @__PURE__ */ new Map());
62
+
63
+ const TransientStateProvider = ({ value, children }) => {
64
+ let defaultValueRef = (0, react.useRef)(null);
65
+ let stateMap = (0, react.useMemo)(() => {
66
+ if (value instanceof Map) return value;
67
+ if (typeof value === "object" && value !== null) return new Map(Object.entries(value).map(([key, value]) => [key, new stateshape.State(value)]));
68
+ if (defaultValueRef.current === null) defaultValueRef.current = /* @__PURE__ */ new Map();
69
+ return defaultValueRef.current;
70
+ }, [value]);
71
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TransientStateContext.Provider, {
72
+ value: stateMap,
73
+ children
74
+ });
75
+ };
76
+
77
+ const URLContext = (0, react.createContext)(new stateshape.URLState(null, { autoStart: false }));
78
+
79
+ /**
80
+ * A component providing a URL value to the nested components.
81
+ */
82
+ const URLProvider = ({ href, children }) => {
83
+ let urlState = (0, react.useMemo)(() => {
84
+ if (href instanceof stateshape.URLState) return href;
85
+ else if (href === void 0 || typeof href === "string") return new stateshape.URLState(href);
86
+ else throw new Error("URLProvider's 'href' of unsupported type");
87
+ }, [href]);
88
+ (0, react.useEffect)(() => {
89
+ urlState.start();
90
+ return () => urlState.stop();
91
+ }, [urlState]);
92
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(URLContext.Provider, {
93
+ value: urlState,
94
+ children
95
+ });
96
+ };
97
+
98
+ const defaultRenderCallback = (render) => render();
99
+ function useExternalState(state, callback = defaultRenderCallback, event) {
100
+ if (!(0, stateshape.isState)(state)) throw new Error("'state' is not an instance of PortableState");
101
+ let [, setRevision] = (0, react.useState)(-1);
102
+ let setValue = (0, react.useMemo)(() => state.setValue.bind(state), [state]);
103
+ let initialStateRevision = (0, react.useRef)(state.revision);
104
+ let shouldUpdate = (0, react.useRef)(false);
105
+ (0, react.useEffect)(() => {
106
+ state.start();
107
+ if (callback === false) return;
108
+ shouldUpdate.current = true;
109
+ let render = () => {
110
+ if (shouldUpdate.current) setRevision(Math.random());
111
+ };
112
+ let resolvedCallback = typeof callback === "function" ? callback : defaultRenderCallback;
113
+ let unsubscribe = state.on(event ?? "update", (payload) => {
114
+ resolvedCallback(render, payload);
115
+ });
116
+ if (state.revision !== initialStateRevision.current) setRevision(Math.random());
117
+ return () => {
118
+ unsubscribe();
119
+ initialStateRevision.current = state.revision;
120
+ shouldUpdate.current = false;
121
+ };
122
+ }, [
123
+ state,
124
+ callback,
125
+ event
126
+ ]);
127
+ return [state.getValue(), setValue];
128
+ }
129
+
130
+ function useNavigationComplete(callback) {
131
+ let route = (0, react.useContext)(RouteContext);
132
+ (0, react.useEffect)(() => route.on("navigationcomplete", callback), [route, callback]);
133
+ }
134
+
135
+ function useNavigationStart(callback) {
136
+ let route = (0, react.useContext)(RouteContext);
137
+ (0, react.useEffect)(() => route.on("navigationstart", callback), [route, callback]);
138
+ }
139
+
140
+ function useRoute(callback) {
141
+ let route = (0, react.useContext)(RouteContext);
142
+ useExternalState(route, callback, "navigation");
143
+ return (0, react.useMemo)(() => ({
144
+ route,
145
+ at: route.at.bind(route)
146
+ }), [route]);
147
+ }
148
+
149
+ /**
150
+ * Converts plain HTML links to SPA route links.
151
+ *
152
+ * @param containerRef - A React Ref pointing to a container element.
153
+ * @param elements - An optional selector, or an HTML element, or a
154
+ * collection thereof, specifying the links inside the container to
155
+ * be converted to SPA route links. Default: `"a, area"`.
156
+ */
157
+ function useRouteLinks(containerRef, elements) {
158
+ let route = (0, react.useContext)(RouteContext);
159
+ (0, react.useEffect)(() => {
160
+ return route.observe(() => containerRef.current, elements);
161
+ }, [
162
+ route,
163
+ elements,
164
+ containerRef
165
+ ]);
166
+ }
167
+
168
+ /**
169
+ * Reads and sets URL parameters in a way similar to React's `useState()`.
170
+ * This hooks returns `[state, setState]`, where `state` contains path
171
+ * placeholder parameters and query parameters, `{ params?, query? }`.
172
+ *
173
+ * Note that the path placeholders, `params`, are only available if the
174
+ * `url` parameter is an output of a typed URL builder (like the one
175
+ * produced with *url-shape*).
176
+ */
177
+ function useRouteState(url, options) {
178
+ let { route } = useRoute();
179
+ let getState = (0, react.useCallback)((href) => {
180
+ let resolvedHref = String(href ?? route.href);
181
+ return (0, stateshape.matchURL)(url === void 0 ? resolvedHref : url, resolvedHref);
182
+ }, [url, route]);
183
+ let setState = (0, react.useCallback)((update) => {
184
+ let urlData = typeof update === "function" ? update(getState()) : update;
185
+ route.navigate({
186
+ ...options,
187
+ href: (0, stateshape.compileURL)(url, urlData)
188
+ });
189
+ }, [
190
+ url,
191
+ route,
192
+ options,
193
+ getState
194
+ ]);
195
+ return [getState(), setState];
196
+ }
197
+
198
+ function createState(initial = true, pending = false, error) {
199
+ return {
200
+ initial,
201
+ pending,
202
+ error,
203
+ time: Date.now()
204
+ };
205
+ }
206
+ function useTransientState(state, action) {
207
+ let stateMap = (0, react.useContext)(TransientStateContext);
208
+ let stateRef = (0, react.useRef)(null);
209
+ let [stateItemInited, setStateItemInited] = (0, react.useState)(false);
210
+ let [actionState, setActionState] = useExternalState((0, react.useMemo)(() => {
211
+ if ((0, stateshape.isState)(state)) return state;
212
+ if (typeof state === "string") {
213
+ let stateItem = stateMap.get(state);
214
+ if (!stateItem) {
215
+ stateItem = new stateshape.State(createState());
216
+ stateMap.set(state, stateItem);
217
+ if (!stateItemInited) setStateItemInited(true);
218
+ }
219
+ return stateItem;
220
+ }
221
+ if (!stateRef.current) stateRef.current = new stateshape.State(createState());
222
+ return stateRef.current;
223
+ }, [
224
+ state,
225
+ stateMap,
226
+ stateItemInited
227
+ ]));
228
+ let trackableAction = (0, react.useCallback)((...args) => {
229
+ if (!action) throw new Error("A trackable action is only available when the hook's 'action' parameter is set");
230
+ let options = args.at(-1);
231
+ let originalArgs = args.slice(0, -1);
232
+ let result;
233
+ try {
234
+ result = action(...originalArgs);
235
+ } catch (error) {
236
+ setActionState((prevState) => ({
237
+ ...prevState,
238
+ ...createState(false, false, error)
239
+ }));
240
+ if (options?.throws) throw error;
241
+ }
242
+ if (result instanceof Promise) {
243
+ let delayedTracking = null;
244
+ if (!options?.silent) {
245
+ let delay = options?.delay;
246
+ if (delay === void 0) setActionState((prevState) => ({
247
+ ...prevState,
248
+ ...createState(false, true)
249
+ }));
250
+ else delayedTracking = setTimeout(() => {
251
+ setActionState((prevState) => ({
252
+ ...prevState,
253
+ ...createState(false, true)
254
+ }));
255
+ delayedTracking = null;
256
+ }, delay);
257
+ }
258
+ return result.then((value) => {
259
+ if (delayedTracking !== null) clearTimeout(delayedTracking);
260
+ setActionState((prevState) => ({
261
+ ...prevState,
262
+ ...createState(false, false)
263
+ }));
264
+ return value;
265
+ }).catch((error) => {
266
+ if (delayedTracking !== null) clearTimeout(delayedTracking);
267
+ setActionState((prevState) => ({
268
+ ...prevState,
269
+ ...createState(false, false, error)
270
+ }));
271
+ if (options?.throws) throw error;
272
+ });
273
+ }
274
+ setActionState((prevState) => ({
275
+ ...prevState,
276
+ ...createState(false, false)
277
+ }));
278
+ return result;
279
+ }, [action, setActionState]);
280
+ return (0, react.useMemo)(() => {
281
+ let extendedActionState = {
282
+ ...actionState,
283
+ update: setActionState
284
+ };
285
+ return action ? [extendedActionState, trackableAction] : [extendedActionState];
286
+ }, [
287
+ action,
288
+ trackableAction,
289
+ actionState,
290
+ setActionState
291
+ ]);
292
+ }
293
+
294
+ function useURL(callback) {
295
+ return useExternalState((0, react.useContext)(URLContext), callback, "navigation");
296
+ }
297
+
298
+ exports.A = A;
299
+ exports.Area = Area;
300
+ exports.RouteContext = RouteContext;
301
+ exports.RouteProvider = RouteProvider;
302
+ exports.TransientStateContext = TransientStateContext;
303
+ exports.TransientStateProvider = TransientStateProvider;
304
+ exports.URLContext = URLContext;
305
+ exports.URLProvider = URLProvider;
306
+ exports.useExternalState = useExternalState;
307
+ exports.useLinkClick = useLinkClick;
308
+ exports.useNavigationComplete = useNavigationComplete;
309
+ exports.useNavigationStart = useNavigationStart;
310
+ exports.useRoute = useRoute;
311
+ exports.useRouteLinks = useRouteLinks;
312
+ exports.useRouteState = useRouteState;
313
+ exports.useTransientState = useTransientState;
314
+ exports.useURL = useURL;
315
+ Object.keys(stateshape).forEach(function (k) {
316
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
317
+ enumerable: true,
318
+ get: function () { return stateshape[k]; }
319
+ });
320
+ });
@@ -0,0 +1,164 @@
1
+ import * as stateshape0 from "stateshape";
2
+ import { ContainerElement, EventCallback, LocationValue, MatchState, NavigationOptions, Route, State, StatePayloadMap, URLData, URLState } from "stateshape";
3
+ import * as react0 from "react";
4
+ import { AnchorHTMLAttributes, AreaHTMLAttributes, MouseEvent, ReactNode, RefObject } from "react";
5
+ import * as react_jsx_runtime0 from "react/jsx-runtime";
6
+ export * from "stateshape";
7
+
8
+ type EnhanceHref<T extends {
9
+ href?: string | undefined;
10
+ }> = Omit<T, "href"> & {
11
+ href?: LocationValue;
12
+ };
13
+
14
+ type LinkNavigationProps = {
15
+ "data-spa"?: NavigationOptions["spa"];
16
+ "data-history"?: NavigationOptions["history"];
17
+ "data-scroll"?: NavigationOptions["scroll"];
18
+ "data-id"?: NavigationOptions["id"];
19
+ };
20
+
21
+ type AProps = EnhanceHref<AnchorHTMLAttributes<HTMLAnchorElement>> & LinkNavigationProps;
22
+
23
+ declare const A: ({
24
+ children,
25
+ ...props
26
+ }: AProps) => react_jsx_runtime0.JSX.Element;
27
+
28
+ type AreaProps = EnhanceHref<AreaHTMLAttributes<HTMLAreaElement>> & LinkNavigationProps;
29
+
30
+ declare const Area: ({
31
+ alt,
32
+ ...props
33
+ }: AreaProps) => react_jsx_runtime0.JSX.Element;
34
+
35
+ declare const RouteContext: react0.Context<Route>;
36
+
37
+ type RouteProviderProps = {
38
+ href?: string | Route | undefined;
39
+ children?: ReactNode;
40
+ };
41
+ /**
42
+ * A component providing a Route instance to the nested components.
43
+ */
44
+ declare const RouteProvider: ({
45
+ href,
46
+ children
47
+ }: RouteProviderProps) => react_jsx_runtime0.JSX.Element;
48
+
49
+ type TransientState = {
50
+ initial?: boolean | undefined;
51
+ pending?: boolean | undefined;
52
+ error?: unknown;
53
+ time?: number | undefined;
54
+ };
55
+
56
+ declare const TransientStateContext: react0.Context<Map<string, State<TransientState, stateshape0.StatePayloadMap<TransientState>>>>;
57
+
58
+ type TransientStateProviderProps = {
59
+ value?: Record<string, TransientState> | Map<string, State<TransientState>> | null | undefined;
60
+ children?: ReactNode;
61
+ };
62
+ declare const TransientStateProvider: ({
63
+ value,
64
+ children
65
+ }: TransientStateProviderProps) => react_jsx_runtime0.JSX.Element;
66
+
67
+ type RenderCallback<T> = (render: () => void, payload: T) => boolean | undefined | void;
68
+
69
+ declare const URLContext: react0.Context<URLState>;
70
+
71
+ type URLProviderProps = {
72
+ href?: string | URLState | undefined;
73
+ children?: ReactNode;
74
+ };
75
+ /**
76
+ * A component providing a URL value to the nested components.
77
+ */
78
+ declare const URLProvider: ({
79
+ href,
80
+ children
81
+ }: URLProviderProps) => react_jsx_runtime0.JSX.Element;
82
+
83
+ type SetExternalStateValue<T, P extends StatePayloadMap<T> = StatePayloadMap<T>> = State<T, P>["setValue"];
84
+ declare function useExternalState<T, P extends StatePayloadMap<T>>(state: State<T, P>, callback?: RenderCallback<P["update"]> | boolean): [T, SetExternalStateValue<T, P>];
85
+ declare function useExternalState<T, P extends StatePayloadMap<T>, E extends keyof P>(state: State<T, P>, callback?: RenderCallback<P[E]> | boolean, event?: E): [T, SetExternalStateValue<T, P>];
86
+
87
+ declare function useLinkClick({
88
+ target,
89
+ onClick
90
+ }: AProps | AreaProps): (event: MouseEvent<HTMLAnchorElement & HTMLAreaElement>) => void;
91
+
92
+ declare function useNavigationComplete(callback: EventCallback<NavigationOptions>): void;
93
+
94
+ declare function useNavigationStart(callback: EventCallback<NavigationOptions>): void;
95
+
96
+ declare function useRoute(callback?: RenderCallback<NavigationOptions>): {
97
+ route: stateshape0.Route;
98
+ at: {
99
+ <P extends stateshape0.LocationPattern, X>(urlPattern: P, matchOutput: X | stateshape0.MatchHandler<P, X>): X | undefined;
100
+ <P extends stateshape0.LocationPattern, X, Y>(urlPattern: P, matchOutput: X | stateshape0.MatchHandler<P, X>, mismatchOutput: Y | stateshape0.MatchHandler<P, Y>): X | Y;
101
+ };
102
+ };
103
+
104
+ /**
105
+ * Converts plain HTML links to SPA route links.
106
+ *
107
+ * @param containerRef - A React Ref pointing to a container element.
108
+ * @param elements - An optional selector, or an HTML element, or a
109
+ * collection thereof, specifying the links inside the container to
110
+ * be converted to SPA route links. Default: `"a, area"`.
111
+ */
112
+ declare function useRouteLinks(containerRef: RefObject<ContainerElement>, elements?: Parameters<Route["observe"]>[1]): void;
113
+
114
+ type SetRouteState<T extends LocationValue> = (update: URLData<T> | ((state: MatchState<T>) => URLData<T>)) => void;
115
+ /**
116
+ * Reads and sets URL parameters in a way similar to React's `useState()`.
117
+ * This hooks returns `[state, setState]`, where `state` contains path
118
+ * placeholder parameters and query parameters, `{ params?, query? }`.
119
+ *
120
+ * Note that the path placeholders, `params`, are only available if the
121
+ * `url` parameter is an output of a typed URL builder (like the one
122
+ * produced with *url-shape*).
123
+ */
124
+ declare function useRouteState<T extends LocationValue>(url?: T, options?: Omit<NavigationOptions, "href">): [MatchState<T>, SetRouteState<T>];
125
+
126
+ type TransientStateOptions = {
127
+ /**
128
+ * Whether to track the action state silently (e.g. with a background
129
+ * action or an optimistic update).
130
+ *
131
+ * When set to `true`, the state's `pending` property doesn't switch
132
+ * to `true` in the pending state.
133
+ */
134
+ silent?: boolean;
135
+ /**
136
+ * Delays switching the action state's `pending` property to `true`
137
+ * in the pending state by the given number of milliseconds.
138
+ *
139
+ * Use case: to avoid flashing a process indicator if the action is
140
+ * likely to complete by the end of a short delay.
141
+ */
142
+ delay?: number;
143
+ /**
144
+ * Allows the async action to reject explicitly, along with exposing
145
+ * the action state's `error` property that goes by default.
146
+ */
147
+ throws?: boolean;
148
+ };
149
+ type ControllableTransientState = TransientState & {
150
+ update: SetExternalStateValue<TransientState>;
151
+ };
152
+ /**
153
+ * The hook's `state` parameter is a unique string key or an instance of
154
+ * `State`. Providing a string key or a `State` instance allows to share the
155
+ * action state across multiple components. If the key is omitted or set to
156
+ * `null`, the action state stays locally scoped to the component where the
157
+ * hook is used.
158
+ */
159
+ declare function useTransientState<F extends (...args: unknown[]) => unknown>(state: string | State<TransientState> | null, action: F): [ControllableTransientState, (...args: [...Parameters<F>, TransientStateOptions?]) => ReturnType<F>];
160
+ declare function useTransientState(state: string | State<TransientState> | null): [ControllableTransientState];
161
+
162
+ declare function useURL(callback?: RenderCallback<NavigationOptions>): [string, (update: string | stateshape0.StateUpdate<string>) => void];
163
+
164
+ export { A, AProps, Area, AreaProps, ControllableTransientState, EnhanceHref, LinkNavigationProps, RenderCallback, RouteContext, RouteProvider, RouteProviderProps, SetExternalStateValue, SetRouteState, TransientState, TransientStateContext, TransientStateOptions, TransientStateProvider, TransientStateProviderProps, URLContext, URLProvider, URLProviderProps, useExternalState, useLinkClick, useNavigationComplete, useNavigationStart, useRoute, useRouteLinks, useRouteState, useTransientState, useURL };