emoji-picker-react icon indicating copy to clipboard operation
emoji-picker-react copied to clipboard

Feature request: Emoji text renderer

Open Sodj opened this issue 2 years ago • 6 comments

It would be very helpful to have a function that takes text containing emojis and returns react object that converts the character emojis to component emojis using <Emoji .../>

Sodj avatar Oct 25 '22 17:10 Sodj

Should be pretty straightforward to implement really. I'm overloaded with other work (both OSS and work), but I'd love to see a PR. The basis for this would be to split the string, identify the emojis, then map into components.

Obviously, this would work with simple strings, but what if you want to have markup within that string? Need to think how to handle that as well.

ealush avatar Oct 25 '22 18:10 ealush

I know it should be straight forward but I did try to do just that but failed. Then thought it would be neat if it came with the package

Sodj avatar Oct 25 '22 19:10 Sodj

I'd love to help with guidance on this.

Could you give a sample string that you've tried to convert and try to explain what you attempted?

ealush avatar Oct 25 '22 19:10 ealush

Well I deleted my attempt but it was like this:

var msg = "Hello 👋 world"
var output = msg.split(emojiRegex) 
// gives : [hello, 👋, world]

return 
<div>
{output.map(e=>{
  if(isEmoji(e)) return <Emoji unified={emojiToUnicode(e)} />
  else return e
})}
</div>

But I was getting the same error I got when I passed wrong unicode

My issue was most likely in either the emojiRegex or the emojiToUnicode, if you could suggest some reliable regex and converter that would be superb

Sodj avatar Oct 25 '22 20:10 Sodj

@ealush Update: Upon further testing I found that the issue is in my emojiToUnicode function which sometimes returns NaN for special emojis like ♥,🇦🇫 and ♥️ (different heart)

It would be nice if the Emoji component could take either unified or emoji as param like <Emoji emoji="🇦🇫" /> Or have an exported function from the module emojiToUnicode

Also it would be nice if the component would just render nothing instead of throwing an error when the unified code isn't correct

Sodj avatar Oct 26 '22 10:10 Sodj

Here is a workaround - might submit a PR to implement this:

// based on https://stackoverflow.com/a/58691389/567524
const emojiToUnified = (emoji: string) => {
    const code = [...emoji]
        .map((x) => x.codePointAt(0) ?? 0)
        .filter(n => n > 0xfff || n === 0x200d || (0xfe00 <= n && n <= 0xfeff))
        .map(x => x.toString(16))
        .join(`-`);
    console.log(`emojiToUnicode`, { emoji, code });
    return code;
};

// usage:
<Emoji unified={emojiToUnified(value)} />

ricklove avatar Jun 26 '23 22:06 ricklove