ghosh / Micromodal

⭕ Tiny javascript library for creating accessible modal dialogs
https://micromodal.now.sh/
MIT License
3.54k stars 230 forks source link

Close button doesn't work when contains any elements #392

Closed DimaGashko closed 3 years ago

DimaGashko commented 4 years ago

I've added an svg icon to the close button and realized that the button doesn't work anymore.

Reason: you use event delegation slightly incorrect (https://github.com/ghosh/Micromodal/blob/master/lib/src/index.js#L131).

if (event.target.hasAttribute(this.config.closeTrigger)) {
    this.closeModal(event)
}

Solution: replace hasAttribute with closest:

if (event.target.closest(`[${this.config.closeTrigger}]`)) ...

Html:

 <button data-micromodal-close>
        <svg><use xlink:href="#close.svg"></use></svg>
 </button>

p.s. For now I'm just going to use something like:

.modal__close svg {
    pointer-events: none;
}

It's working, but I hope you would fix it.

Details: https://javascript.info/event-delegation

deniseismo commented 3 years ago

I can confirm this: had the same issue where the close button with an svg icon inside wouldn't close properly (occasionally it did work), thought I was tripping. Thanks for the workaround.

HongPong commented 3 years ago

for eslint i put

if (event.target.closest(`[${ this.config.closeTrigger }]`)) {

seems to work as it should. thank you!

ghosh commented 3 years ago

Fixed in #443. Thanks @HongPong for the PR