Skip to content

Commit

Permalink
fix: Modifier (cmd, shift, ctrl) clicking on BaseLink Links modifie…
Browse files Browse the repository at this point in the history
…s current page view & url (#1335)

Fixes #1301.

This fix intercepts the click event and stops event propagation on the
current tab (what causes the view to change). Covers all bases with
event propagation (both native and synthetic events).
  • Loading branch information
daniel-ji authored Nov 27, 2024
1 parent 544297c commit aed70fb
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions frontend/packages/data-portal/app/components/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,25 @@ function BaseLink(
}
}

// Handle modifier clicks and prevent default behavior
const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
// cmd, ctrl, shift, or middle click
const isModifierClick =
event.metaKey || event.ctrlKey || event.shiftKey || event.button === 1

if (isModifierClick) {
event.preventDefault()
event.stopPropagation()
event.nativeEvent.stopImmediatePropagation()
window.open(event.currentTarget.href, '_blank', 'noopener,noreferrer')
}
}

return (
<RemixLink
ref={ref}
to={url}
onClick={handleClick}
className={cnsNoMerge(
variant === 'dashed-bordered' && DASHED_BORDERED_CLASSES,
variant === 'dashed-underlined' && DASHED_UNDERLINED_CLASSES,
Expand Down

0 comments on commit aed70fb

Please sign in to comment.