Skip to content

Instantly share code, notes, and snippets.

@neetigyachahar
Last active May 30, 2020 10:55
Show Gist options
  • Save neetigyachahar/0b9e308d4659d3ef03a592ddeecc43f2 to your computer and use it in GitHub Desktop.
Save neetigyachahar/0b9e308d4659d3ef03a592ddeecc43f2 to your computer and use it in GitHub Desktop.
React Higher Order component to implement accessibility of emojis in text.
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