Skip to content

Next.js: Handle legacyBehavior prop in Link mock component #33862

Merged
valentinpalkovic merged 1 commit into
storybookjs:nextfrom
yatishgoel:fix/nextjs-link-legacy-behavior
Feb 18, 2026
Merged

Next.js: Handle legacyBehavior prop in Link mock component #33862
valentinpalkovic merged 1 commit into
storybookjs:nextfrom
yatishgoel:fix/nextjs-link-legacy-behavior

Conversation

@yatishgoel
Copy link
Copy Markdown
Contributor

@yatishgoel yatishgoel commented Feb 18, 2026

Closes #33861

What I did

Fixed the Next.js Link mock component to properly handle the legacyBehavior prop. Previously, MockLink always rendered its own <a> tag, which caused nested <a> tags when users passed legacyBehavior with a child <a> element.

When legacyBehavior is true, the mock now uses React.cloneElement to inject props (href, onClick, ref) onto the child element instead of wrapping it in an <a>, matching the real Next.js Link behavior.

Checklist for Contributors

Testing

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

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

Manual testing

  1. Run a sandbox with a Next.js template
  2. Create a story using <Link href="/about" legacyBehavior><a>About</a></Link>
  3. Verify it renders a single <a> tag (not nested <a> tags)
  4. Verify the click action is still logged in the Actions panel
  5. Also verify that non-legacy <Link href="/about">About</Link> still works as before

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 PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

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

Summary by CodeRabbit

  • New Features
    • Added legacyBehavior support to the Link component mock, enabling proper handling of child elements with ref, href, and onClick passthrough when legacy behavior is enabled.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 18, 2026

No actionable comments were generated in the recent review. 🎉


📝 Walkthrough

Walkthrough

Adds legacyBehavior support to the mock Next.js Link component. When enabled, the component resolves the child element, constructs merged props with ref and onClick handling, conditionally injects href, and returns the cloned child. Otherwise, standard anchor rendering is used.

Changes

Cohort / File(s) Summary
Link component legacyBehavior support
code/frameworks/nextjs/src/export-mocks/link/index.tsx
Introduces conditional logic for legacyBehavior mode: resolves child element, constructs childProps with ref, prevents default onClick while delegating to child's onClick before invoking linkAction, conditionally injects href based on passHref or child anchor state, and returns cloned child element with merged props.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

✨ Finishing Touches
  • 📝 Generate docstrings

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Comment @coderabbitai help to get the list of available commands and usage tips.

@valentinpalkovic valentinpalkovic self-assigned this Feb 18, 2026
@valentinpalkovic valentinpalkovic changed the title Fix: Handle legacyBehavior prop in Next.js Link mock component Next.js: Handle legacyBehavior prop in Link mock component Feb 18, 2026
@valentinpalkovic valentinpalkovic moved this to In Progress in Core Team Projects Feb 18, 2026
@valentinpalkovic valentinpalkovic moved this from In Progress to On Hold in Core Team Projects Feb 18, 2026
@valentinpalkovic valentinpalkovic added the patch:yes Bugfix & documentation PR that need to be picked to main branch label Feb 18, 2026
@valentinpalkovic valentinpalkovic merged commit e043b98 into storybookjs:next Feb 18, 2026
120 of 131 checks passed
@github-project-automation github-project-automation Bot moved this from On Hold to Done in Core Team Projects Feb 18, 2026
valentinpalkovic added a commit that referenced this pull request Feb 23, 2026
…vior

Next.js: Handle legacyBehavior prop in Link mock component

(cherry picked from commit e043b98)
@github-actions github-actions Bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug ci:normal nextjs patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug]: #31251 breaks Next.js links with legacyBehavior

2 participants