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

[1.x] Restore command + click behavior on Inertia links in React #2132

Merged
merged 1 commit into from
Dec 13, 2024
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
11 changes: 10 additions & 1 deletion packages/core/src/shouldIntercept.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
export default function shouldIntercept(event: MouseEvent | KeyboardEvent): boolean {
// The actual event passed to this function could be a native JavaScript event
// or a React synthetic event, so we are picking just the keys needed here (that
// are present in both types).

export default function shouldIntercept(
event: Pick<
MouseEvent,
'altKey' | 'ctrlKey' | 'defaultPrevented' | 'target' | 'currentTarget' | 'metaKey' | 'shiftKey' | 'button'
>,
): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There might be a more elegant way to do this? I didn't want to introduce a dependency on React type definitions in the core package (to make this a union type), so figured the next best thing was to narrow this down to accommodate both native and synthetic event shapes.

I confirmed that TypeScript will fail if this is type is just a MouseEvent, since React.MouseEvent contains some different stuff.

const isLink = (event.currentTarget as HTMLElement).tagName.toLowerCase() === 'a'
return !(
(event.target && (event?.target as HTMLElement).isContentEditable) ||
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const Link = forwardRef<unknown, InertiaLinkProps>(
(event: React.MouseEvent) => {
onClick(event)

if (shouldIntercept(event.nativeEvent)) {
if (shouldIntercept(event)) {
event.preventDefault()

router.visit(href, {
Expand Down
Loading