Skip to content

Commit 0b274c1

Browse files
committed
Ignore non-vanilla click events
1 parent 21c81d8 commit 0b274c1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/HashLink.jsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,19 @@ export function genericHashLink(As) {
107107
}
108108

109109
function handleClick(e) {
110+
console.log('HREER', e.defaultPrevented, e);
110111
reset();
111112
hashFragment = props.elementId ? `#${props.elementId}` : linkHash;
112113
if (props.onClick) props.onClick(e);
113-
if (hashFragment !== '') {
114+
if (
115+
hashFragment !== '' &&
116+
// ignore non-vanilla click events, same as react-router
117+
// below logic adapted from react-router: https://github.com/ReactTraining/react-router/blob/fc91700e08df8147bd2bb1be19a299cbb14dbcaa/packages/react-router-dom/modules/Link.js#L43-L48
118+
!e.defaultPrevented && // onClick prevented default
119+
e.button === 0 && // ignore everything but left clicks
120+
(!props.target || props.target === '_self') && // let browser handle "target=_blank" etc
121+
!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) // ignore clicks with modifier keys
122+
) {
114123
scrollFunction =
115124
props.scroll ||
116125
((el) =>
@@ -136,7 +145,7 @@ export const NavHashLink = genericHashLink(NavLink);
136145
if (process.env.NODE_ENV !== 'production') {
137146
HashLink.displayName = 'HashLink';
138147
NavHashLink.displayName = 'NavHashLink';
139-
148+
140149
const propTypes = {
141150
onClick: PropTypes.func,
142151
children: PropTypes.node,
@@ -145,7 +154,7 @@ if (process.env.NODE_ENV !== 'production') {
145154
elementId: PropTypes.string,
146155
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
147156
};
148-
157+
149158
HashLink.propTypes = propTypes;
150159
NavHashLink.propTypes = propTypes;
151160
}

0 commit comments

Comments
 (0)