@koordinates/xstate-tree 4.1.0-beta.3 → 4.1.0-beta.5

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/lib/builders.js CHANGED
@@ -122,7 +122,7 @@ export function createXStateTreeMachine(machine, options) {
122
122
  const xstateTreeMeta = {
123
123
  selectors,
124
124
  actions,
125
- view: options.view,
125
+ View: options.View,
126
126
  slots: (_c = options.slots) !== null && _c !== void 0 ? _c : [],
127
127
  };
128
128
  machine.meta = {
package/lib/index.js CHANGED
@@ -3,6 +3,6 @@ export * from "./slots";
3
3
  export { broadcast, buildRootComponent, onBroadcast } from "./xstateTree";
4
4
  export * from "./types";
5
5
  export { buildTestRootComponent, buildViewProps, genericSlotsTestingDummy, slotTestingDummyFactory, } from "./testingUtilities";
6
- export { Link, buildCreateRoute, matchRoute, } from "./routing";
6
+ export { Link, buildCreateRoute, matchRoute, useIsRouteActive, useRouteArgsIfActive, } from "./routing";
7
7
  export { loggingMetaOptions } from "./useService";
8
8
  export { lazy } from "./lazy";
@@ -4,4 +4,5 @@ export { Link } from "./Link";
4
4
  export { matchRoute } from "./matchRoute";
5
5
  export { handleLocationChange, } from "./handleLocationChange";
6
6
  export { useIsRouteActive } from "./useIsRouteActive";
7
+ export { useRouteArgsIfActive } from "./useRouteArgsIfActive";
7
8
  export { RoutingContext } from "./providers";
@@ -1,11 +1,12 @@
1
1
  import { useActiveRouteEvents } from "./providers";
2
2
  /**
3
+ * @public
3
4
  * Accepts Routes and returns true if any route is currently active. False if not.
4
5
  *
5
6
  * If used outside of a RoutingContext, an error will be thrown.
6
7
  * @param routes - the routes to check
7
8
  * @returns true if any route is active, false if not
8
- * @throws if used outside of a RoutingContext
9
+ * @throws if used outside of an xstate-tree root
9
10
  */
10
11
  export function useIsRouteActive(...routes) {
11
12
  const activeRouteEvents = useActiveRouteEvents();
@@ -0,0 +1,26 @@
1
+ import { assertIsDefined } from "../utils";
2
+ import { useActiveRouteEvents } from "./providers";
3
+ import { useIsRouteActive } from "./useIsRouteActive";
4
+ /**
5
+ * @public
6
+ * Returns the arguments for the given route if the route is active.
7
+ * Returns undefined if the route is not active.
8
+ *
9
+ * @param route - the route to get the arguments for
10
+ * @returns the arguments for the given route if the route is active, undefined otherwise
11
+ * @throws if used outside of an xstate-tree root
12
+ */
13
+ export function useRouteArgsIfActive(route) {
14
+ const isActive = useIsRouteActive(route);
15
+ const activeRoutes = useActiveRouteEvents();
16
+ if (!isActive) {
17
+ return undefined;
18
+ }
19
+ const activeRoute = activeRoutes === null || activeRoutes === void 0 ? void 0 : activeRoutes.find((activeRoute) => activeRoute.type === route.event);
20
+ assertIsDefined(activeRoute, "active route is not defined, but the route is active??");
21
+ return {
22
+ params: activeRoute.params,
23
+ query: activeRoute.query,
24
+ meta: activeRoute.meta,
25
+ };
26
+ }
@@ -697,6 +697,28 @@ declare type States = {
697
697
  */
698
698
  export declare type StyledLink<TStyleProps = {}> = <TRoute extends AnyRoute>(props: LinkProps<TRoute> & TStyleProps) => JSX.Element;
699
699
 
700
+ /**
701
+ * @public
702
+ * Accepts Routes and returns true if any route is currently active. False if not.
703
+ *
704
+ * If used outside of a RoutingContext, an error will be thrown.
705
+ * @param routes - the routes to check
706
+ * @returns true if any route is active, false if not
707
+ * @throws if used outside of an xstate-tree root
708
+ */
709
+ export declare function useIsRouteActive(...routes: AnyRoute[]): boolean;
710
+
711
+ /**
712
+ * @public
713
+ * Returns the arguments for the given route if the route is active.
714
+ * Returns undefined if the route is not active.
715
+ *
716
+ * @param route - the route to get the arguments for
717
+ * @returns the arguments for the given route if the route is active, undefined otherwise
718
+ * @throws if used outside of an xstate-tree root
719
+ */
720
+ export declare function useRouteArgsIfActive<TRoute extends AnyRoute>(route: TRoute): ArgumentsForRoute<TRoute> | undefined;
721
+
700
722
  /**
701
723
  * @public
702
724
  */
@@ -709,7 +731,7 @@ export declare type V2BuilderMeta<TMachine extends AnyStateMachine, TSelectorsOu
709
731
  selectors?: Selectors<TMachine, TSelectorsOutput>;
710
732
  actions?: Actions<TMachine, TSelectorsOutput, TActionsOutput>;
711
733
  slots?: TSlots;
712
- view: View<TActionsOutput, TSelectorsOutput, TSlots>;
734
+ View: View<TActionsOutput, TSelectorsOutput, TSlots>;
713
735
  };
714
736
 
715
737
  /**
package/lib/xstateTree.js CHANGED
@@ -165,7 +165,7 @@ export function XstateTreeView({ interpreter }) {
165
165
  const ViewV1 = interpreter.machine.meta.view;
166
166
  return (React.createElement(ViewV1, { selectors: selectorsRef.current, actions: actions, slots: slots, inState: inState }));
167
167
  case 2:
168
- const ViewV2 = interpreter.machine.meta.view;
168
+ const ViewV2 = interpreter.machine.meta.View;
169
169
  return (React.createElement(ViewV2, { selectors: selectorsRef.current, actions: actions, slots: slots }));
170
170
  default:
171
171
  throw new Error("builderVersion not set");
@@ -199,8 +199,17 @@ export function buildRootComponent(machine, routing) {
199
199
  if (!machine.meta) {
200
200
  throw new Error("Root machine has no meta");
201
201
  }
202
- if (!machine.meta.view) {
203
- throw new Error("Root machine has no associated view");
202
+ switch (machine.meta.builderVersion) {
203
+ case 1:
204
+ if (!machine.meta.view) {
205
+ throw new Error("Root machine has no associated view");
206
+ }
207
+ break;
208
+ case 2:
209
+ if (!machine.meta.View) {
210
+ throw new Error("Root machine has no associated view");
211
+ }
212
+ break;
204
213
  }
205
214
  const RootComponent = function XstateTreeRootComponent() {
206
215
  const [_, __, interpreter] = useMachine(machine, { devTools: true });
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@koordinates/xstate-tree",
3
3
  "main": "lib/index.js",
4
4
  "types": "lib/xstate-tree.d.ts",
5
- "version": "4.1.0-beta.3",
5
+ "version": "4.1.0-beta.5",
6
6
  "license": "MIT",
7
7
  "description": "Build UIs with Actors using xstate and React",
8
8
  "keywords": [