Last active
May 24, 2022 07:28
-
-
Save Armster15/9904b27c565ce8365954f6d8c9ee89b9 to your computer and use it in GitHub Desktop.
An emoji component for React that has accessibility and Twemoji support!
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
import React, { useRef, useEffect } from "react"; | |
import { SpanPropsWithoutRef } from "react-html-props"; | |
import assert from "assert"; | |
import twemoji from "twemoji"; | |
export interface EmojiProps extends SpanPropsWithoutRef { | |
symbol: string; | |
} | |
export const Emoji = ({ symbol, "aria-label": ariaLabel, ...props }: EmojiProps) => { | |
const ref = useRef<HTMLSpanElement>(null); | |
useEffect(() => { | |
assert(ref.current !== null); | |
twemoji.parse(ref.current, { | |
folder: "svg", | |
ext: ".svg", | |
}); | |
}); | |
return ( | |
<span | |
ref={ref} | |
aria-hidden={ariaLabel ? undefined : true} | |
role="img" | |
{...props} | |
> | |
{symbol} | |
</span> | |
); | |
}; |
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 CSS is required, make sure its global */ | |
img.emoji { | |
height: 1em; | |
width: 1em; | |
margin: 0 .05em 0 .1em; | |
vertical-align: -0.1em; | |
display: inline-block; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment