Skip to content

Tanstack: Add @storybook/tanstack-react package#34403

Merged
huang-julien merged 154 commits into
nextfrom
julien/tanstack
May 5, 2026
Merged

Tanstack: Add @storybook/tanstack-react package#34403
huang-julien merged 154 commits into
nextfrom
julien/tanstack

Conversation

@huang-julien
Copy link
Copy Markdown
Contributor

@huang-julien huang-julien commented Mar 30, 2026

#34284

What I did

This PR introduces first-class support for TanStack apps in Storybook via a new @storybook/tanstack-react framework integration, including router-aware story rendering, TanStack start and router compatible mocks.

Build

Tanstack start plugins removal in the preset file

Tanstack start mostly provide server features and SSR capabilities to tanstack router. Since storybook is client-only, we strip out any tanstack start plugins

Server code elimination

Added server-side code elimination/plugin behavior to prevent server-only modules from breaking browser Storybook runtime.

It is similar to https://github.com/TanStack/router/blob/main/packages/start-plugin-core/src/start-compiler/compiler.ts#L965 but without needing a server environement.

Runtime

createFileRoute

Most tanstack router users uses createFileRoute to create a file based tanstack route.
However, the path is ignored when calling createFileRoute(route)(options), the implementation will provide it's own createFileRoute to force the path being set in the returned route object.

Page extraction from route

The implementation provides loader and decorator to extract a page component from a route if the user did not provide a component through the meta or story object.
In most cases, tanstack router page files doesn't export the page component. Only the route itself.
This is a DX improvement and to avoid storybook impacting users project.

Server functions

All server functions are mocked by default, the build-time storybook server code elimination plugins strips out any server code dependency/imports

Types

The framework ships a typed parameter layer that threads route inference through stories end-to-end:

parameters.tanstack.router can be passed to Meta<> to propagate route typing across a story file without repeating the route generic.
This means incorrect route params, unknown override paths, or wrong search shapes are caught at compile time rather than at runtime in the browser.

Checklist for Contributors

Testing

  • test the tanstack integration with a transtack react router project
  • test with tanstack start
  • verify things are not breaking in a monorepo
  • test on windows

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

react router + start

  • npx @tanstack/cli create --router-only
  • npm create storybook@10.4.0-alpha.17
  • verify stories works
    • Without a router (empty storybook)
    • With a route createFileRoute to parameters.tanstack.router.route
    • With the whole app tree routeTree to parameters.tanstack.router.route
      • Verify type constraints works on
        • routeOverrides
        • path
        • params

start additionnal tests

  • verify stories works
    • Add some server handlers in the project with server only code (test server code elimination)
      • Without a router (empty storybook)
      • With a route createFileRoute to parameters.tanstack.router.route
      • With the whole app tree routeTree to parameters.tanstack.router.route
        • Verify type constraints works on
          • routeOverrides
          • path
          • params

Caution

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

  1. Run a sandbox for template, e.g. yarn task --task sandbox --start-from auto --template tanstack-react/default-ts
  2. Open Storybook in your browser
  3. Access and view all sotries

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This pull request has been released as version 0.0.0-pr-34403-sha-14169208. Try it out in a new sandbox by running npx storybook@0.0.0-pr-34403-sha-14169208 sandbox or in an existing project with npx storybook@0.0.0-pr-34403-sha-14169208 upgrade.

More information
Published version 0.0.0-pr-34403-sha-14169208
Triggered by @huang-julien
Repository storybookjs/storybook
Branch julien/tanstack
Commit 14169208
Datetime Mon May 4 09:03:09 UTC 2026 (1777885389)
Workflow run 25310407446

To request a new release of this pull request, mention the @storybookjs/core team.

core team members can create a new canary release here or locally with gh workflow run --repo storybookjs/storybook publish.yml --field pr=34403

Summary by CodeRabbit

  • New Features
    • First-class TanStack React integration: router + query support, typed Storybook APIs, Vite-based preset, preview decorators/loaders, runtime mocks for Start/Router, and CLI generator + project templates.
  • Documentation
    • Added framework README and example templates demonstrating TanStack React + Storybook usage.

valentinpalkovic and others added 30 commits February 3, 2026 09:38
…amework with support for story, routeTree, and instance modes. Introduce helper functions for creating memory routers and update documentation with routing examples.
…y removing unnecessary server type definition and enhancing context and createRouter types for better type safety.
…streamline the Vite setup for the TanStack React framework.
- Added `createServerFnStub` and `resetAllServerFnStubs` for mocking server functions.
- Introduced integration tests for server function stubs demonstrating various scenarios.
- Updated `preset.ts` to include a Vite plugin for handling server function stubs.
- Enhanced `preview.tsx` to reset server function stubs between stories.
- Created new story examples showcasing loaders and pathless layouts with mocked data.
- Removed outdated router example stories and components.
Copy link
Copy Markdown
Contributor

@JReinhold JReinhold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome job 🎉 Only very small comments, the most important ones being about the Babel dependency.

Comment thread code/frameworks/tanstack-react/src/export-mocks/start-server.ts Outdated
Comment thread code/frameworks/tanstack-react/src/plugins/server-code-elimination.ts Outdated
Comment thread code/frameworks/tanstack-react/src/plugins/server-code-elimination.ts Outdated
Comment thread code/frameworks/tanstack-react/src/plugins/server-only-stub.ts Outdated
Comment thread code/frameworks/tanstack-react/src/plugins/server-only-stub.ts
Comment thread code/frameworks/tanstack-react/template/cli/ts/Page.tsx
Comment thread code/frameworks/tanstack-react/package.json Outdated
Comment thread code/frameworks/tanstack-react/package.json Outdated
Comment thread code/frameworks/tanstack-react/package.json Outdated
Comment thread code/frameworks/tanstack-react/package.json Outdated
huang-julien and others added 11 commits May 4, 2026 15:54
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
…tion.ts

Co-authored-by: Jeppe Reinhold <jeppe@chromatic.com>
Co-authored-by: Jeppe Reinhold <jeppe@reinhold.is>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Comment thread code/frameworks/tanstack-react/template/cli/ts/Page.stories.ts Outdated
Copy link
Copy Markdown
Contributor

@JReinhold JReinhold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM™

Comment thread code/frameworks/tanstack-react/src/export-mocks/start.ts
Comment on lines +45 to +57
const isTanStackStartPlugin = (p: unknown): boolean => {
if (Array.isArray(p)) {
return p.some(isTanStackStartPlugin);
}
const pluginRecord = p as Record<string, unknown>;
return (
typeof p === 'object' &&
p !== null &&
'name' in pluginRecord &&
typeof pluginRecord.name === 'string' &&
(pluginRecord.name.startsWith('tanstack-start') || pluginRecord.name.includes('rsc:'))
);
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM. I'm sure the utility function can be modified to support startsWith/includes

@huang-julien huang-julien merged commit 53e094a into next May 5, 2026
138 of 140 checks passed
@huang-julien huang-julien deleted the julien/tanstack branch May 5, 2026 08:53
@github-actions github-actions Bot mentioned this pull request May 5, 2026
10 tasks
@JReinhold JReinhold changed the title Feat: Storybook/tanstack-react Tanstack: Add @storybook/tanstack-react package May 5, 2026
@github-actions github-actions Bot mentioned this pull request May 5, 2026
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants