Search code examples
javascripthtmlcssgoogle-chrome-extensionevent-bubbling

How to make click only on modal exterior and not on dialog/content?


So I'm making an extension for Google Chrome with my friend, and for most of the features (i.e. calendar, settings, etc.) we open a modal so we don't have to redirect to another page. We are trying to get the modal to close when you click outside of the content. Here's a little markup screenshot to give you an idea of what I'm talking about.

enter image description here I want to be able to close the modal by clicking outside the white area. Currently it closes even when I click inside the white area. I have tried pretty much everything, including stopPropagation(), pointer-events:none;, and jquery disabling. None of them work for reasons unknown to me. Something weird about Chrome Extensions, I guess. Here's some of my code so far:

    function calendar() {
      document.getElementById("calendmodal").style.display = "block";
    }

document.addEventListener("DOMContentLoaded", function () {
  document.getElementById("calendicon").addEventListener("click", calendar);
  document.getElementById("calendmodal").addEventListener("click", closeModal);
  document.getElementById("calendmodal").addEventListener("click", function(event) {
    event.stopPropagation();
  });  
});

And HTML:

<div class="icons" id="icons_calendar">
        <img src='https://preview.ibb.co/jE76Bm/calendar.png' alt="calendar" border="0" id="calendicon"/>
      </div>
    <div id=calendmodal class="generalmodal">
      <div class="modal-content" id="calendcontent">
        <iframe src="https://calendar.google.com/calendar/embed?src=googleapps.wrdsb.ca_ptpdj55efmhg1nb89ruc15fi3k%40group.calendar.google.com&ctz=America%2FToronto" style="border: 0" width="800" height="600" frameborder="0" scrolling="no" visibility="hidden" id="calendiframe"></iframe>
        <p id=infostuff>For a more extensive calendar and<br>more school tools, visit <a href="http://jam.wrdsb.ca">The SJAM Website</a>.</p>
      </div>
    </div>

Not really sure what I'm doing wrong, I don't know how to do this in an extension.


Solution

  • I don't know exactly how you show your modal dialog, but my approach for this issue is like this (you may be able to adapt it ot your particular case):

    I have a transparent <div> as container that takes up the whole screen, and inside it, using CSS, I position the modal dialog. Something like:

    <div id="container" style="position:fixed;top:0;bottom:0;left:0;right:0;display:none">
        <div id="myModal" style="position:absolute;width:300px;height:200px;left:100px;top:100px">
            <!-- Here goes my content -->
        </div>
    </div>
    

    With this approach, to display the dialog I do something like:

    function openModal(){
      document.getElementById("container").style.display = "block";
    }
    

    And to close it when clicked on the transparent background but not on the dialog I have:

    document.getElementById("container").onclick = function(event) {
      if (event.target === this) {
        this.style.display = "none";
      }
    };