react-linkify icon indicating copy to clipboard operation
react-linkify copied to clipboard

The properties don't work when in Semantic UI <Comments>

Open ElixirMike opened this issue 6 years ago • 13 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?

ElixirMike avatar Jun 15 '19 19:06 ElixirMike

really could use the ability to set target to _blank 😬

dlombardi avatar Jun 25 '19 15:06 dlombardi

anybody find a solution?

steverecio avatar Jul 24 '19 18:07 steverecio

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 avatar Jul 24 '19 18:07 steverecio

@steverecio Good solution!

LaurentiuCotaga avatar Aug 07 '19 12:08 LaurentiuCotaga

@steverecio Thank you!

amandasavluchinske avatar Aug 09 '19 18:08 amandasavluchinske

@steverecio Really helpful. Many thanks

sydinh avatar Sep 19 '19 10:09 sydinh

https://github.com/tasti/react-linkify/issues/78#issuecomment-514754050

mhemrg avatar Dec 03 '19 09:12 mhemrg

This bug was introduced in 1.0.0-alpha. Release 0.2.2 does not have this issue.

evoyy avatar Jan 29 '20 21:01 evoyy

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>

nodabladam avatar Feb 11 '20 03:02 nodabladam

Seems properties doesn't work at all. Maybe it's missing to pass them to the default componentDecorator.

seromenho avatar Jun 12 '20 14:06 seromenho

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 avatar Dec 26 '20 02:12 dbudwin

@dbudwin is the gist here to add rel="noopener noreferrer" to the <a /> tags?

steverecio avatar Dec 26 '20 21:12 steverecio

@steverecio yes, that's effectively what it does. Just abstracts away the "how" of creating a secure link in a new tab. Nothing fancy.

dbudwin avatar Dec 26 '20 21:12 dbudwin