Skip to content

Instantly share code, notes, and snippets.

@Armster15
Last active May 24, 2022 07:28
Show Gist options
  • Save Armster15/9904b27c565ce8365954f6d8c9ee89b9 to your computer and use it in GitHub Desktop.
Save Armster15/9904b27c565ce8365954f6d8c9ee89b9 to your computer and use it in GitHub Desktop.
An emoji component for React that has accessibility and Twemoji support!
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 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