jsor / lity

Lightweight, accessible and responsive lightbox.
https://sorgalla.com/lity/
MIT License
1.16k stars 196 forks source link

Stop event propagation when I close a light box #272

Open denbyangus opened 3 years ago

denbyangus commented 3 years ago

I have implemented a window close when the user clicks the ESC key. Unfortunately the event propagates from the Lity close event (also triggered by an ESC action) and closes the window too.

I tried the following but I fear I'm too late.

$(document).on('lity:close', function(event, instance) {
  console.log('Lity lightbox closed');
  event.stopPropagation();
});

Is there a handle closer to the close that I can use to insert the stopPropagation?

denbyangus commented 3 years ago

All good. I came up with a solution using a flag. :) vat lityon = false;

The flag is set when the lity object instantiates $(document).on('lity:ready' ... lityon = true;

and unset when the object is removed: $(document).on('lity:remove',... lityon = false;

I check the flag when I handle the keyup event to ensure I don't close the window after closing the lightbox.