-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Tanstack: Add @storybook/tanstack-react package
#34403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
154 commits
Select commit
Hold shift + click to select a range
054c0cc
Feature: Add @storybook/tanstack-react framework
valentinpalkovic 433af94
Enhancement: Implement custom Vite plugin filtering for TanStack Star…
valentinpalkovic a920a3c
Enhancement: Add routing capabilities to @storybook/tanstack-react fr…
valentinpalkovic c7c5e37
Refactor: Simplify FrameworkOptions and TanStackRouterOptions types b…
valentinpalkovic 1e1b71a
Fix types
valentinpalkovic 13bbb6d
Refactor: Remove server configuration from viteFinal in preset.ts to …
valentinpalkovic 096b0db
Fix types
valentinpalkovic 4945a18
T1: Framework Core (preview.tsx, types.ts)
valentinpalkovic 48c7203
T2: Helpers & Exports (4 files)
valentinpalkovic 91f1243
T3: Template Stories (6 files)
valentinpalkovic 4ea368e
Linting
valentinpalkovic b33f415
Fix TypeScript formatter to use ESLint instead of Prettier
valentinpalkovic 24f5a02
Add basic stories
valentinpalkovic d1b1d7b
feat: implement server function stubs and integration tests
valentinpalkovic faf8abb
Remove router experiments
valentinpalkovic 640d43f
Remove tanstack template stories
valentinpalkovic f7a79aa
Cleanup Readme
valentinpalkovic 2f4955f
Merge remote-tracking branch 'origin/next' into julien/tanstack
huang-julien 0570f3e
Merge branch 'next' into julien/tanstack
huang-julien 0f9d59a
Merge branch 'next' into julien/tanstack
huang-julien 4bc05e8
Merge branch 'next' into julien/tanstack
huang-julien 57c6c53
feat: initial tanstack router mock
huang-julien 4f31ee9
fix: block navigation in memoryHistory
huang-julien b8b9b69
feat: expose router via decorator file
huang-julien a70afac
build: export getRouter from index and add missing decorator
huang-julien cdd0672
feat: mock tanstack query
huang-julien 0d58ae8
refactor: partial router cleanup
huang-julien 8fdc056
feat: add additionnal mocks and fix router implementation
huang-julien 6eb8692
Build: update build config
huang-julien c491732
fix: types
huang-julien 9669b76
chore: lockfile and exports json
huang-julien 25cc51e
chore: update template
huang-julien 1a06c5c
Merge remote-tracking branch 'origin/next' into julien/tanstack
huang-julien d97607b
feat: provide better types
huang-julien ef7ab22
feat: provide helper
huang-julien d5347df
feat: improve createStoryRoute API
huang-julien abf31b4
fix(tanstack-react): update the correct component in decorator
huang-julien cce1fe3
feat(tanstack-react): update templates
huang-julien e1ccd75
refactor: improve type
huang-julien 96e43b6
fix: make route available at TanstackTypes level for future library i…
huang-julien 58d9eb5
cleanup: remove loaders
huang-julien 5b4bb92
feat: improve type inference from CSF factory
huang-julien bc4cb86
feat: add params and query parameters to route
huang-julien b9fb69a
fix: mocks
huang-julien cdba65a
fix: use history replace instead of push
huang-julien 5a398e1
refactor: remove the whole query integration (implement later)
huang-julien af06aaf
fix(export-mocks): fix Navigate mock
huang-julien cf5770f
feat: use loader to allow Route as component
huang-julien 4b04eaa
chore: cleanup
huang-julien 732419f
feat: update CLI templates
huang-julien bcee564
feat: allow to use args to customize route behavior
huang-julien 259b8f8
fix: correctly infger story parameters from MEta type
huang-julien 191c419
fix: correctly infer types
huang-julien cfdcd53
feat(tanstack): update template stories
huang-julien a16ebc5
docs: draft ocumentation
huang-julien 071acda
fix types
huang-julien f204b4d
chore: cleanup sandbox-templates
huang-julien 8bf4a1b
chore : cleanups
huang-julien d52fbab
chore: fix lockfile
huang-julien 358607e
chore: add todo verification
huang-julien 09459ea
Merge branch 'next' into julien/tanstack
huang-julien 3672a44
chore: cleanup unused args
huang-julien d55d757
Merge branch 'julien/tanstack' of https://github.com/storybookjs/stor…
huang-julien f5a2924
Merge branch 'next' into julien/tanstack
huang-julien 53a142e
Merge branch 'next' into julien/tanstack
huang-julien effaf69
fix: remove rsc from plugin list
huang-julien 854b0ab
fix(tanstack-react): extract component from route from context in loader
huang-julien bf1e9a7
fix(tanstack-react): mock additionnal tanstack react server lib
huang-julien d523dcb
fix(tanstack-react): clone route and improve override
huang-julien 6693517
fix(tanstack-react): clone route and improve override
huang-julien ff2ee1d
chore update lock
huang-julien 65d03c1
fix update types and docs
huang-julien da30715
feat(tanstack-react): remove and mock out server side code
huang-julien a66779c
chore: cleanup
huang-julien 4e0bdce
fix(tanstack-react): context provider
huang-julien 6a715b5
fix(tanstack-react): route overrides
huang-julien d0b5917
fix: type extraction
huang-julien 75c370a
ci: add tanstack-react to sandbox template
huang-julien ff757e2
chore: update template story template
huang-julien 60dc912
update documentation
huang-julien a2f6b39
docs(tanstack-react): remove nodejs deps optimization
huang-julien 83289dd
fix: improve types for route overrides
huang-julien ff12e04
fix: type
huang-julien b9b5f83
docs: update docs and move code into codesnippets
huang-julien 9da4095
Merge branch 'next' into julien/tanstack
huang-julien 516c044
docs: split FAQ
huang-julien ad3bcf9
build: fix import
huang-julien 119d0de
Merge branch 'julien/tanstack' of https://github.com/storybookjs/stor…
huang-julien 3ab9dc6
chore: remove docs
huang-julien 2055b42
ci: uncomment sandbox generation
huang-julien 86937ea
Revert "ci: uncomment sandbox generation"
huang-julien 3d85527
fix: feedback
huang-julien c6ada2b
fix(create-sb): fix tanstack deps detection
huang-julien 005e60c
Update code/frameworks/tanstack-react/src/export-mocks/react-router.ts
huang-julien 4f5324f
Update code/frameworks/tanstack-react/src/export-mocks/react-router.ts
huang-julien 977d10b
Update code/frameworks/tanstack-react/src/export-mocks/react-router.ts
huang-julien 9a3b958
Merge branch 'next' into julien/tanstack
huang-julien 8611d0b
fix: improve plugin ignore list
huang-julien dcf02d1
revert: revert sandbox-part changes
huang-julien 0286697
Merge branch 'next' into julien/tanstack
huang-julien e7426ce
build: update tanstack sandboxes to start / routerSPA
huang-julien 7b239a4
Merge branch 'next' into julien/tanstack
huang-julien bc3639c
fix: sandbox names
huang-julien 8f26078
Merge branch 'julien/tanstack' of https://github.com/storybookjs/stor…
huang-julien 35fefa3
Merge branch 'next' into julien/tanstack
huang-julien 0faaf9a
ci: add missing extra deps for tanstack
huang-julien 2ea9d5c
Merge branch 'julien/tanstack' of https://github.com/storybookjs/stor…
huang-julien e110fa8
fix(tanstack§react): add default error component to show error
huang-julien 58d3dba
ci(tanstack-react): fix sandbox generation
huang-julien f544bfc
refactor(tanstack-react): rename ProjectType.TANSTACK
huang-julien b5387e9
Merge branch 'next' into julien/tanstack
huang-julien d2f4ded
fix: strip out server options in routes
huang-julien 9d778f4
fix: stub server only files
huang-julien 5bd3eaf
feat(tanstack-react): allow routeTree
huang-julien 71bff3c
fix(tanstack-react): fix Route only stories
huang-julien 2c8718d
chore: remove unnecessary mocks
huang-julien d0e6818
Merge branch 'julien/tanstack' of https://github.com/storybookjs/stor…
huang-julien b08cb0b
fix: improve server side code removal
huang-julien 9823870
fix: type constraint, tree duplication and memoize render
huang-julien 26e27f3
fix: unmemoize router creation and fix types
huang-julien 14e0eb8
fix: corectly duplicate tree for Route instances
huang-julien da49192
fix: don't duplicate route when users provides routeOptions
huang-julien 754eb7b
Merge remote-tracking branch 'origin/next' into julien/tanstack
huang-julien 4e037f3
chore: update package json
huang-julien 353385c
fix: fix react context issues in tests
huang-julien 9392ab0
chore: format
huang-julien b1c367d
fix: remove story memoization
huang-julien 97906de
fix: memoize router
huang-julien 9c9a141
fix: use react provider to provide story in react contextr
huang-julien 4ff02bf
fix: decorator
huang-julien 41577f8
fix: decorator order
huang-julien c4854a1
fix: exclude @storybook/react from optimizeDeps
huang-julien 1416920
Merge remote-tracking branch 'origin/next' into julien/tanstack
huang-julien b3023c4
chore: code cleaning and add comments
huang-julien c381a10
fix: add devtools deps to optimize deps
huang-julien caf2e9d
chore: clean types
huang-julien 03a176c
Merge remote-tracking branch 'origin/next' into julien/tanstack
huang-julien d268a96
chore: rename plugin
huang-julien 8c9eb9f
refactor: simplify mocks
huang-julien 5cbc922
chore: move stub plugin into dedicated file
huang-julien 8b7c863
Update code/frameworks/tanstack-react/src/plugins/server-code-elimina…
huang-julien 2d58254
Apply suggestions from code review
huang-julien 40f8bbd
fix: babel changes
huang-julien b0b5af5
fix: type
huang-julien fa6e73f
chore: cleanup package.json
huang-julien 5b44054
chore: use type augmentation
huang-julien 989a95f
fix: coderabbitai fedddback
huang-julien c92bd95
refactor: use template string
huang-julien ec79572
chore: regen lockfile
huang-julien 8edbea5
docs: update comment to guide users on documentation
huang-julien ed7615b
fix: make tanstack template similar to nextjs ones
huang-julien cea37cc
Merge branch 'next' into julien/tanstack
huang-julien 976b1a7
refactor: remove start-server mock
huang-julien 5909e3e
Merge remote-tracking branch 'origin/next' into julien/tanstack
huang-julien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Storybook for TanStack (React + Vite) | ||
|
|
||
| Develop, document, and test UI components in isolation with built-in TanStack Router and TanStack Query support. | ||
|
|
||
| See [documentation](https://storybook.js.org/docs/get-started?ref=readme) for installation instructions, usage examples, APIs, and more. | ||
|
|
||
| Learn more about Storybook at [storybook.js.org](https://storybook.js.org/?ref=readme). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import type { BuildEntries } from '../../../scripts/build/utils/entry-utils.ts'; | ||
|
|
||
| const config: BuildEntries = { | ||
| entries: { | ||
| browser: [ | ||
| { | ||
| exportEntries: ['.'], | ||
| entryPoint: './src/index.ts', | ||
| }, | ||
| { | ||
| exportEntries: ['./preview'], | ||
| entryPoint: './src/preview.tsx', | ||
| }, | ||
| { | ||
| exportEntries: ['./react-router'], | ||
| entryPoint: './src/export-mocks/react-router.ts', | ||
| external: ['@tanstack/react-router'], | ||
| }, | ||
| { | ||
| exportEntries: ['./start-storage-context'], | ||
| entryPoint: './src/export-mocks/start-storage-context.ts', | ||
| }, | ||
| { | ||
| exportEntries: ['./start'], | ||
| entryPoint: './src/export-mocks/start.ts', | ||
| }, | ||
| ], | ||
| node: [ | ||
| { | ||
| exportEntries: ['./preset'], | ||
| entryPoint: './src/preset.ts', | ||
| }, | ||
| { | ||
| exportEntries: ['./node'], | ||
| entryPoint: './src/node/index.ts', | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| { | ||
| "name": "@storybook/tanstack-react", | ||
| "version": "10.4.0-alpha.16", | ||
| "description": "Storybook for TanStack (React, Vite): Router and Start ready Storybook framework", | ||
| "keywords": [ | ||
| "storybook", | ||
| "storybook-framework", | ||
| "tanstack", | ||
| "tanstack-router", | ||
| "tanstack-start", | ||
| "react", | ||
| "vite", | ||
| "component", | ||
| "components" | ||
| ], | ||
| "homepage": "https://github.com/storybookjs/storybook/tree/next/code/frameworks/tanstack-react", | ||
| "bugs": { | ||
| "url": "https://github.com/storybookjs/storybook/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/storybookjs/storybook.git", | ||
| "directory": "code/frameworks/tanstack-react" | ||
| }, | ||
| "funding": { | ||
| "type": "opencollective", | ||
| "url": "https://opencollective.com/storybook" | ||
| }, | ||
| "license": "MIT", | ||
| "type": "module", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "code": "./src/index.ts", | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "./node": { | ||
| "types": "./dist/node/index.d.ts", | ||
| "code": "./src/node/index.ts", | ||
| "default": "./dist/node/index.js" | ||
| }, | ||
| "./package.json": "./package.json", | ||
| "./preset": { | ||
| "types": "./dist/preset.d.ts", | ||
| "code": "./src/preset.ts", | ||
| "default": "./dist/preset.js" | ||
| }, | ||
| "./preview": { | ||
| "types": "./dist/preview.d.ts", | ||
| "code": "./src/preview.tsx", | ||
| "default": "./dist/preview.js" | ||
| }, | ||
| "./react-router": { | ||
| "types": "./dist/export-mocks/react-router.d.ts", | ||
| "code": "./src/export-mocks/react-router.ts", | ||
| "default": "./dist/export-mocks/react-router.js" | ||
| }, | ||
| "./start": { | ||
| "types": "./dist/export-mocks/start.d.ts", | ||
| "code": "./src/export-mocks/start.ts", | ||
| "default": "./dist/export-mocks/start.js" | ||
| }, | ||
| "./start-storage-context": { | ||
| "types": "./dist/export-mocks/start-storage-context.d.ts", | ||
| "code": "./src/export-mocks/start-storage-context.ts", | ||
| "default": "./dist/export-mocks/start-storage-context.js" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "dist/**/*", | ||
| "template/**/*", | ||
| "README.md", | ||
| "*.js", | ||
| "*.d.ts", | ||
| "!src/**/*" | ||
| ], | ||
| "dependencies": { | ||
| "@storybook/builder-vite": "workspace:*", | ||
| "@storybook/react": "workspace:*", | ||
| "@storybook/react-vite": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "@tanstack/react-router": "^1.168.10", | ||
| "@tanstack/react-start": "^1.167.16", | ||
| "@tanstack/router-core": "^1.168.9", | ||
| "@tanstack/start-client-core": "^1.167.9", | ||
| "@types/node": "^22.19.1", | ||
| "typescript": "^5.9.3", | ||
| "vite": "^7.0.4" | ||
| }, | ||
| "peerDependencies": { | ||
| "@tanstack/react-router": "^1.168.10", | ||
| "@tanstack/react-start": "^1.167.16", | ||
| "@tanstack/router-core": "^1.168.9", | ||
| "@tanstack/start-client-core": "^1.167.9", | ||
| "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0", | ||
| "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0", | ||
| "storybook": "workspace:^", | ||
| "vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0" | ||
| }, | ||
| "peerDependenciesMeta": { | ||
| "@tanstack/react-start": { | ||
| "optional": true | ||
| }, | ||
| "@tanstack/start-client-core": { | ||
| "optional": true | ||
| } | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "name": "tanstack-react", | ||
| "$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
| "projectType": "library", | ||
| "targets": { | ||
| "compile": {}, | ||
| "check": {} | ||
| }, | ||
| "tags": ["library"] | ||
| } |
99 changes: 99 additions & 0 deletions
99
code/frameworks/tanstack-react/src/export-mocks/react-router.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { createRoute } from '@tanstack/react-router'; | ||
| export * from '@tanstack/react-router'; | ||
|
|
||
| import { fn } from 'storybook/test'; | ||
| import React from 'react'; | ||
| import { useEffect } from 'storybook/internal/preview-api'; | ||
|
|
||
| import { | ||
| useNavigate as _useNavigate, | ||
| useRouter as _useRouter, | ||
| useBlocker as _useBlocker, | ||
| useMatch as _useMatch, | ||
| useSearch as _useSearch, | ||
| useParams as _useParams, | ||
| useLocation as _useLocation, | ||
| useRouterState as _useRouterState, | ||
| useMatchRoute as _useMatchRoute, | ||
| useLoaderData as _useLoaderData, | ||
| useLoaderDeps as _useLoaderDeps, | ||
| useRouteContext as _useRouteContext, | ||
| useMatches as _useMatches, | ||
| useParentMatches as _useParentMatches, | ||
| useChildMatches as _useChildMatches, | ||
| useCanGoBack as _useCanGoBack, | ||
| useLinkProps as _useLinkProps, | ||
| } from '@tanstack/react-router'; | ||
| import type { Navigate as _Navigate } from '@tanstack/react-router'; | ||
| import { onNavigate } from './spies.ts'; | ||
|
|
||
| // Mock navigation hooks — backed by real implementations so they work in stories | ||
| export const useNavigate = fn(_useNavigate).mockName('@tanstack/react-router::useNavigate'); | ||
| export const useRouter = fn(_useRouter).mockName('@tanstack/react-router::useRouter'); | ||
| export const useBlocker = fn(_useBlocker).mockName('@tanstack/react-router::useBlocker'); | ||
| export const useSearch = fn(_useSearch).mockName('@tanstack/react-router::useSearch'); | ||
| export const useParams = fn(_useParams).mockName('@tanstack/react-router::useParams'); | ||
| export const useLocation = fn(_useLocation).mockName('@tanstack/react-router::useLocation'); | ||
| export const useRouterState = fn(_useRouterState).mockName( | ||
| '@tanstack/react-router::useRouterState' | ||
| ); | ||
| export const useLoaderData = fn(_useLoaderData).mockName('@tanstack/react-router::useLoaderData'); | ||
| export const useLoaderDeps = fn(_useLoaderDeps).mockName('@tanstack/react-router::useLoaderDeps'); | ||
| export const useRouteContext = fn(_useRouteContext).mockName( | ||
| '@tanstack/react-router::useRouteContext' | ||
| ); | ||
| export const useCanGoBack = fn(_useCanGoBack).mockName('@tanstack/react-router::useCanGoBack'); | ||
| export const useLinkProps = fn(_useLinkProps).mockName('@tanstack/react-router::useLinkProps'); | ||
|
|
||
| export const Outlet = () => null; | ||
|
|
||
| export const Navigate: typeof _Navigate = ({ to, href }) => { | ||
| useEffect(() => { | ||
| onNavigate({ to: (to as string) || href }); | ||
| }, [to, href]); | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| export const Link = ({ | ||
| to, | ||
| children, | ||
| ...props | ||
| }: { | ||
| to: string; | ||
| children?: React.ReactNode; | ||
| [key: string]: unknown; | ||
| }) => { | ||
| const location = useLocation(); | ||
| return React.createElement( | ||
| 'a', | ||
| { | ||
| href: to, | ||
| onClick: (e: React.MouseEvent) => { | ||
| e.preventDefault(); | ||
| onNavigate({ to, from: location.href }); | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ...props, | ||
| }, | ||
| children | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Override createFileRoute from tanstack react router | ||
| * because the org `createFileRoute` doesn't set the path in the Route | ||
| */ | ||
|
huang-julien marked this conversation as resolved.
|
||
| export function createFileRoute(path: string) { | ||
| return (options: any) => { | ||
| return createRoute({ | ||
| path, | ||
| ...options, | ||
| isRoot: false, | ||
| }).update({ | ||
| id: path, | ||
| path: path, | ||
| fullPath: path, | ||
| // any because tanstack router does that | ||
| } as any); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { fn } from 'storybook/test'; | ||
|
|
||
| export type NavigationEvent = { to?: string; from?: string }; | ||
|
|
||
| /** | ||
| * Spy called whenever navigation is attempted (Link click, useNavigate, etc.). | ||
| * Navigation is blocked — the story stays on screen — but the spy records | ||
| * `{ to, from }` so play functions can assert on it. | ||
| */ | ||
| export const onNavigate = fn<(event: NavigationEvent) => void>().mockName('navigate'); |
60 changes: 60 additions & 0 deletions
60
code/frameworks/tanstack-react/src/export-mocks/start-storage-context.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import type { StartStorageContext } from '@tanstack/start-storage-context'; | ||
|
|
||
| const START_CONTEXT_SYMBOL = Symbol.for('storybook.tanstack-react.start-storage-context'); | ||
|
|
||
| type BrowserStartGlobals = typeof globalThis & { | ||
| __TSR_ROUTER__?: unknown; | ||
| __TSS_START_OPTIONS__?: unknown; | ||
| [START_CONTEXT_SYMBOL]?: StartStorageContext; | ||
| }; | ||
|
|
||
| const browserGlobals = globalThis as BrowserStartGlobals; | ||
|
|
||
| function createFallbackStartContext(): StartStorageContext | undefined { | ||
| if ( | ||
| browserGlobals.__TSR_ROUTER__ === undefined && | ||
| browserGlobals.__TSS_START_OPTIONS__ === undefined | ||
| ) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { | ||
| getRouter: () => browserGlobals.__TSR_ROUTER__ as never, | ||
| request: new Request('http://localhost/'), | ||
| startOptions: browserGlobals.__TSS_START_OPTIONS__, | ||
| contextAfterGlobalMiddlewares: undefined, | ||
| executedRequestMiddlewares: new Set(), | ||
| }; | ||
| } | ||
|
|
||
| export async function runWithStartContext<T>( | ||
| context: StartStorageContext, | ||
| fn: () => T | Promise<T> | ||
| ): Promise<T> { | ||
| const previousContext = browserGlobals[START_CONTEXT_SYMBOL]; | ||
| browserGlobals[START_CONTEXT_SYMBOL] = context; | ||
|
|
||
| try { | ||
| return await fn(); | ||
| } finally { | ||
| if (previousContext) { | ||
| browserGlobals[START_CONTEXT_SYMBOL] = previousContext; | ||
| } else { | ||
| delete browserGlobals[START_CONTEXT_SYMBOL]; | ||
| } | ||
| } | ||
| } | ||
|
huang-julien marked this conversation as resolved.
|
||
|
|
||
| export function getStartContext<TThrow extends boolean = true>(opts?: { | ||
| throwIfNotFound?: TThrow; | ||
| }): TThrow extends false ? StartStorageContext | undefined : StartStorageContext { | ||
| const context = browserGlobals[START_CONTEXT_SYMBOL] ?? createFallbackStartContext(); | ||
|
|
||
| if (!context && opts?.throwIfNotFound !== false) { | ||
| throw new Error('No Start context found in Storybook mock.'); | ||
| } | ||
|
|
||
| return context as TThrow extends false ? StartStorageContext | undefined : StartStorageContext; | ||
| } | ||
|
|
||
| export type { StartStorageContext }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.