Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
fix: enable 'open in new tab' functionality when clicking link with c…
Browse files Browse the repository at this point in the history
…trl/cmd
  • Loading branch information
0xpatrickdev committed May 8, 2019
1 parent 3b21155 commit 95f7bb2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/components/TransitionLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import PropTypes from "prop-types";
import { Link } from "gatsby";

import { shouldNavigate } from "../utils/shouldNavigate";
import { triggerTransition } from "../utils/triggerTransition";
import { Consumer } from "../context/createTransitionContext";

Expand Down Expand Up @@ -31,20 +32,21 @@ const TransitionLink = ({
activeClassName={activeClassName}
partiallyActive={partiallyActive}
onClick={event => {
triggerTransition({
event,
to,
exit,
entry,
trigger,
replace,
linkState: state,
...context
});

if (shouldNavigate(event)) {
triggerTransition({
event,
to,
exit,
entry,
trigger,
replace,
linkState: state,
...context
});
};
if (typeof onClick === "function") {
onClick(event);
}
};
}}
to={to} // use gatsby link so prefetching still happens. this is prevent defaulted in triggertransition
{...rest}
Expand Down
12 changes: 12 additions & 0 deletions src/utils/shouldNavigate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* adapted from @reach/router implementation:
* defintion: https://github.com/reach/router/blob/master/src/index.js#L542-L545
* usage: https://github.com/reach/router/blob/master/src/index.js#L391-L397
*/

const shouldNavigate = event =>
!event.defaultPrevented &&
event.button === 0 &&
!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);

export { shouldNavigate };

0 comments on commit 95f7bb2

Please sign in to comment.