@koordinates/xstate-tree
Version:
Build UIs with Actors using xstate and React
766 lines (692 loc) • 30.1 kB
TypeScript
import type { ActorRefFrom } from 'xstate';
import { AnyStateMachine } from 'xstate';
import { ContextFrom } from 'xstate';
import type { EventFrom } from 'xstate';
import { EventObject } from 'xstate';
import { History as History_2 } from 'history';
import { InputFrom } from 'xstate';
import type { IsNever } from 'xstate';
import { ParsedQuery } from 'query-string';
import { default as React_2 } from 'react';
import type { SnapshotFrom } from 'xstate';
import type { StateValue } from 'xstate';
import * as Z from 'zod';
/**
* @public
*/
export declare type Actions<TMachine extends AnyStateMachine, TSelectorsOutput, TOut> = (args: {
send: ActorRefFrom<TMachine>["send"];
selectors: TSelectorsOutput;
}) => TOut;
/**
* @public
*
* Retrieves the actions return type from the xstate-tree machine
*/
export declare type ActionsFrom<TMachine extends AnyXstateTreeMachine> = TMachine["_xstateTree"] extends XstateTreeMachineStateSchemaV2<any, any, infer TOut, any> ? TOut : never;
/**
* @public
*/
export declare type AnyRoute = {
matches: (url: string, search: string) => any;
reverse: any;
navigate: any;
getEvent: any;
event: string;
preload: any;
basePath: string;
history: () => XstateTreeHistory;
parent?: AnyRoute;
paramsSchema?: Z.ZodObject<any>;
querySchema?: Z.ZodObject<any>;
matcher: (url: string, query: ParsedQuery<string> | undefined) => any;
reverser: any;
redirect?: any;
canMatch?: any;
};
/**
* @public
*/
export declare type AnyXstateTreeMachine = XstateTreeMachine<AnyStateMachine, any, any, any[]>;
/**
* @public
*/
export declare type ArgumentsForRoute<T> = T extends Route<infer TParams, infer TQuery, any, infer TMeta> ? RouteArguments<TParams, TQuery, TMeta> : never;
/**
* @public
*
* Broadcasts a global event to all xstate-tree machines
*
* @param event - the event to broadcast
*/
export declare function broadcast(event: GlobalEvents): void;
/**
* @public
*
* Creates a route factory
*
* @param history - the history object to use for this route factory, this needs to be the same one used in the trees root component
* @param basePath - the base path for this route factory
*/
export declare function buildCreateRoute(history: () => XstateTreeHistory, basePath: string): {
simpleRoute<TBaseRoute extends AnyRoute>(baseRoute?: TBaseRoute | undefined): <TEvent extends string, TParamsSchema extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
[x: string]: any;
}, {
[x: string]: any;
}> | undefined, TQuerySchema extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
[x: string]: any;
}, {
[x: string]: any;
}> | undefined, TMeta extends Record<string, unknown>>({ url, paramsSchema, querySchema, ...args }: {
event: TEvent;
url: string;
paramsSchema?: TParamsSchema | undefined;
querySchema?: TQuerySchema | undefined;
meta?: TMeta | undefined;
redirect?: RouteRedirect<MergeRouteTypes<RouteParams<TBaseRoute>, ResolveZodType<TParamsSchema>>, ResolveZodType<TQuerySchema>, MergeRouteTypes<RouteMeta<TBaseRoute>, TMeta> & SharedMeta> | undefined;
preload?: RouteArgumentFunctions<void, MergeRouteTypes<RouteParams<TBaseRoute>, ResolveZodType<TParamsSchema>>, ResolveZodType<TQuerySchema>, MergeRouteTypes<RouteMeta<TBaseRoute>, TMeta>, RouteArguments<MergeRouteTypes<RouteParams<TBaseRoute>, ResolveZodType<TParamsSchema>>, ResolveZodType<TQuerySchema>, MergeRouteTypes<RouteMeta<TBaseRoute>, TMeta>>> | undefined;
canMatch?: RouteArgumentFunctions<boolean, MergeRouteTypes<RouteParams<TBaseRoute>, ResolveZodType<TParamsSchema>>, ResolveZodType<TQuerySchema>, MergeRouteTypes<RouteMeta<TBaseRoute>, TMeta> & SharedMeta, RouteArguments<MergeRouteTypes<RouteParams<TBaseRoute>, ResolveZodType<TParamsSchema>>, ResolveZodType<TQuerySchema>, MergeRouteTypes<RouteMeta<TBaseRoute>, TMeta> & SharedMeta>> | undefined;
}) => Route<MergeRouteTypes<RouteParams<TBaseRoute>, ResolveZodType<TParamsSchema>>, ResolveZodType<TQuerySchema>, TEvent, MergeRouteTypes<RouteMeta<TBaseRoute>, TMeta> & SharedMeta>;
route<TBaseRoute_1 extends AnyRoute>(baseRoute?: TBaseRoute_1 | undefined): <TEvent_1 extends string, TParamsSchema_1 extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
[x: string]: any;
}, {
[x: string]: any;
}> | undefined, TQuerySchema_1 extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
[x: string]: any;
}, {
[x: string]: any;
}> | undefined, TMeta_1 extends Record<string, unknown>>({ event, matcher, reverser, paramsSchema, querySchema, redirect, preload, canMatch, }: {
event: TEvent_1;
paramsSchema?: TParamsSchema_1 | undefined;
querySchema?: TQuerySchema_1 | undefined;
meta?: TMeta_1 | undefined;
redirect?: RouteRedirect<MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1> & SharedMeta> | undefined;
/**
* Determines if the route matches the given url and query
*
* If there is no match, return false
* If there is a match, return the parsed params and query as well as the length of the matched path in the URL
*/
matcher: (url: string, query: ParsedQuery<string> | undefined) => false | (RouteArguments<MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1>> & {
matchLength: number;
});
/**
* Reverses the route to a URL
*
* Supplied with params/query objects and constructs the correct URL based on them
*/
reverser: RouteArgumentFunctions<string, MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1>, RouteArguments<MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1>>>;
preload?: RouteArgumentFunctions<void, MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1>, RouteArguments<MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1>>> | undefined;
canMatch?: RouteArgumentFunctions<boolean, MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1> & SharedMeta, RouteArguments<MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1> & SharedMeta>> | undefined;
}) => Route<MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, TEvent_1, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1> & SharedMeta>;
};
/**
* @public
*
* Builds a React host component for the root machine of an xstate-tree
*
* @param machine - The root machine of the tree
* @param routing - The routing configuration for the tree
*/
export declare function buildRootComponent<TMachine extends AnyXstateTreeMachine>(options: {
machine: TMachine;
} & MarkOptionalLikePropertiesOptional<RootOptions<InputFrom<TMachine>>>): {
({ children, }: {
children?: React_2.ReactNode;
}): JSX.Element;
rootMachine: TMachine;
};
/**
* @public
*
* Utility to aid in reducing boilerplate of mapping route events to xstate-tree machines
*
* Takes a list of routes and a mapping of route events to xstate-tree machines and returns an xstate-tree machine
* that renders the machines based on the routing events
*
* @param _routes - the array of routes you wish to map to machines
* @param mappings - an object mapping the route events to the machine to invoke
* @returns an xstate-tree machine that will render the right machines based on the routing events
*/
export declare function buildRoutingMachine<TRoutes extends AnyRoute[]>(_routes: TRoutes, mappings: Record<TRoutes[number]["event"], AnyXstateTreeMachine>): AnyXstateTreeMachine;
/**
* @internal
*/
export declare type CanHandleEvent<TMachine extends AnyStateMachine> = (e: EventFrom<TMachine>) => boolean;
/**
* @public
* Creates an xstate-tree machine from an xstate-machine
*
* Accepts an options object defining the selectors/actions/slots and view for the xstate-tree machine
*
* Selectors/slots/actions can be omitted from the options object and will default to
* - actions: an empty object
* - selectors: the context of the machine
* - slots: an empty array
*
* @param machine - The xstate machine to create the xstate-tree machine from
* @param options - the xstate-tree options
*/
export declare function createXStateTreeMachine<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = []>(machine: TMachine, options: V2BuilderMeta<TMachine, TSelectorsOutput, TActionsOutput, TSlots>): XstateTreeMachine<TMachine, TSelectorsOutput, TActionsOutput, TSlots>;
declare type EmptyKeys<T> = keyof {
[K in keyof T as IsEmptyObject<T[K], true> extends true ? K : never]: T[K];
};
declare type EmptyRouteArguments<TParams, TQuery> = IsEmptyObject<TParams, true> extends true ? IsEmptyObject<TQuery, true> extends true ? true : false : false;
/**
* @public
*
* Can be used as the slots prop for an xstate-tree view, will render a div containing a <p>slotName-slot<p> for each slot
*/
export declare const genericSlotsTestingDummy: any;
/**
* @public
*/
export declare type GetSlotNames<TSlots extends readonly Slot[]> = TSlots[number]["name"];
/**
* @public
* Extracts the properties defined on the XstateTreeEvents interface and converts them
* into proper event objects.
*
* Properties extending `string` have no payloads, any other type is the payload for the event
* The property name is extracted as the `type` of the event
*/
export declare type GlobalEvents = {
[I in keyof XstateTreeEvents]: XstateTreeEvents[I] extends string ? {
type: I;
} : XstateTreeEvents[I] & {
type: I;
};
}[keyof XstateTreeEvents];
declare type IsEmptyObject<Obj, ExcludeOptional extends boolean = false> = undefined extends Obj ? true : [keyof (ExcludeOptional extends true ? OmitOptional<Obj> : Obj)] extends [
never
] ? true : false;
declare type IsUnknown<T> = unknown extends T ? true : false;
/**
* @public
*
* Wraps an xstate-tree returning Promise (generated by `import()` in an xstate-tree machine responsible for
* booting up the machine upon resolution
*
* @param factory - the factory function that returns the promise that resolves to the machine
* @param options - configure loading component and context to invoke machine with
* @returns an xstate-tree machine that wraps the promise, invoking the resulting machine when it resolves
*/
export declare function lazy<TMachine extends AnyXstateTreeMachine>(factory: () => Promise<TMachine>, { Loader, input }?: Options<TMachine>): AnyXstateTreeMachine;
/**
* @public
*
* Renders an anchor tag pointing at the provided Route
*
* The query/params/meta props are conditionally required based on the
* route passed as the To parameter
*/
export declare const Link: <TRoute extends AnyRoute>(props: {
to: TRoute;
children: React_2.ReactNode;
testId?: string | undefined;
/**
* onClick works as normal, but if you return false from it the navigation will not happen
*/
onClick?: ((e: React_2.MouseEvent<HTMLAnchorElement>) => boolean | void) | undefined;
preloadOnInteraction?: boolean | undefined;
preloadOnHoverMs?: number | undefined;
} & RouteArguments<TRoute extends Route<infer TParams, any, any, any> ? TParams : undefined, TRoute extends Route<any, infer TQuery, any, any> ? TQuery : undefined, TRoute extends Route<any, any, any, infer TMeta> ? TMeta : undefined> & Omit<React_2.AnchorHTMLAttributes<HTMLAnchorElement>, "href" | "onClick"> & {
ref?: React_2.ForwardedRef<HTMLAnchorElement> | undefined;
}) => ReturnType<typeof LinkInner>;
declare function LinkInner<TRoute extends AnyRoute>({ to, children, testId, preloadOnHoverMs, preloadOnInteraction, onMouseDown: _onMouseDown, onMouseEnter: _onMouseEnter, onMouseLeave: _onMouseLeave, ...rest }: LinkProps<TRoute>, ref: React_2.ForwardedRef<HTMLAnchorElement>): JSX.Element;
/**
* @public
*/
export declare type LinkProps<TRoute extends AnyRoute, TRouteParams = TRoute extends Route<infer TParams, any, any, any> ? TParams : undefined, TRouteQuery = TRoute extends Route<any, infer TQuery, any, any> ? TQuery : undefined, TRouteMeta = TRoute extends Route<any, any, any, infer TMeta> ? TMeta : undefined> = {
to: TRoute;
children: React_2.ReactNode;
testId?: string;
/**
* onClick works as normal, but if you return false from it the navigation will not happen
*/
onClick?: (e: React_2.MouseEvent<HTMLAnchorElement>) => boolean | void;
preloadOnInteraction?: boolean;
preloadOnHoverMs?: number;
} & RouteArguments<TRouteParams, TRouteQuery, TRouteMeta> & Omit<React_2.AnchorHTMLAttributes<HTMLAnchorElement>, "href" | "onClick">;
/**
* @public
*/
export declare function loggingMetaOptions<TEvents extends EventObject, TContext>(ignoredEvents: TEvents["type"][], ignoreContext?: (keyof TContext)[] | undefined): {
xstateTree: {
ignoredEvents: Map<string, boolean>;
ignoreContext: (keyof TContext)[] | undefined;
};
};
/**
* Marks any required property that can accept undefined as optional
*/
declare type MarkOptionalLikePropertiesOptional<T> = Omit<T, EmptyKeys<T>> & Partial<Pick<T, EmptyKeys<T>>>;
/**
* @internal
*/
export declare type MatchesFrom<T extends AnyStateMachine> = (value: ToStatePaths<SnapshotFrom<T>["value"]>) => boolean;
/**
* @public
*/
export declare function matchRoute<TRoutes extends Route<any, any, any, any>[]>(routes: TRoutes, basePath: string, path: string, search: string): Return<TRoutes>;
declare type MergeRouteTypes<TBase, TSupplied> = undefined extends TBase ? TSupplied : undefined extends TSupplied ? TBase : TBase & TSupplied;
/**
* @public
*
* Extract meta type from route object
*/
export declare type Meta<T> = T extends {
meta: infer TMeta;
} ? TMeta : undefined;
/**
* @public
*/
export declare type MultiSlot<T extends string> = {
type: SlotType.MultiSlot;
name: `${T}Multi`;
getId(id: string): string;
};
/**
* @public
*/
export declare function multiSlot<T extends string>(name: T): MultiSlot<T>;
declare type OmitOptional<T> = {
[P in keyof Required<T> as Pick<T, P> extends Required<Pick<T, P>> ? P : never]: T[P];
};
/**
* @public
*
* Allows hooking in to the global events sent between machines
*
* @param handler - the handler to call when an event is broadcast
*/
export declare function onBroadcast(handler: (event: GlobalEvents) => void): () => void;
declare type Options<TStateMachine extends AnyXstateTreeMachine> = {
/**
* Displayed while the promise is resolving, defaults to returning null
*/
Loader?: React_2.ComponentType;
/**
* Allows you to specify an overriden context when the machine is invoked
* Automatically supplies the machines default context so only requires a partial of overrides
*/
input?: InputFrom<TStateMachine>;
};
/**
* @public
*
* Extract params type from route object object
*/
export declare type Params<T> = T extends {
params: infer TParams;
} ? TParams : undefined;
/**
* @public
*
* Extracts the event objects for the specified event types from the GlobalEvents union
*/
export declare type PickEvent<T extends Extract<GlobalEvents, {
type: string;
}>["type"]> = Extract<GlobalEvents, {
type: T;
}>;
/**
* @public
*
* Extract query type from route object
*/
export declare type Query<T> = T extends {
query: infer TQuery;
} ? TQuery : undefined;
/**
* Repairs the return type of the `provide` function on XstateTreeMachines to correctly return
* an XstateTreeMachine type instead of an xstate StateMachine
*/
declare type RepairProvideReturnType<T extends AnyStateMachine, TSelectorsOutput, TActionsOutput, TSlots extends readonly Slot[]> = {
[K in keyof T]: K extends "provide" ? (...args: Parameters<T[K]>) => XstateTreeMachine<T, TSelectorsOutput, TActionsOutput, TSlots> : T[K];
};
declare type ResolveZodType<T extends Z.ZodType<any> | undefined> = undefined extends T ? undefined : Z.TypeOf<Exclude<T, undefined>>;
declare type Return<TRoutes extends Route<any, any, any, any>[]> = {
type: "matched";
route: TRoutes[number];
event: RoutingEvent<TRoutes[number]>;
} | {
type: "no-matches";
} | {
type: "match-error";
error: unknown;
};
declare type RootOptions<TInput> = {
routing: {
routes: AnyRoute[];
history: XstateTreeHistory<any>;
basePath: string;
getPathName?: () => string;
getQueryString?: () => string;
} | undefined;
input: IsUnknown<TInput> extends true ? undefined : TInput;
};
/**
* @public
*
* xstate-tree routing event
*/
export declare type Route<TParams, TQuery, TEvent, TMeta> = {
/**
* Returns an event object for this route, or undefined if the route does not match
*
* The params are automatically extracted out of the url
* The query data is automatically extracted out the search
*
* The params/query objects are validated against the routes Zod schemas
* if they fail to validate an error is thrown
*
* @param url - the pathname portion of a url, this function expects the base path to have been stripped
* @param search - the query string information (ie "?foo=bar")
* @returns undefined if the route doesn't match the supplied url, event object otherwise
* @throws Error if the params or query schemas fail to parse the params/query objects
*/
matches: (url: string, search: string) => ({
type: TEvent;
originalUrl: string;
} & RouteArguments<TParams, TQuery, TMeta>) | false;
/**
* Takes in query/params objects as required by the route and returns a URL for that route
*
* The returned URL does not contain the base path
*
* Reverse can't do anything with meta arguments, so they are removed from the types
*/
reverse: RouteArgumentFunctions<string, TParams, TQuery, undefined>;
/**
* Takes in query/params/meta objects as required by the route and updates the
* url via History.push
*/
navigate: RouteArgumentFunctions<void, TParams, TQuery, TMeta>;
/**
* Preloads data required by the route. Passed in query/params/meta objects as required by the route
*
* Must be idempotent as it may be called multiple times
*
* Can be called on
* * Mouse down on a Link
* * Hovering on a Link
* * When a route is matched
*/
preload: RouteArgumentFunctions<void, TParams, TQuery, TMeta>;
/**
* Returns an event object for this route based on the supplied params/query/meta
*
* Primarily intended for internal use
*/
getEvent: RouteArgumentFunctions<{
type: TEvent;
} & RouteArguments<TParams, TQuery, TMeta>, TParams, TQuery, TMeta>;
matcher: (url: string, query: ParsedQuery<string> | undefined) => (RouteArguments<TParams, TQuery, TMeta> & {
matchLength: number;
}) | false;
reverser: RouteArgumentFunctions<string, TParams, TQuery, TMeta>;
/**
* Event type for this route
*/
event: TEvent;
history: () => XstateTreeHistory;
basePath: string;
parent?: AnyRoute;
paramsSchema?: Z.ZodObject<any>;
querySchema?: Z.ZodObject<any>;
redirect?: RouteRedirect<TParams, TQuery, TMeta>;
/**
* Optional predicate to control whether this route can be matched.
* Called after URL matching but before the route is considered matched.
* Useful for access control or conditional routing.
*/
canMatch?: RouteArgumentFunctions<boolean, TParams, TQuery, TMeta>;
};
/**
* @public
*/
export declare type RouteArgumentFunctions<TReturn, TParams, TQuery, TMeta, TArgs = RouteArguments<TParams, TQuery, TMeta>> = IsEmptyObject<TArgs> extends true ? () => TReturn : keyof TArgs extends "meta" ? (args?: TArgs) => TReturn : EmptyRouteArguments<TParams, TQuery> extends true ? (args?: Partial<TArgs>) => TReturn : (args: MarkOptionalLikePropertiesOptional<TArgs>) => TReturn;
/**
* @public
*/
export declare type RouteArguments<TParams, TQuery, TMeta> = TParams extends undefined ? TQuery extends undefined ? TMeta extends undefined ? {} : {
meta?: TMeta;
} : TMeta extends undefined ? {
query: TQuery;
} : {
query: TQuery;
meta?: TMeta;
} : TQuery extends undefined ? TMeta extends undefined ? {
params: TParams;
} : {
params: TParams;
meta?: TMeta;
} : TMeta extends undefined ? {
params: TParams;
query: TQuery;
} : {
params: TParams;
query: TQuery;
meta?: TMeta;
};
/**
* @public
*
* Extract meta type from route
*/
export declare type RouteMeta<T> = T extends Route<any, any, any, infer TMeta> ? TMeta : undefined;
/**
* @public
*
* Extract params type from route
*/
export declare type RouteParams<T> = T extends Route<infer TParams, any, any, any> ? TParams : undefined;
/**
* @public
*
* Extract query type from route
*/
export declare type RouteQuery<T> = T extends Route<any, infer TQuery, any, any> ? TQuery : undefined;
declare type RouteRedirect<TParams, TQuery, TMeta> = (args: MarkOptionalLikePropertiesOptional<{
params: TParams;
query: TQuery;
meta?: TMeta;
abortSignal: AbortSignal;
}>) => Promise<undefined | RouteArguments<Partial<TParams>, Partial<TQuery>, TMeta>>;
/**
* @public
*/
export declare type Routing404Event = {
type: "ROUTING_404";
url: string;
};
/**
* @public
*
* Converts a Route type into the Event that will be broadcast for that route
*
* All routes a machine should handle should be added to the machines event union using this type
*/
export declare type RoutingEvent<T> = T extends Route<infer TParams, infer TQuery, infer TEvent, infer TMeta> ? {
type: TEvent;
originalUrl: string;
params: TParams;
query: TQuery;
meta: TMeta;
} : never;
/**
* @public
*/
export declare type Selectors<TMachine extends AnyStateMachine, TOut> = (args: {
ctx: ContextFrom<TMachine>;
canHandleEvent: CanHandleEvent<TMachine>;
inState: MatchesFrom<TMachine>;
meta?: unknown;
}) => TOut;
/**
* @public
*
* Retrieves the selector return type from the xstate-tree machine
*/
export declare type SelectorsFrom<TMachine extends AnyXstateTreeMachine> = TMachine["_xstateTree"] extends XstateTreeMachineStateSchemaV2<any, infer TOut, any, any> ? TOut : never;
/**
* @public
*/
export declare type SharedMeta = {
/**
* Suppresses this routing change event from being picked up by react-router
*
* This is for a Koordinates specific modification to react-router
* TODO: Remove once there are user providable shared meta
*/
doNotNotifyReactRouter?: boolean;
/**
* True if this was the last routing event in the chain
*/
indexEvent?: boolean;
/**
* If true, use history.replace instead history.push
*/
replace?: boolean;
/**
* true if the event was triggered by the initial match of the URL on load
*/
onloadEvent?: boolean;
};
/**
* @public
*/
export declare type SingleSlot<T extends string> = {
type: SlotType.SingleSlot;
name: T;
getId(): string;
};
/**
* @public
*/
export declare function singleSlot<T extends string>(name: T): SingleSlot<T>;
/**
* @public
*/
export declare type Slot = SingleSlot<any> | MultiSlot<any>;
/**
* @public
*/
export declare enum SlotType {
SingleSlot = 0,
MultiSlot = 1
}
/**
* @public
*/
export declare type StyledLink<TStyleProps = {}> = <TRoute extends AnyRoute>(props: LinkProps<TRoute> & TStyleProps) => JSX.Element;
/**
* @public
*
* Renders the xstate-tree routing context. Designed for use in tests/storybook
* for components that make use of routing hooks but aren't part of an xstate-tree view
*
* @param activeRouteEvents - The active route events to use in the context
*/
export declare function TestRoutingContext({ activeRouteEvents, children, }: {
activeRouteEvents: RoutingEvent<AnyRoute>[];
children: React_2.ReactNode;
}): JSX.Element;
declare type ToStatePaths<TStateValue extends StateValue, TParentPath extends string = ""> = TStateValue extends string ? WithParentPath<TStateValue, TParentPath> : IsNever<keyof TStateValue> extends true ? never : WithParentPath<keyof TStateValue & string, TParentPath> | Values<{
[K in keyof TStateValue & string]?: ToStatePaths<NonNullable<TStateValue[K]>, WithParentPath<K, TParentPath>>;
}>;
/**
* @public
*
* Returns the list of active routing events, or undefined if there are none / used outside of an xstate-tree routing context
*/
export declare function useActiveRouteEvents(): {
type: string;
originalUrl: string;
params: unknown;
query: unknown;
meta: unknown;
}[] | undefined;
/**
* @public
* Accepts Routes and returns true if any route is currently active. False if not.
*
* If used outside of a RoutingContext, an error will be thrown.
* @param routes - the routes to check
* @returns true if any route is active, false if not
* @throws if used outside of an xstate-tree root
*/
export declare function useIsRouteActive(...routes: AnyRoute[]): boolean;
/**
* @public
* Accepts a single Route and returns true if the route is currently active and marked as an index route.
* False if not.
*
* If used outside of a RoutingContext, an error will be thrown.
* @param route - the route to check
* @returns true if the route is active and an index route, false if not
* @throws if used outside of an xstate-tree root
*/
export declare function useOnRoute(route: AnyRoute): boolean;
/**
* @public
* Returns the arguments for the given route if the route is active.
* Returns undefined if the route is not active.
*
* @param route - the route to get the arguments for
* @returns the arguments for the given route if the route is active, undefined otherwise
* @throws if used outside of an xstate-tree root
*/
export declare function useRouteArgsIfActive<TRoute extends AnyRoute>(route: TRoute): ArgumentsForRoute<TRoute> | undefined;
/**
* @public
*/
export declare type V2BuilderMeta<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = Slot[]> = {
selectors?: Selectors<TMachine, TSelectorsOutput>;
actions?: Actions<TMachine, TSelectorsOutput, TActionsOutput>;
slots?: TSlots;
View: View<TActionsOutput, TSelectorsOutput, TSlots>;
};
declare type Values<T> = T[keyof T];
/**
* @public
*/
export declare type View<TActionsOutput, TSelectorsOutput, TSlots extends readonly Slot[]> = React_2.ComponentType<{
slots: Record<GetSlotNames<TSlots>, React_2.ComponentType>;
actions: TActionsOutput;
selectors: TSelectorsOutput;
children?: React_2.ReactNode;
}>;
/**
* @public
*
* Simple utility builder to aid in integrating existing React views with xstate-tree
*
* @param view - the React view you want to invoke in an xstate machine
* @returns The view wrapped into an xstate-tree machine, ready to be invoked by other xstate machines or used with `buildRootComponent`
*/
export declare function viewToMachine(view: (args?: any) => JSX.Element | null): AnyXstateTreeMachine;
declare type WithParentPath<TCurrent extends string, TParentPath extends string> = `${TParentPath extends "" ? "" : `${TParentPath}.`}${TCurrent}`;
/**
* @public
*/
export declare type XstateTreeHistory<T = unknown> = History_2<{
meta?: T;
previousUrl?: string;
}>;
/**
* @public
*/
export declare type XstateTreeMachine<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = Slot[]> = RepairProvideReturnType<TMachine, TSelectorsOutput, TActionsOutput, TSlots> & XstateTreeMachineInjection<TMachine, TSelectorsOutput, TActionsOutput, TSlots>;
/**
* @internal
*/
export declare type XstateTreeMachineInjection<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = Slot[]> = {
_xstateTree: XstateTreeMachineStateSchemaV2<TMachine, TSelectorsOutput, TActionsOutput, TSlots>;
};
/**
* @public
*/
export declare type XstateTreeMachineStateSchemaV2<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = Slot[]> = Required<V2BuilderMeta<TMachine, TSelectorsOutput, TActionsOutput, TSlots>>;
export { }