ddon
ddon

Reputation: 205

(Vue.js) modal close event

When I click on the outer area of ​​the modal, I want the same event as the close button of the modal. (Event that closes modal when clicking outside area of ​​modal)

The current progress is that the modal is closed when the close modal button is clicked.

Carousel.vue

<template>
  <div>
    <div v-for="(item, index) in photos" :key="index">
      <div @click="imgClick(item)" style="cursor:pointer;">
        <img :src="item.thumbnail" />
      </div>
      <Modal v-if='item.show' @close="item.show = false">
        <div slot='body'>
          <img :src="item.thumbnail" :class="`img-index--${index}`"/>
        </div>        
      </Modal>
    </div>
  </div>
</template>
<script>
import Modal from './Modal.vue'
export default {
  props: {
    items: { type: Array, default: () => [] }
  },
  data() {
    return {
      photos: {}
    }
  },
  created() {
    this.photos = this.items.map(item => {
      return { ...item, show: false }
    })
  },
  methods: {
    imgClick(item) {
      item.show = true
    }
  },
  components: {
    Modal: Modal
  }
}
</script>

Modal.vue

<template>
  <transition name="modal">
    <div class="modal-mask" @click="$emit('close')">
      <div class="modal-wrapper">
        <div class="app__phone">
          <div class="feed">
            <div class="post">
              <div class="header headroom">
                <div class="level-left">
                  <img src="../assets/imgs/user.gif" class="modal-header-img"/>
                  <div class="user">
                    <span class="username">username</span>
                    <button class="modal-default-button" @click="$emit('close')">Close</button>
                  </div>
                </div>
              </div>
              <slot name="modal-img"></slot>
              <div class="content">
                <div class="content-title">
                  <slot name="modal-tit"></slot>
                </div>
              </div>
            </div>
          </div> 
        </div>
      </div>
    </div>
  </transition>
</template>

When I add a click event to the bottom <div>, it closes when I click outside the modal, but it closes when I click anywhere in the modal.

<div class="modal-mask" @click="$emit('close')">

And this link has a Fiddle example in the accepted answer to the question.

https://stackoverflow.com/a/58023701/12066654

Upvotes: 3

Views: 25900

Answers (2)

Syed Shah Riage
Syed Shah Riage

Reputation: 51

Try to create a div with opacity that covers all the screen but with a z-index < your modals z-index. Then @click on it, you emit your event to close the modal :)

 <template>
        <div v-show="showModal" :class="{ 'modal fade show' : showModal}" tabindex="-1" ref="backdrop">
            <div class="modal">
                <h1> Modal Title </h1>
                <input type="text" /> 
                <p> Modal Content </p>
            </div>
            <div class="backdrop" @click="showModal = false"></div>
        </div>
    </template>
    <style>
    .modal.backdrop {
       position: fixed;
       width: 100%;
       height: 100%;
       z-index: 1;
       background-color: rgba(0, 0, 0, 0.5);
    }
    .modal-dialog {
       margin: 0px auto;
       padding: 25px 20px;
       background-color: #fff;
       transition: all 0.3s ease;
       border-radius: 15px;
       width: 375px;
       z-index: 2;
    }
    </style>
    export default {
        data: function () {
            return {
              showModal: false
            }
        }
        methods: {
            closeModal: function() {
                this.showModal = true;
            }
        }
    }
    </script>

Upvotes: 0

David Weldon
David Weldon

Reputation: 64342

You need to add a handler to the outer modal div like so:

<template id="modal">
  <div class="modal" @click.self="close">
    <div class="close" @click="close">&times;</div>
    <div class="body">
      <slot name="body" />
    </div>
  </div>
</template>

Upvotes: 6

Related Questions