Skip to content
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

feat: bypass Link click handler if alt click or defaultPrevented #1249

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rmarscher
Copy link
Collaborator

Fixes #1248.

Copy link

vercel bot commented Feb 19, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
waku ✅ Ready (Inspect) Visit Preview Mar 28, 2025 1:19pm

Copy link

codesandbox-ci bot commented Feb 19, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@rmarscher
Copy link
Collaborator Author

I think this is a draft until we create an e2e test.

Comment on lines 272 to 274
if (!isAltClick) {
event.preventDefault();
}
Copy link
Member

Choose a reason for hiding this comment

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

Why can it be this?

if (isAltClick) {
  return;
}

Copy link
Collaborator Author

@rmarscher rmarscher Feb 20, 2025

Choose a reason for hiding this comment

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

That skips the onClick prop. What do you think about firing the onClick prop first? React Router does that and then doesn't run the internal handler if the onClick calls preventDefault().
https://github.com/remix-run/react-router/blob/react-router%407.2.0/packages/react-router/lib/dom/lib.tsx#L628-L635

function handleClick(
  event: React.MouseEvent<HTMLAnchorElement, MouseEvent>
) {
  if (onClick) onClick(event);
  if (!event.defaultPrevented) {
    internalOnClick(event);
  }
}

Copy link
Member

Choose a reason for hiding this comment

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

Oh, that looks great. Any drawbacks?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not that I can think of. It looks good to me. I'm traveling for the next few days so I probably won't push another update until the beginning of next week.

Copy link
Member

Choose a reason for hiding this comment

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

ping

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The last two weeks flew by 😅 But yes, I was also just looking at the open issues and would like to get all of these loose ends wrapped up asap.

@rmarscher rmarscher force-pushed the feat/link-allow-new-tab branch from 08e2b4d to 1bcc4b4 Compare March 27, 2025 19:58
Copy link

codesandbox-ci bot commented Mar 27, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@rmarscher rmarscher changed the title feat: unstable_defaultAltClick link prop, no route change if enabled feat: bypass Link click handler if alt click or defaultPrevented Mar 27, 2025
@rmarscher rmarscher marked this pull request as ready for review March 27, 2025 20:24
@@ -81,6 +81,13 @@ const parseRouteFromLocation = (): RouteProps => {
return parseRoute(new URL(window.location.href));
};

function isAltClick(event: MouseEvent<HTMLAnchorElement>) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you make it an arrow function? (Just a preference)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All set

@@ -265,8 +272,7 @@ export function Link({
};
}
}, [unstable_prefetchOnView, router, to]);
const onClick = (event: MouseEvent<HTMLAnchorElement>) => {
event.preventDefault();
const internalOnClick = (_event: MouseEvent<HTMLAnchorElement>) => {
Copy link
Member

Choose a reason for hiding this comment

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

It's not confusing without it.

Suggested change
const internalOnClick = (_event: MouseEvent<HTMLAnchorElement>) => {
const internalOnClick = () => {

@@ -265,8 +272,7 @@ export function Link({
};
}
}, [unstable_prefetchOnView, router, to]);
const onClick = (event: MouseEvent<HTMLAnchorElement>) => {
event.preventDefault();
Copy link
Member

Choose a reason for hiding this comment

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

Removing this causes e2e tests to fail, doesn't it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that was a mistake. I thought I moved it, not deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

It should be possible to open a Link in a new tab using browser default behavior
2 participants