Skip to content
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

Svelte Integration #904

Open
halafi opened this issue Mar 26, 2024 · 0 comments
Open

Svelte Integration #904

halafi opened this issue Mar 26, 2024 · 0 comments

Comments

@halafi
Copy link

halafi commented Mar 26, 2024

in case anyone would find this useful, as I do not have time for putting it into PR, but I seemed to have resolved most issues I had when using this inside a svelte component (except category click is sometimes not working, but most of time it does)

it shouldn't be too much work to put this into emoji-mart-svelte package

EmojiMart.svelte:

<script>
  import { Picker } from "emoji-mart";
  import { onMount, createEventDispatcher, onDestroy } from "svelte";

  let emojiPickerEl;
  let picker;
  let mounted = false;

  const dispatch = createEventDispatcher();

  onMount(() => {
    picker = new Picker({
      theme: "light",
      autoFocus: true,
      onClickOutside: () => {
        if (mounted) dispatch("close");
      },
      onEmojiSelect: (emoji) => {
        dispatch("select", emoji);
      },
    });
    emojiPickerEl.appendChild(picker);
    setTimeout(() => {
      // ensure click outside works
      mounted = true;
    }, 1);
  });

  onDestroy(() => {
    // the clickoutside handler is not unregistered properly, so this is probably redundant
    picker = null;
    emojiPickerEl = null;
  });
</script>

<div on:click|preventDefault|stopPropagation bind:this={emojiPickerEl} />

usage:

    <EmojiMart
      on:select={(ev) => handleEmojiSelect(ev.detail)}
      on:close={() => emojiPickerVisible = false}
    />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant