Created
June 15, 2016 08:20
-
-
Save shprink/676333fa85bc7c0506224ba92053aa3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import {Link as ReactLink} from 'react-router'; | |
export default class Link extends React.Component { | |
parseTo(to) { | |
let parser = document.createElement('a'); | |
parser.href = to; | |
return parser; | |
} | |
isInternal(toLocation) { | |
return window.location.host === toLocation.host; | |
} | |
render() { | |
const {to, children, ...rest} = this.props; | |
const toLocation = this.parseTo(to); | |
const isInternal = this.isInternal(toLocation); | |
if (isInternal) { | |
return (<ReactLink to={toLocation.pathname} {...rest}>{children}</ReactLink>); | |
} else { | |
return (<a href={to} target="_blank" {...rest}>{children}</a>); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment