Skip to content

[Bug] unstable_ViewTransition is undefined in Storybook when experimental.viewTransition is enabled in Next.js #52

@elecdeer

Description

@elecdeer

Describe the bug

As of Next.js 15.2, we can use the unstable_ViewTransition component from react@experimental by enabling the experimental.viewTransition flag in next.config.js. You can find more details in the Next.js documentation.

However, when using @storybook/nextjs-vite, any attempt to import { unstable_ViewTransition } from "react" within a story results in unstable_ViewTransition being undefined. This prevents the story from rendering.

Steps to reproduce the behavior

  1. Enable viewTransition in next.config.mjs:
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    viewTransition: true,
  },
};

export default nextConfig;
  1. Create a component that imports unstable_ViewTransition:
import { unstable_ViewTransition as ViewTransition } from "react";

// This logs the React component object when running `next dev`.
// This logs `undefined` when running `storybook dev` or `vitest`.
console.log(ViewTransition);

Expected behavior

unstable_ViewTransition should be correctly imported and available for use within Storybook stories, just as it is in a standard Next.js application when the experimental.viewTransition flag is enabled.

Screenshots and/or logs

If applicable, add screenshots and/or logs to help explain your problem.

Environment

  • Next.js: 15.3.4
  • vite-plugin-storybook-nextjs: 2.0.2

Additional context

It seems the Next.js bundler has specific logic to alias react to next/dist/compiled/react-experimental when the viewTransition flag is active. You can see this implementation in the Next.js source code here:

https://github.com/vercel/next.js/blob/b5ced37273d49cefa447c3e87895fda338f1afe3/packages/next/src/build/webpack-config.ts#L391-L393

It appears that similar logic has not yet been implemented in vite-plugin-storybook-nextjs. This likely causes Vite to resolve the standard React module instead of the experimental one that includes ViewTransition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions