Hi.
I want to close my modal when user click outside <div class="modal-dialog">
.
My code below works wonderfully except if user move his mouse during the click.
If at mousedown the mouse is in modal-dialog but at mouseup the mouse is outside (mouse has moved during the click), than the modal close
I would like to prevent this behavior and not close modal if mouse at mousedown is inside modal-dialog. Is it possible ?
I did some search and I have found that
if coords of
mousedown
andmouseup
are not equal, theclick
event won’t be fired
But it appears to be not true as the click event is fired in my situation. Is this information not correct for Stimulus ?
Thank you for your help !
<div data-controller="modal">
<button id="btn-user" data-action="modal#open" type="button">WonderfulButton</button>
<div class="modal" tabindex="-1" data-modal-target="modal" data-action="click->modal#close" data-close-modal>
<div class="modal-dialog">Content of my modal (form)</div>
</div>
</div>
import { Controller } from '@hotwired/stimulus';
/**
* @property {HTMLElement} modalTarget
*/
export default class extends Controller {
static targets = ['modal']
open() {
this.modalTarget.style.display = "flex";
}
close(event) {
if (event.target.hasAttribute("data-close-modal")) {
this.modalTarget.style.display = "none";
}
}
}