signal-desktop/ts/components/emoji/Emoji.tsx

72 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-05-24 23:58:27 +00:00
import * as React from 'react';
import classNames from 'classnames';
2020-01-17 22:23:19 +00:00
import { emojiToImage, getImagePath, SkinToneKey } from './lib';
2019-05-24 23:58:27 +00:00
2020-08-20 22:04:59 +00:00
export const EmojiSizes = [16, 18, 20, 24, 28, 32, 48, 64, 66] as const;
export type EmojiSizeType = typeof EmojiSizes[number];
2019-05-24 23:58:27 +00:00
export type OwnProps = {
inline?: boolean;
2020-01-17 22:23:19 +00:00
emoji?: string;
shortName?: string;
2019-05-24 23:58:27 +00:00
skinTone?: SkinToneKey | number;
2020-08-20 22:04:59 +00:00
size?: EmojiSizeType;
children?: React.ReactNode;
2019-05-24 23:58:27 +00:00
};
export type Props = OwnProps &
Pick<React.HTMLProps<HTMLDivElement>, 'style' | 'className'>;
export const Emoji = React.memo(
React.forwardRef<HTMLDivElement, Props>(
(
{
style = {},
size = 28,
shortName,
skinTone,
2020-01-17 22:23:19 +00:00
emoji,
inline,
className,
children,
}: Props,
2019-05-24 23:58:27 +00:00
ref
) => {
2020-01-17 22:23:19 +00:00
const image = shortName
? getImagePath(shortName, skinTone)
: emoji
? emojiToImage(emoji)
: '';
const backgroundStyle = inline
? { backgroundImage: `url('${image}')` }
: {};
2019-05-24 23:58:27 +00:00
return (
<span
2019-05-24 23:58:27 +00:00
ref={ref}
className={classNames(
'module-emoji',
`module-emoji--${size}px`,
inline ? `module-emoji--${size}px--inline` : null,
2019-05-24 23:58:27 +00:00
className
)}
style={{ ...style, ...backgroundStyle }}
>
{inline ? (
// When using this component as a draft.js decorator it is very
// important that these children are the only elements to render
children
) : (
<img
className={`module-emoji__image--${size}px`}
src={image}
alt={shortName}
/>
)}
</span>
2019-05-24 23:58:27 +00:00
);
}
)
);