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

Commit

Permalink
🐛 Fixed: Avoid catching unwanted <Link> that can be children of a…
Browse files Browse the repository at this point in the history
… ``<PageContainer>`` instance

Now we cleanup listened parts of the PageContainer on each
mount/update, before re-executing catchLinks.
  • Loading branch information
MoOx committed Nov 22, 2016
1 parent 582d852 commit 8995758
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/_utils/catch-links/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ const catchLinks = (cb) => (ev: any): void => {
export default function(
elements: Array<Element>,
cb: Function
): void {
): Function {
const eventCallback = catchLinks(cb)
elements.map((el) => el.addEventListener("click", eventCallback))

return () => {
elements.map((el) => el.removeEventListener("click", eventCallback))
}
}
14 changes: 14 additions & 0 deletions src/components/PageContainer/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class PageContainer extends Component<DefaultProps, Props, void> {
this.catchInternalLink()
}

cleanupInternalLinks: Function

catchInternalLink() {
const layoutDOMElement = findDOMNode(this)

Expand All @@ -127,6 +129,18 @@ class PageContainer extends Component<DefaultProps, Props, void> {
if (!bodyContainers.length) {
bodyContainers = [ layoutDOMElement ]
}

// as soon as we listened to something, we should carefully unlisten
// because
// - it can be listening in the layoutDOMElement (a parent)
// - it can be listening in a <BodyContainer (a children)
// if we don't do this cleanup, we might listen on a parent and a children
// and if the parent contains other links to, let's say a parent part of
// the website, <Link> might be catched (but they are not supposed to)
if (this.cleanupInternalLinks) {
this.cleanupInternalLinks()
}
this.cleanupInternalLinks =
catchLinks(bodyContainers, (href) => {
// do not catch links that are under the current base path
if (href.indexOf(process.env.PHENOMIC_USER_PATHNAME) === -1) {
Expand Down

0 comments on commit 8995758

Please sign in to comment.