@storybook/tanstack-react 10.5.6 → 10.5.8
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/_browser-chunks/{chunk-6UULZFFJ.js → chunk-TR4QWDPU.js} +38 -14
- package/dist/chunk-B0pqNVvJ.d.ts +135 -0
- package/dist/export-mocks/react-router.d.ts +294 -291
- package/dist/export-mocks/start-storage-context.d.ts +12 -10
- package/dist/export-mocks/start.d.ts +61 -53
- package/dist/index.d.ts +54 -188
- package/dist/index.js +1 -1
- package/dist/node/index.d.ts +4 -26
- package/dist/node/index.js +6 -6
- package/dist/preset.d.ts +5 -4
- package/dist/preset.js +20 -13
- package/dist/preview.d.ts +7 -109
- package/dist/preview.js +3 -1
- package/package.json +5 -5
- package/template/stories/LoaderContextInjection.stories.tsx +45 -0
- package/template/stories/RouterContextInjection.stories.tsx +45 -0
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
var preview_exports = {};
|
|
10
10
|
__export(preview_exports, {
|
|
11
11
|
applyDecorators: () => applyDecorators,
|
|
12
|
+
beforeEach: () => beforeEach,
|
|
12
13
|
loaders: () => loaders,
|
|
13
14
|
optimizeDeps: () => optimizeDeps,
|
|
14
15
|
parameters: () => parameters
|
|
@@ -16,16 +17,16 @@ __export(preview_exports, {
|
|
|
16
17
|
import { applyDecorators as reactApplyDecorators } from "@storybook/react/entry-preview-docs";
|
|
17
18
|
|
|
18
19
|
// src/routing/decorator.tsx
|
|
19
|
-
import React from "react";
|
|
20
20
|
import {
|
|
21
21
|
createMemoryHistory,
|
|
22
22
|
createRootRoute,
|
|
23
23
|
createRoute as createRoute2,
|
|
24
24
|
createRouter,
|
|
25
|
-
|
|
25
|
+
defaultStringifySearch,
|
|
26
26
|
interpolatePath,
|
|
27
|
-
|
|
27
|
+
RouterProvider
|
|
28
28
|
} from "@tanstack/react-router";
|
|
29
|
+
import React from "react";
|
|
29
30
|
|
|
30
31
|
// src/routing/duplicate-tree.ts
|
|
31
32
|
import {
|
|
@@ -133,17 +134,15 @@ var StoryContext = React.createContext({ Story: () => null }), StoryFromContext
|
|
|
133
134
|
return React.createElement(Story, null);
|
|
134
135
|
}, tanstackRouteDecorator = (Story, context) => React.createElement(TanStackRouterStory, { Story, context });
|
|
135
136
|
function TanStackRouterStory({ Story, context }) {
|
|
136
|
-
let routerContext = context.parameters.tanstack?.router?.useRouterContext?.({
|
|
137
|
+
let router = context.tanstackRouter, routerContext = context.parameters.tanstack?.router?.useRouterContext?.({
|
|
137
138
|
storyContext: context
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
...routerContext
|
|
146
|
-
}),
|
|
139
|
+
});
|
|
140
|
+
if (!router)
|
|
141
|
+
throw new Error(
|
|
142
|
+
"No story router found on the story context: the `routerBeforeEach` hook of @storybook/tanstack-react did not run before rendering. Note that portable stories are not supported by this framework."
|
|
143
|
+
);
|
|
144
|
+
let providerContext = React.useMemo(
|
|
145
|
+
() => routerContext,
|
|
147
146
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
148
147
|
[context.id, routerContext]
|
|
149
148
|
);
|
|
@@ -242,6 +241,30 @@ function resolveTree(Story, context) {
|
|
|
242
241
|
};
|
|
243
242
|
}
|
|
244
243
|
|
|
244
|
+
// src/routing/before-each.ts
|
|
245
|
+
var storyRouters = /* @__PURE__ */ new Map(), routerBeforeEach = async (context) => {
|
|
246
|
+
let cached = storyRouters.get(context.id);
|
|
247
|
+
if (cached) {
|
|
248
|
+
context.tanstackRouter = cached;
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
let storyContext = context, parameterContext = (context.parameters.tanstack?.router ?? {}).context, routerContext = typeof parameterContext == "function" ? parameterContext({ storyContext }) : parameterContext, router = createStoryRouter({
|
|
252
|
+
Story: StoryFromContext,
|
|
253
|
+
context: storyContext,
|
|
254
|
+
routerContext
|
|
255
|
+
}), load = router.load();
|
|
256
|
+
if (context.abortSignal && !context.abortSignal.aborted ? (load.catch(() => {
|
|
257
|
+
}), await Promise.race([
|
|
258
|
+
load,
|
|
259
|
+
new Promise(
|
|
260
|
+
(resolve) => context.abortSignal.addEventListener("abort", () => resolve(), { once: !0 })
|
|
261
|
+
)
|
|
262
|
+
])) : await load, !context.abortSignal?.aborted)
|
|
263
|
+
return storyRouters.set(context.id, router), context.tanstackRouter = router, () => {
|
|
264
|
+
storyRouters.delete(context.id);
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
|
|
245
268
|
// src/routing/loader.ts
|
|
246
269
|
import { RootRoute as RootRoute3 } from "@tanstack/react-router";
|
|
247
270
|
function getComponentFromRoute(route) {
|
|
@@ -262,7 +285,7 @@ var routeComponentLoader = (context) => {
|
|
|
262
285
|
};
|
|
263
286
|
|
|
264
287
|
// src/preview.tsx
|
|
265
|
-
var loaders = [routeComponentLoader], applyDecorators = (storyFn, allDecorators) => (
|
|
288
|
+
var loaders = [routeComponentLoader], beforeEach = [routerBeforeEach], applyDecorators = (storyFn, allDecorators) => (
|
|
266
289
|
// reorder decorators so `jsxDecorator` is innermost, and `tanstackRouteDecorator` is just outside it
|
|
267
290
|
// There is an issue if `tanstackRouteDecorator` is innermost. All stories crashes due to a bug with the jsxDecorator.
|
|
268
291
|
reactApplyDecorators(storyFn, [
|
|
@@ -279,6 +302,7 @@ var loaders = [routeComponentLoader], applyDecorators = (storyFn, allDecorators)
|
|
|
279
302
|
|
|
280
303
|
export {
|
|
281
304
|
loaders,
|
|
305
|
+
beforeEach,
|
|
282
306
|
applyDecorators,
|
|
283
307
|
parameters,
|
|
284
308
|
optimizeDeps,
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { ComponentType } from "react";
|
|
2
|
+
import { CompatibleString } from "storybook/internal/types";
|
|
3
|
+
import { AnyRoute, FileRoutesByPath, Register } from "@tanstack/react-router";
|
|
4
|
+
import { Decorator } from "@storybook/react";
|
|
5
|
+
import { AnyContext, ResolveParams, RouteOptions, RoutesByPath } from "@tanstack/router-core";
|
|
6
|
+
import { BuilderOptions } from "@storybook/builder-vite";
|
|
7
|
+
import { StorybookConfig } from "@storybook/react-vite";
|
|
8
|
+
|
|
9
|
+
//#region code/frameworks/tanstack-react/.dts-emit/code/frameworks/tanstack-react/src/routing/types.d.ts
|
|
10
|
+
/** Union of every registered full path (e.g. `'/' | '/admin/users' | '/$libraryId/$version'`). */
|
|
11
|
+
type RegisteredFullPath = keyof Register['router']['routesByPath'];
|
|
12
|
+
type IsAppRouteTree<TRoute> = TRoute extends Register['router']['routeTree'] ? true : false;
|
|
13
|
+
type IsRoute<T> = T extends AnyRoute ? true : T extends FileRoutesByPath[keyof FileRoutesByPath] ? true : false;
|
|
14
|
+
type ExtractAllPathsFromFileRoutes<TRoute extends FileRoutesByPath[keyof FileRoutesByPath]['preLoaderRoute'] | AnyRoute> = TRoute['path'];
|
|
15
|
+
type StoryRoutePath<TRoute = undefined> = TRoute extends FileRoutesByPath[keyof FileRoutesByPath] ? ExtractAllPathsFromFileRoutes<TRoute> : keyof FileRoutesByPath | `/${string}`;
|
|
16
|
+
type StoryRouteSearch<TRoute> = IsAppRouteTree<TRoute> extends true ? Record<string, unknown> : TRoute extends FileRoutesByPath[keyof FileRoutesByPath] ? TRoute['preLoaderRoute'] extends {
|
|
17
|
+
types: {
|
|
18
|
+
allSearch: infer A;
|
|
19
|
+
};
|
|
20
|
+
} ? A : never : Record<string, unknown>;
|
|
21
|
+
type StoryRouteFileOptions<TRoute = undefined> = IsRoute<TRoute> extends true ? TRoute extends {
|
|
22
|
+
options: infer O;
|
|
23
|
+
} ? Pick<O, Extract<keyof O, 'loader' | 'beforeLoad' | 'validateSearch' | 'loaderDeps' | 'context' | 'params' | 'head' | 'search' | 'parseParams' | 'context'>> : Pick<RouteOptions<unknown>, 'loader' | 'beforeLoad' | 'validateSearch' | 'loaderDeps' | 'context' | 'params' | 'head' | 'search' | 'parseParams' | 'context'> : Pick<RouteOptions<unknown>, 'loader' | 'beforeLoad' | 'validateSearch' | 'loaderDeps' | 'context' | 'params' | 'head' | 'search' | 'parseParams' | 'context'>;
|
|
24
|
+
type CreateStoryRouteOptions<TRoute = undefined> = StoryRouteFileOptions<TRoute> & {
|
|
25
|
+
path?: StoryRoutePath<TRoute>;
|
|
26
|
+
};
|
|
27
|
+
type StoryRouteOptions<TRoute = undefined> = CreateStoryRouteOptions<TRoute> | (TRoute extends AnyRoute ? TRoute : AnyRoute);
|
|
28
|
+
/**
|
|
29
|
+
* Per-route override options for use inside `RouteTreeOverrides`.
|
|
30
|
+
* Users can override `loader`, `beforeLoad`, etc. for a specific route.
|
|
31
|
+
*/
|
|
32
|
+
interface RouteOverrideOptions<TRoute extends FileRoutesByPath[keyof FileRoutesByPath]['preLoaderRoute'] | undefined = undefined> {
|
|
33
|
+
/** Override the route's component. */
|
|
34
|
+
component?: TRoute extends FileRoutesByPath[keyof FileRoutesByPath]['preLoaderRoute'] ? TRoute['options']['component'] | ComponentType : ComponentType;
|
|
35
|
+
/** Override the route's loader function. */
|
|
36
|
+
loader?: TRoute extends FileRoutesByPath[keyof FileRoutesByPath]['preLoaderRoute'] ? TRoute['options']['loader'] | ((ctx: unknown) => Promise<unknown> | unknown) : (ctx: unknown) => Promise<unknown> | unknown;
|
|
37
|
+
/** Override the route's beforeLoad function. */
|
|
38
|
+
beforeLoad?: TRoute extends FileRoutesByPath[keyof FileRoutesByPath]['preLoaderRoute'] ? TRoute['options']['beforeLoad'] | ((ctx: unknown) => Promise<void> | void) : (ctx: unknown) => Promise<void> | void;
|
|
39
|
+
/** Override the route's search params validation. */
|
|
40
|
+
validateSearch?: TRoute extends FileRoutesByPath[keyof FileRoutesByPath]['preLoaderRoute'] ? TRoute['options']['validateSearch'] | ((search: unknown) => Promise<void> | void) : (search: unknown) => Promise<void> | void;
|
|
41
|
+
/** Override the route's loader dependencies. */
|
|
42
|
+
loaderDeps?: TRoute extends FileRoutesByPath[keyof FileRoutesByPath]['preLoaderRoute'] ? TRoute['options']['loaderDeps'] | string[] : string[];
|
|
43
|
+
/** Override the route's context function. */
|
|
44
|
+
context?: TRoute extends FileRoutesByPath[keyof FileRoutesByPath]['preLoaderRoute'] ? TRoute['options']['context'] | ((ctx: unknown) => Promise<unknown> | unknown) : (ctx: unknown) => Promise<unknown> | unknown;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A map of route overrides keyed by route ID.
|
|
48
|
+
* Each entry can override `loader`, `beforeLoad`, etc. for that route.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* routeOverrides: {
|
|
53
|
+
* '/_authed': { beforeLoad: () => {} },
|
|
54
|
+
* '/demo/form/simple/$id': {
|
|
55
|
+
* loader: async () => ({ name: 'Mock User' }),
|
|
56
|
+
* },
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
type RouteTreeOverrides = Partial<{ [routePath in keyof FileRoutesByPath]: RouteOverrideOptions<FileRoutesByPath[routePath]['preLoaderRoute']> | undefined }>;
|
|
61
|
+
interface RouterParameters<TRoute = undefined, Path extends (TRoute extends AnyRoute ? keyof RoutesByPath<TRoute> : RegisteredFullPath) = (TRoute extends AnyRoute ? keyof RoutesByPath<TRoute> : keyof FileRoutesByPath)> {
|
|
62
|
+
route?: StoryRouteOptions<TRoute>;
|
|
63
|
+
/**
|
|
64
|
+
* Path to resolve the story route against.
|
|
65
|
+
* Constrained to known registered paths in route tree mode, but can be any string in app route mode (since the user may be passing a custom `route` that doesn't exist in the registered tree).
|
|
66
|
+
*/
|
|
67
|
+
path?: Path;
|
|
68
|
+
/** URL params to interpolate into the path (e.g. `{ id: '42' }` for `/$id`). */
|
|
69
|
+
params?: ResolveParams<Path>;
|
|
70
|
+
/** Search/query params to append to the URL (e.g. `{ tab: 'details' }`). */
|
|
71
|
+
query?: Partial<StoryRouteSearch<TRoute>>;
|
|
72
|
+
/**
|
|
73
|
+
* Override options for specific routes in the app route tree (route tree mode only).
|
|
74
|
+
*
|
|
75
|
+
* Each key is a route ID (e.g. `'/about'`, `'__root__'`, `'/demo/form/simple/$id'`).
|
|
76
|
+
* Values can override `loader`, `beforeLoad`, etc. for that route.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* routeOverrides: {
|
|
81
|
+
* '/_authed': { beforeLoad: () => {} },
|
|
82
|
+
* '/demo/form/simple/$id': {
|
|
83
|
+
* loader: async () => ({ name: 'Mock User' }),
|
|
84
|
+
* },
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
routeOverrides?: RouteTreeOverrides;
|
|
89
|
+
/** Object or factory; the factory runs before initial router load outside React, so loader and beforeLoad can read its values. */
|
|
90
|
+
context?: Record<string, unknown> | ((options: {
|
|
91
|
+
storyContext: Parameters<Decorator>[1];
|
|
92
|
+
}) => Record<string, unknown>);
|
|
93
|
+
/** React hook render context reaches components, not the initial loader or beforeLoad; use the context factory for loader-visible values. */
|
|
94
|
+
useRouterContext?: ({
|
|
95
|
+
storyContext
|
|
96
|
+
}: {
|
|
97
|
+
storyContext: Parameters<Decorator>[1];
|
|
98
|
+
}) => AnyContext;
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region code/frameworks/tanstack-react/.dts-emit/code/frameworks/tanstack-react/src/types.d.ts
|
|
102
|
+
type FrameworkName = CompatibleString<'@storybook/tanstack-react'>;
|
|
103
|
+
type BuilderName = CompatibleString<'@storybook/builder-vite'>;
|
|
104
|
+
type FrameworkOptions = {
|
|
105
|
+
/** Builder options passed through to @storybook/builder-vite. */builder?: BuilderOptions;
|
|
106
|
+
};
|
|
107
|
+
type StorybookConfigFramework = {
|
|
108
|
+
framework: FrameworkName | {
|
|
109
|
+
name: FrameworkName;
|
|
110
|
+
options: FrameworkOptions;
|
|
111
|
+
};
|
|
112
|
+
core?: StorybookConfig['core'] & {
|
|
113
|
+
builder?: BuilderName | {
|
|
114
|
+
name: BuilderName;
|
|
115
|
+
options: BuilderOptions;
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
/** The interface for Storybook configuration in `main.ts` files. */
|
|
120
|
+
type StorybookConfig$1 = Omit<StorybookConfig, keyof StorybookConfigFramework> & StorybookConfigFramework;
|
|
121
|
+
/** Path constraint mirroring `RouterParameters`'s second generic. */
|
|
122
|
+
type DefaultStoryPath<TRoute> = TRoute extends AnyRoute ? keyof RoutesByPath<TRoute> : RegisteredFullPath;
|
|
123
|
+
interface TanStackPreviewOptions<TRoute = undefined, Path extends DefaultStoryPath<TRoute> = DefaultStoryPath<TRoute>> {
|
|
124
|
+
/** Router configuration for stories */
|
|
125
|
+
router?: RouterParameters<TRoute, Path>;
|
|
126
|
+
}
|
|
127
|
+
interface TanStackParameters<TRoute = undefined, Path extends DefaultStoryPath<TRoute> = DefaultStoryPath<TRoute>> {
|
|
128
|
+
/** TanStack framework configuration (router integration). */
|
|
129
|
+
tanstack?: TanStackPreviewOptions<TRoute, Path>;
|
|
130
|
+
}
|
|
131
|
+
interface TanStackTypes<TRoute = undefined, Path extends DefaultStoryPath<TRoute> = DefaultStoryPath<TRoute>> {
|
|
132
|
+
parameters: TanStackParameters<TRoute, Path>;
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
export { TanStackPreviewOptions as a, IsRoute as c, StoryRouteOptions as d, TanStackParameters as i, RouterParameters as l, FrameworkOptions as n, TanStackTypes as o, StorybookConfig$1 as r, CreateStoryRouteOptions as s, DefaultStoryPath as t, StoryRouteFileOptions as u };
|