2025-09-30 15:06:02 +02:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
|
|
|
|
|
import type { CustomEmojiMapArg } from '@/mastodon/features/emoji/types';
|
2025-10-08 13:11:25 +02:00
|
|
|
import type {
|
|
|
|
|
OnAttributeHandler,
|
|
|
|
|
OnElementHandler,
|
|
|
|
|
} from '@/mastodon/utils/html';
|
2025-09-30 15:06:02 +02:00
|
|
|
import { htmlStringToComponents } from '@/mastodon/utils/html';
|
2025-10-06 11:31:10 +02:00
|
|
|
import { polymorphicForwardRef } from '@/types/polymorphic';
|
2025-09-30 15:06:02 +02:00
|
|
|
|
|
|
|
|
import { AnimateEmojiProvider, CustomEmojiProvider } from './context';
|
|
|
|
|
import { textToEmojis } from './index';
|
|
|
|
|
|
2025-10-14 11:36:25 +02:00
|
|
|
export interface EmojiHTMLProps {
|
2025-09-30 15:06:02 +02:00
|
|
|
htmlString: string;
|
|
|
|
|
extraEmojis?: CustomEmojiMapArg;
|
|
|
|
|
className?: string;
|
2025-10-06 11:31:10 +02:00
|
|
|
onElement?: OnElementHandler;
|
2025-10-08 13:11:25 +02:00
|
|
|
onAttribute?: OnAttributeHandler;
|
2025-10-06 11:31:10 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-28 12:33:27 +01:00
|
|
|
export const EmojiHTML = polymorphicForwardRef<'div', EmojiHTMLProps>(
|
2026-02-25 17:59:18 +01:00
|
|
|
({ extraEmojis, htmlString, onElement, onAttribute, ...props }, ref) => {
|
2025-10-06 11:31:10 +02:00
|
|
|
const contents = useMemo(
|
|
|
|
|
() =>
|
2025-10-08 13:11:25 +02:00
|
|
|
htmlStringToComponents(htmlString, {
|
|
|
|
|
onText: textToEmojis,
|
|
|
|
|
onElement,
|
|
|
|
|
onAttribute,
|
|
|
|
|
}),
|
|
|
|
|
[htmlString, onAttribute, onElement],
|
2025-10-06 11:31:10 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<CustomEmojiProvider emojis={extraEmojis}>
|
2026-02-25 17:59:18 +01:00
|
|
|
<AnimateEmojiProvider {...props} ref={ref}>
|
2025-10-06 11:31:10 +02:00
|
|
|
{contents}
|
|
|
|
|
</AnimateEmojiProvider>
|
|
|
|
|
</CustomEmojiProvider>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-10-28 12:33:27 +01:00
|
|
|
EmojiHTML.displayName = 'EmojiHTML';
|