react-stateshape 0.2.0 → 0.2.2

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,6 +1,6 @@
1
1
  # react-stateshape
2
2
 
3
- Shared state management and routing in React apps. Under the hood, routing is shared state management, too, with the shared data being the URL.
3
+ A shared state management and routing lib for React apps. Under the hood, routing is shared state management, too, with the shared data being the URL.
4
4
 
5
5
  Contents: [useExternalState](#useexternalstate) · [useRoute](#useroute) · [useNavigationStart / useNavigationComplete](#usenavigationstart--usenavigationcomplete) · [useRouteState](#useroutestate) · [Type-safe routes](#type-safe-routes) · [useTransientState](#usetransientstate) · [Annotated examples](#annotated-examples) · [Internals](#internals)
6
6
 
@@ -66,16 +66,19 @@ let App = () => (
66
66
 
67
67
  ### Filtering state updates
68
68
 
69
- ⬥ Use the optional `false` parameter in `useExternalState(state, false)`, as in `<ResetButton>` above, to tell the hook not to subscribe the component to tracking the external state updates. The common use case for it is when a component makes use of the external state value setter without using the state value itself.
69
+ ⬥ Use the optional `false` parameter in `useExternalState(state, false)`, as in `ResetButton` above, to tell the hook not to subscribe the component to tracking the external state updates. The common use case for it is when a component makes use of the external state value setter without using the state value itself.
70
70
 
71
71
  ⬥ Apart from setting the optional second parameter of `useExternalState(state, callback)` to a boolean value, use it as a render callback for more fine-grained control over component's re-renders in response to state changes:
72
72
 
73
73
  ```js
74
+ let itemState = new State({/* A map of `<id>: <item>` */});
75
+
76
+ // Renders a specific item from `itemState`
74
77
  let ItemCard = ({ id }) => {
75
78
  let [items, setItems] = useExternalState(itemState, (render, { current, previous }) => {
76
- // Assuming that the items have a `revision` property, re-render
77
- // `ItemCard` only if the relevant item's `revision` has changed.
78
- if (current[id].revision !== previous[id].revision) render();
79
+ // Assuming that the items have a `timestamp` property, re-render
80
+ // `ItemCard` only if the relevant item's `timestamp` has increased
81
+ if (current[id].timestamp > previous[id].timestamp) render();
79
82
  });
80
83
 
81
84
  // ...
@@ -130,7 +133,7 @@ let App = () => {
130
133
 
131
134
  ⬥ `params` in dynamic values (as in `({ params }) => <Section id={params.id}/>` above) contains the URL pattern's capturing groups.
132
135
 
133
- ⬥ By default, `useRoute` and the other routing hooks described here make use of the browser's URL, if it's available. Otherwise, use `<RouteProvider href="/x">` to set a specific URL value. Common use cases: SSR and tests. A less common use case: custom routing behavior, including custom non-URL-based routing ([example](https://codesandbox.io/p/sandbox/tykt44?file=%252Fsrc%252FApp.tsx)).
136
+ ⬥ By default, `useRoute` and the other routing hooks described here make use of the browser's URL, if it's available. Otherwise, use `<RouteProvider href={url}>` to set a specific URL value. Common use cases: SSR and tests. A less common use case: custom routing behavior, including custom non-URL-based routing ([example](https://codesandbox.io/p/sandbox/tykt44?file=%252Fsrc%252FApp.tsx)).
134
137
 
135
138
  ⬥ See also the [Type-safe routes](#type-safe-routes) section.
136
139
 
package/dist/index.cjs CHANGED
@@ -1,3 +1,4 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
1
2
  let stateshape = require("stateshape");
2
3
  let react = require("react");
3
4
  let react_jsx_runtime = require("react/jsx-runtime");
@@ -312,9 +313,11 @@ exports.useRouteLinks = useRouteLinks;
312
313
  exports.useRouteState = useRouteState;
313
314
  exports.useTransientState = useTransientState;
314
315
  exports.useURL = useURL;
315
- Object.keys(stateshape).forEach(function (k) {
316
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
316
+ Object.keys(stateshape).forEach(function(k) {
317
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
317
318
  enumerable: true,
318
- get: function () { return stateshape[k]; }
319
+ get: function() {
320
+ return stateshape[k];
321
+ }
319
322
  });
320
323
  });
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import * as stateshape0 from "stateshape";
1
+ import * as stateshape from "stateshape";
2
2
  import { ContainerElement, EventCallback, LocationValue, MatchState, NavigationOptions, Route, State, StatePayloadMap, URLData, URLState } from "stateshape";
3
- import * as react0 from "react";
3
+ import * as react from "react";
4
4
  import { AnchorHTMLAttributes, AreaHTMLAttributes, MouseEvent, ReactNode, RefObject } from "react";
5
5
  import * as react_jsx_runtime0 from "react/jsx-runtime";
6
6
  export * from "stateshape";
@@ -32,7 +32,7 @@ declare const Area: ({
32
32
  ...props
33
33
  }: AreaProps) => react_jsx_runtime0.JSX.Element;
34
34
 
35
- declare const RouteContext: react0.Context<Route>;
35
+ declare const RouteContext: react.Context<Route>;
36
36
 
37
37
  type RouteProviderProps = {
38
38
  href?: string | Route | undefined;
@@ -53,7 +53,7 @@ type TransientState = {
53
53
  time?: number | undefined;
54
54
  };
55
55
 
56
- declare const TransientStateContext: react0.Context<Map<string, State<TransientState, stateshape0.StatePayloadMap<TransientState>>>>;
56
+ declare const TransientStateContext: react.Context<Map<string, State<TransientState, stateshape.StatePayloadMap<TransientState>>>>;
57
57
 
58
58
  type TransientStateProviderProps = {
59
59
  value?: Record<string, TransientState> | Map<string, State<TransientState>> | null | undefined;
@@ -66,7 +66,7 @@ declare const TransientStateProvider: ({
66
66
 
67
67
  type RenderCallback<T> = (render: () => void, payload: T) => boolean | undefined | void;
68
68
 
69
- declare const URLContext: react0.Context<URLState>;
69
+ declare const URLContext: react.Context<URLState>;
70
70
 
71
71
  type URLProviderProps = {
72
72
  href?: string | URLState | undefined;
@@ -94,10 +94,10 @@ declare function useNavigationComplete(callback: EventCallback<NavigationOptions
94
94
  declare function useNavigationStart(callback: EventCallback<NavigationOptions>): void;
95
95
 
96
96
  declare function useRoute(callback?: RenderCallback<NavigationOptions>): {
97
- route: stateshape0.Route;
97
+ route: stateshape.Route;
98
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;
99
+ <P extends stateshape.LocationPattern, X>(urlPattern: P, matchOutput: X | stateshape.MatchHandler<P, X>): X | undefined;
100
+ <P extends stateshape.LocationPattern, X, Y>(urlPattern: P, matchOutput: X | stateshape.MatchHandler<P, X>, mismatchOutput: Y | stateshape.MatchHandler<P, Y>): X | Y;
101
101
  };
102
102
  };
103
103
 
@@ -159,6 +159,6 @@ type ControllableTransientState = TransientState & {
159
159
  declare function useTransientState<F extends (...args: unknown[]) => unknown>(state: string | State<TransientState> | null, action: F): [ControllableTransientState, (...args: [...Parameters<F>, TransientStateOptions?]) => ReturnType<F>];
160
160
  declare function useTransientState(state: string | State<TransientState> | null): [ControllableTransientState];
161
161
 
162
- declare function useURL(callback?: RenderCallback<NavigationOptions>): [string, (update: string | stateshape0.StateUpdate<string>) => void];
162
+ declare function useURL(callback?: RenderCallback<NavigationOptions>): [string, (update: string | stateshape.StateUpdate<string>) => void];
163
163
 
164
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 };
package/dist/index.mjs CHANGED
@@ -1,8 +1,7 @@
1
1
  import { Route, State, URLState, compileURL, getNavigationOptions, isRouteEvent, isState, matchURL } from "stateshape";
2
2
  import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
3
3
  import { jsx } from "react/jsx-runtime";
4
-
5
- export * from "stateshape"
4
+ export * from "stateshape";
6
5
 
7
6
  const RouteContext = createContext(new Route(null, { autoStart: false }));
8
7
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-stateshape",
3
- "version": "0.2.0",
4
- "description": "Concise shared state management and routing in React apps",
3
+ "version": "0.2.2",
4
+ "description": "A shared state management and routing lib for React apps",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
7
7
  "types": "./dist/index.d.ts",
@@ -12,7 +12,7 @@
12
12
  "shape": "npx codeshape",
13
13
  "t3": "npx auxsrv tests/Tic-tac-toe",
14
14
  "test": "npx playwright test --project=chromium",
15
- "typecheck": "npx codeshape --typecheck-only"
15
+ "typecheck": "npx codeshape typecheck"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
@@ -34,16 +34,16 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@playwright/test": "^1.58.2",
37
- "@types/node": "^25.3.0",
37
+ "@types/node": "^25.5.0",
38
38
  "@types/react": "^19.2.14",
39
39
  "@types/react-dom": "^19.2.3",
40
- "auxsrv": "^0.3.1",
40
+ "auxsrv": "^0.3.2",
41
41
  "immer": "^11.1.4",
42
42
  "react-dom": "^19.2.4",
43
- "url-shape": "^1.3.13",
43
+ "url-shape": "^1.3.15",
44
44
  "zod": "^4.3.6"
45
45
  },
46
46
  "dependencies": {
47
- "stateshape": "^0.2.0"
47
+ "stateshape": "^0.2.1"
48
48
  }
49
49
  }
package/tsconfig.json CHANGED
@@ -4,6 +4,7 @@
4
4
  "declaration": true,
5
5
  "emitDeclarationOnly": true,
6
6
  "lib": ["ESNext", "DOM"],
7
+ "types": ["node"],
7
8
  "target": "esnext",
8
9
  "outDir": "dist",
9
10
  "module": "nodenext",