Files
mastodon-sakyey/app/javascript/mastodon/components/emoji/html.tsx

44 lines
1.2 KiB
TypeScript
Raw Normal View History

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>(
({ 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}>
<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';