2025-07-31 19:30:14 +02:00
|
|
|
import type { ComponentPropsWithoutRef, ElementType } from 'react';
|
2025-07-22 16:43:15 +02:00
|
|
|
|
2025-08-04 19:16:12 +02:00
|
|
|
import { isModernEmojiEnabled } from '@/mastodon/utils/environment';
|
|
|
|
|
|
2025-07-31 19:30:14 +02:00
|
|
|
import { useEmojify } from './hooks';
|
|
|
|
|
import type { CustomEmojiMapArg } from './types';
|
2025-07-22 16:43:15 +02:00
|
|
|
|
2025-07-31 19:30:14 +02:00
|
|
|
type EmojiHTMLProps<Element extends ElementType = 'div'> = Omit<
|
|
|
|
|
ComponentPropsWithoutRef<Element>,
|
2025-07-22 16:43:15 +02:00
|
|
|
'dangerouslySetInnerHTML'
|
|
|
|
|
> & {
|
|
|
|
|
htmlString: string;
|
2025-07-31 19:30:14 +02:00
|
|
|
extraEmojis?: CustomEmojiMapArg;
|
|
|
|
|
as?: Element;
|
2025-07-22 16:43:15 +02:00
|
|
|
};
|
|
|
|
|
|
2025-08-04 19:16:12 +02:00
|
|
|
export const ModernEmojiHTML = <Element extends ElementType>({
|
2025-07-22 16:43:15 +02:00
|
|
|
extraEmojis,
|
2025-07-31 19:30:14 +02:00
|
|
|
htmlString,
|
|
|
|
|
as: asElement, // Rename for syntax highlighting
|
2025-07-22 16:43:15 +02:00
|
|
|
...props
|
2025-07-31 19:30:14 +02:00
|
|
|
}: EmojiHTMLProps<Element>) => {
|
|
|
|
|
const Wrapper = asElement ?? 'div';
|
|
|
|
|
const emojifiedHtml = useEmojify(htmlString, extraEmojis);
|
2025-07-22 16:43:15 +02:00
|
|
|
|
2025-07-31 19:30:14 +02:00
|
|
|
if (emojifiedHtml === null) {
|
2025-07-22 16:43:15 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 19:30:14 +02:00
|
|
|
return (
|
|
|
|
|
<Wrapper {...props} dangerouslySetInnerHTML={{ __html: emojifiedHtml }} />
|
|
|
|
|
);
|
2025-07-22 16:43:15 +02:00
|
|
|
};
|
2025-08-04 19:16:12 +02:00
|
|
|
|
|
|
|
|
export const EmojiHTML = <Element extends ElementType>(
|
|
|
|
|
props: EmojiHTMLProps<Element>,
|
|
|
|
|
) => {
|
|
|
|
|
if (isModernEmojiEnabled()) {
|
|
|
|
|
return <ModernEmojiHTML {...props} />;
|
|
|
|
|
}
|
|
|
|
|
const Wrapper = props.as ?? 'div';
|
|
|
|
|
return (
|
|
|
|
|
<Wrapper
|
|
|
|
|
{...props}
|
|
|
|
|
dangerouslySetInnerHTML={{ __html: props.htmlString }}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|