react-linkify
react-linkify copied to clipboard
The properties don't work when in Semantic UI <Comments>
I'm using Semantic UI to output a users comment and want to convert any links they enter innto hyperlniks. The link gets converted, but it ignores all the properties so the link doesn't open in a new tab, change, color, etc. Here is simple snippet.
<Linkify properties={{target: '_blank', style: {color: 'red', fontWeight: 'bold'}}}>
{message.content ? <Comment.Text>{message.content}</Comment.Text> : null }
</Linkify>
I've also tried moving the <Linkify> tag inside the <Comment.Text> and it made no difference. Any possible solution?
really could use the ability to set target to _blank 😬
anybody find a solution?
Solution:
const componentDecorator = (href, text, key) => (
<a href={href} key={key} target="_blank">
{text}
</a>
);
<Linkify componentDecorator={componentDecorator}>
some user input http://github.com
</Linkify>
@steverecio Good solution!
@steverecio Thank you!
@steverecio Really helpful. Many thanks
https://github.com/tasti/react-linkify/issues/78#issuecomment-514754050
This bug was introduced in 1.0.0-alpha. Release 0.2.2 does not have this issue.
Thanks @steverecio, this solution saved the day! This is what I ended up with to get noopener and punycode going:
import punycode from "punycode";
...
const componentDecorator = (href, text, key) => (
<a href={href} key={key} target="_blank" rel="noopener">
{punycode.toASCII(text)}
</a>
);
...
<Linkify componentDecorator={componentDecorator}>
...
</Linkify>
Seems properties doesn't work at all. Maybe it's missing to pass them to the default componentDecorator.
You can create a decorator like some of the other comments here have mentioned. If you want to open new tabs securely, especially if the links are leaving the site you control, I suggest implementing the following:
TypeScript supported CodeSandbox: https://codesandbox.io/s/cool-montalcini-vkmtb?file=/src/App.tsx
npm install react-secure-link
...then...
import { SecureLink } from "react-secure-link"
<Linkify componentDecorator={(decoratedHref, decoratedText, key) => (
<SecureLink href={decoratedHref} key={key}>{decoratedText}</SecureLink>
)}>
Here is a link that will open securely in a new tab: www.github.com.
</Linkify>
@dbudwin is the gist here to add rel="noopener noreferrer" to the <a /> tags?
@steverecio yes, that's effectively what it does. Just abstracts away the "how" of creating a secure link in a new tab. Nothing fancy.