Close with esc key
JiveDig opened this issue · comments
Thanks for such a great API.
This feature is pretty easy to add, which I've been doing in onShow
like this:
onShow: (instance) => {
// Close when hitting escape.
document.onkeydown = function(evt) {
evt = evt || window.event;
var isEscape = false;
if ( "key" in evt ) {
isEscape = ( evt.key === "Escape" || evt.key === "Esc" );
} else {
isEscape = ( evt.keyCode === 27 );
}
if ( isEscape ) {
instance.close();
}
};
},
...but this seems to be the standard for modals, I wonder if it make sense in the official script?
You have to consider that basicLightbox just displays whatever you want in the middle of the screen. This might be something that already uses the ESC key. Or you use basicLightbox on a website with a keyboard navigation where ESC is already assigned to a different function. I don't like to make assumptions about keybindings and therefore don't handle them. Instead I like to provide an API that makes it easy to add the features you want in a way that fits into your use-case. ESC handling is easy to add and therefore not a part of the lightbox itself.
Makes sense to me. Thanks for the explanation 👍.