Skip to content

fix: import emoji-mart dynamically like (#950) #1491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,32 @@ import {
} from "../../schema/index.js";
import { DefaultGridSuggestionItem } from "./DefaultGridSuggestionItem.js";

let data:
| Promise<{
default: EmojiMartData;
}>
| undefined;

// Temporary fix for https://github.com/missive/emoji-mart/pull/929
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi, the reason data was global and could be a promise would be to prevent duplicate loads when loadEmojiMart is called twice in a row.

Didn't help readability and it might be a premature optimization so I'm ok with the new version as long as we don't see this causing any issues

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I can de-dupe easily. I'll pull that in.

let emojiMart: typeof import("emoji-mart") | undefined;
let emojiData: EmojiMartData | undefined;

async function loadEmojiMart() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to make both of these the same, and also made them Node.js safe

if (!emojiMart || !emojiData) {
// load dynamically because emoji-mart doesn't specify type: module and breaks in nodejs
const [emojiMartModule, emojiDataModule] = await Promise.all([
import("emoji-mart"),
// use a dynamic import to encourage bundle-splitting
// and a smaller initial client bundle size
import("@emoji-mart/data"),
]);

emojiMart =
"default" in emojiMartModule ? emojiMartModule.default : emojiMartModule;
emojiData =
"default" in emojiDataModule
? (emojiDataModule.default as EmojiMartData)
: (emojiDataModule as EmojiMartData);

emojiMart.init({ data: emojiData });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs an await

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, wasn't clear that it was async in the docs https://github.com/missive/emoji-mart?tab=readme-ov-file#-emoji-component , but won't hurt to await

}

return { emojiMart, emojiData };
}

export async function getDefaultEmojiPickerItems<
BSchema extends BlockSchema,
Expand All @@ -29,23 +48,11 @@ export async function getDefaultEmojiPickerItems<
return [];
}

if (!data) {
// use a dynamic import to encourage bundle-splitting
// and a smaller initial client bundle size

data = import("@emoji-mart/data") as any;

// load dynamically because emoji-mart doesn't specify type: module and breaks in nodejs
emojiMart = await import("emoji-mart");
const emojiMartData = (await data)!.default;
await emojiMart.init({ data: emojiMartData });
}

const emojiMartData = (await data)!.default;
const { emojiData } = await loadEmojiMart();

const emojisToShow =
query.trim() === ""
? Object.values(emojiMartData.emojis)
? Object.values(emojiData.emojis)
: ((await emojiMart!.SearchIndex.search(query)) as Emoji[]);

return emojisToShow.map((emoji) => ({
Expand Down
35 changes: 33 additions & 2 deletions packages/react/src/components/Comments/EmojiMartPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
// From https://github.com/missive/emoji-mart/blob/main/packages/emoji-mart-react/react.tsx
import type { EmojiMartData } from "@emoji-mart/data";
import React, { useEffect, useRef } from "react";
import { Picker } from "emoji-mart";

// Temporary fix for https://github.com/missive/emoji-mart/pull/929
let emojiMart: typeof import("emoji-mart") | undefined;
let emojiData: EmojiMartData | undefined;

async function loadEmojiMart() {
if (!emojiMart || !emojiData) {
// load dynamically because emoji-mart doesn't specify type: module and breaks in nodejs
const [emojiMartModule, emojiDataModule] = await Promise.all([
import("emoji-mart"),
// use a dynamic import to encourage bundle-splitting
// and a smaller initial client bundle size
import("@emoji-mart/data"),
]);

emojiMart =
"default" in emojiMartModule ? emojiMartModule.default : emojiMartModule;
emojiData =
"default" in emojiDataModule
? (emojiDataModule.default as EmojiMartData)
: (emojiDataModule as EmojiMartData);

emojiMart.init({ data: emojiData });
}

return { emojiMart, emojiData };
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function EmojiPicker(props: any) {
Expand All @@ -12,7 +39,11 @@ export default function EmojiPicker(props: any) {
}

useEffect(() => {
instance.current = new Picker({ ...props, ref });
(async () => {
const { emojiMart } = await loadEmojiMart();

instance.current = new emojiMart.Picker({ ...props, ref });
})();

return () => {
instance.current = null;
Expand Down
Loading