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

Merged
merged 7 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions e2e/fs-router.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { expect } from '@playwright/test';

import { test, prepareStandaloneSetup } from './utils.js';
Expand Down Expand Up @@ -81,5 +81,18 @@
await page.goto(`http://localhost:${port}/_components/Counter`);
await expect(page.getByText('404 Not Found')).toBeVisible();
});

test('alt click', async ({ page }) => {
await page.goto(`http://localhost:${port}`);
await expect(page.getByRole('heading', { name: 'Home' })).toBeVisible();
await page.click("a[href='/foo']", {
button: 'right',
});
await expect(page.getByRole('heading', { name: 'Home' })).toBeVisible();
await page.click("a[href='/foo']", {
modifiers: ['ControlOrMeta'],
});
await expect(page.getByRole('heading', { name: 'Home' })).toBeVisible();
});
});
}
17 changes: 14 additions & 3 deletions packages/waku/src/router/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ const parseRouteFromLocation = (): RouteProps => {
return parseRoute(new URL(window.location.href));
};

const isAltClick = (event: MouseEvent<HTMLAnchorElement>) =>
event.button !== 0 ||
!!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);

let savedRscParams: [query: string, rscParams: URLSearchParams] | undefined;

const createRscParams = (query: string): URLSearchParams => {
Expand Down Expand Up @@ -265,8 +269,7 @@ export function Link({
};
}
}, [unstable_prefetchOnView, router, to]);
const onClick = (event: MouseEvent<HTMLAnchorElement>) => {
event.preventDefault();
const internalOnClick = () => {
const url = new URL(to, window.location.href);
if (url.href !== window.location.href) {
const route = parseRoute(url);
Expand All @@ -284,7 +287,15 @@ export function Link({
changeRoute(route, { shouldScroll: scroll ?? newPath });
});
}
props.onClick?.(event);
};
const onClick = (event: MouseEvent<HTMLAnchorElement>) => {
if (props.onClick) {
props.onClick(event);
}
if (!event.defaultPrevented && !isAltClick(event)) {
event.preventDefault();
internalOnClick();
}
};
const onMouseEnter = unstable_prefetchOnEnter
? (event: MouseEvent<HTMLAnchorElement>) => {
Expand Down
Loading