Last active
May 30, 2020 10:55
-
-
Save neetigyachahar/0b9e308d4659d3ef03a592ddeecc43f2 to your computer and use it in GitHub Desktop.
React Higher Order component to implement accessibility of emojis in text.
This file contains hidden or 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 emojiTree from 'emoji-tree'; | |
const cureEmoji = props => { | |
let textChuck = ''; | |
let formattedText = []; | |
const brokenString = [...props.text]; | |
brokenString.forEach((char, index) => { | |
if (emojiTree(char)[0].type === 'emoji') { | |
if (textChuck !== '') { | |
formattedText.push(<span key={index - 1}>{textChuck}</span>); | |
textChuck = ''; | |
} | |
formattedText.push( | |
<span | |
key={index} | |
className="emoji" | |
role="img" | |
aria-hidden="true" | |
> | |
{char} | |
</span > | |
); | |
} else { | |
textChuck += char; | |
} | |
if (brokenString.length - 1 === index && textChuck !== '') { | |
formattedText.push(<span key={index}>{textChuck}</span>); | |
textChuck = ''; | |
} | |
}); | |
return ( | |
[ | |
...formattedText | |
] | |
) | |
}; | |
export default cureEmoji; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment