-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor link/emoji/newline components for composability
- Loading branch information
1 parent
a5416e4
commit d9e5338
Showing
11 changed files
with
295 additions
and
95 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
### All newlines | ||
|
||
```jsx | ||
<AddNewLines text="\n\n\n" /> | ||
``` | ||
|
||
### Starting and ending with newlines | ||
|
||
```jsx | ||
<AddNewLines text="\nin between\n" /> | ||
``` | ||
|
||
### With newlines in the middle | ||
|
||
```jsx | ||
<AddNewLines text="Before \n\n after" /> | ||
``` | ||
|
||
### No newlines | ||
|
||
```jsx | ||
<AddNewLines text="This is the text" /> | ||
``` | ||
|
||
### Providing custom non-newline render function | ||
|
||
```jsx | ||
const renderNonNewLine = ({ text, key }) => ( | ||
<span key={key}>This is my custom content!</span> | ||
); | ||
<AddNewLines | ||
text="\n first \n second \n" | ||
renderNonNewLine={renderNonNewLine} | ||
/>; | ||
``` |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
### All emoji | ||
|
||
```jsx | ||
<Emojify text="🔥🔥🔥" /> | ||
``` | ||
|
||
### With skin color modifier | ||
|
||
```jsx | ||
<Emojify text="👍🏾" /> | ||
``` | ||
|
||
### With `sizeClass` provided | ||
|
||
```jsx | ||
<Emojify text="🔥" sizeClass="jumbo" /> | ||
``` | ||
|
||
```jsx | ||
<Emojify text="🔥" sizeClass="large" /> | ||
``` | ||
|
||
```jsx | ||
<Emojify text="🔥" sizeClass="medium" /> | ||
``` | ||
|
||
```jsx | ||
<Emojify text="🔥" sizeClass="small" /> | ||
``` | ||
|
||
```jsx | ||
<Emojify text="🔥" sizeClass="" /> | ||
``` | ||
|
||
### Starting and ending with emoji | ||
|
||
```jsx | ||
<Emojify text="🔥in between🔥" /> | ||
``` | ||
|
||
### With emoji in the middle | ||
|
||
```jsx | ||
<Emojify text="Before 🔥🔥 after" /> | ||
``` | ||
|
||
### No emoji | ||
|
||
```jsx | ||
<Emojify text="This is the text" /> | ||
``` | ||
|
||
### Providing custom non-link render function | ||
|
||
```jsx | ||
const renderNonEmoji = ({ text, key }) => ( | ||
<span key={key}>This is my custom content</span> | ||
); | ||
<Emojify text="Before 🔥🔥 after" renderNonEmoji={renderNonEmoji} />; | ||
``` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
### All link | ||
|
||
```jsx | ||
<Linkify text="https://somewhere.com" /> | ||
``` | ||
|
||
### Starting and ending with link | ||
|
||
```jsx | ||
<Linkify text="https://somewhere.com Yes? No? https://anotherlink.com" /> | ||
``` | ||
|
||
### With a link in the middle | ||
|
||
```jsx | ||
<Linkify text="Before. https://somewhere.com After." /> | ||
``` | ||
|
||
### No link | ||
|
||
```jsx | ||
<Linkify text="Plain text" /> | ||
``` | ||
|
||
### Should not render as link | ||
|
||
```jsx | ||
<Linkify text="smailto:someone@somewhere.com - ftp://something.com - //local/share - \\local\share" /> | ||
``` | ||
|
||
### Should render as link | ||
|
||
```jsx | ||
<Linkify text="github.com - https://blah.com" /> | ||
``` | ||
|
||
### Providing custom non-link render function | ||
|
||
```jsx | ||
const renderNonLink = ({ text, key }) => ( | ||
<span key={key}>This is my custom non-link content!</span> | ||
); | ||
<Linkify text="Before github.com After" renderNonLink={renderNonLink} />; | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from 'react'; | ||
|
||
import createLinkify from 'linkify-it'; | ||
|
||
import { RenderTextCallback } from '../../types/Util'; | ||
|
||
const linkify = createLinkify(); | ||
|
||
interface Props { | ||
text: string; | ||
/** Allows you to customize now non-links are rendered. Simplest is just a <span>. */ | ||
renderNonLink?: RenderTextCallback; | ||
} | ||
|
||
const SUPPORTED_PROTOCOLS = /^(http|https):/i; | ||
|
||
export class Linkify extends React.Component<Props, {}> { | ||
public static defaultProps: Partial<Props> = { | ||
renderNonLink: ({ text, key }) => <span key={key}>{text}</span>, | ||
}; | ||
|
||
public render() { | ||
const { text, renderNonLink } = this.props; | ||
const matchData = linkify.match(text) || []; | ||
const results: Array<any> = []; | ||
let last = 0; | ||
let count = 1; | ||
|
||
// We have to do this, because renderNonLink is not required in our Props object, | ||
// but it is always provided via defaultProps. | ||
if (!renderNonLink) { | ||
return; | ||
} | ||
|
||
if (matchData.length === 0) { | ||
return renderNonLink({ text, key: 0 }); | ||
} | ||
|
||
matchData.forEach( | ||
(match: { | ||
index: number; | ||
url: string; | ||
lastIndex: number; | ||
text: string; | ||
}) => { | ||
if (last < match.index) { | ||
const textWithNoLink = text.slice(last, match.index); | ||
results.push(renderNonLink({ text: textWithNoLink, key: count++ })); | ||
} | ||
|
||
const { url, text: originalText } = match; | ||
if (SUPPORTED_PROTOCOLS.test(url)) { | ||
results.push( | ||
<a key={count++} href={url}> | ||
{originalText} | ||
</a> | ||
); | ||
} else { | ||
results.push(renderNonLink({ text: originalText, key: count++ })); | ||
} | ||
|
||
last = match.lastIndex; | ||
} | ||
); | ||
|
||
if (last < text.length) { | ||
results.push(renderNonLink({ text: text.slice(last), key: count++ })); | ||
} | ||
|
||
return results; | ||
} | ||
} |
Oops, something went wrong.