emoji-mart-vue icon indicating copy to clipboard operation
emoji-mart-vue copied to clipboard

Unable to close the emoji picker after clicking outside it

Open eakenbor opened this issue 5 years ago • 3 comments

I have tried using custom vue directives to close the modal upon clicking outside it but does not work as the event is triggered prematurely irrespective of the root element it is placed. Is there any inbuilt custom event for this? Or anyway around it?

eakenbor avatar Oct 05 '19 19:10 eakenbor

It would be good if there was a simple 'x' on the pop up to close the modal. Can this be easily done?

OneMoreRound82 avatar Dec 03 '19 11:12 OneMoreRound82

This can easily be done with placing a div that gets displayed when you toggle the picker.

<div 
    class="modal-container"
    v-show="showEmojiPicker"
    @click="toggleEmojiPicker"></div>
.modal-container {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1010;
}

And then adjust the z-index of the emoji-mart container. Which I think by default is 1 or 2.

.emoji-mart {
    z-index: 1020;
}

merfed avatar Aug 29 '21 02:08 merfed

@eakenbor I did the followings and it worked smoothly.

  • First of all, you can add click event listener to body tag on mounted event, which closes emoji picker.
  • Please make sure that you remove it on beforeDestroy event to reduce memory leak
  • In picker component, stop propagation of click event to prevent closing picker when you select any emoji

Here is the emoji picker component which is toggled by icon button

<template>
  <div class="emoji" @click.stop>
    <div>
      <font-awesome-icon
        class="check"
        icon="smile-beam"
        @click.stop="toggleWindow"
      />
    </div>
    <picker
      v-show="isOpen"
      @select="addEmoji"
    />
  </div>
</template>
<script>
import { Picker } from 'emoji-mart-vue'

export default {
  components: {
    Picker
  },
  data() {
    return {
      isOpen: false,
    }
  },
  methods: {
    toggleWindow() {
      this.isOpen = !this.isOpen
    },
    addEmoji(emoji) {
      // emit to parent or do something
    },
    hidePopup() {
      this.isOpen = false
    }
  },
  mounted() {
    document.body.addEventListener('click', this.hidePopup)
  },
  beforeDestroy() {
    document.body.removeEventListener('click', this.hidePopup)
  }
}
</script>

coolstackdev avatar Oct 08 '21 20:10 coolstackdev