Skip to content

Commit f90e87b

Browse files
authored
Merge pull request #664 from lokesh/prevent-esc-bubbling
feature: Prevent Esc keypress from bubbling
2 parents a603e35 + 4c07143 commit f90e87b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/css/lightbox.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ body.lb-disable-scrolling {
2121
text-align: center;
2222
line-height: 0;
2323
font-weight: normal;
24+
outline: none;
2425
}
2526

2627
.lightbox .lb-image {

src/js/lightbox.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,19 @@
9999
}
100100

101101
var self = this;
102-
$('<div id="lightboxOverlay" class="lightboxOverlay"></div><div id="lightbox" class="lightbox"><div class="lb-outerContainer"><div class="lb-container"><img class="lb-image" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt=""/><div class="lb-nav"><a class="lb-prev" aria-label="Previous image" href="" ></a><a class="lb-next" aria-label="Next image" href="" ></a></div><div class="lb-loader"><a class="lb-cancel"></a></div></div></div><div class="lb-dataContainer"><div class="lb-data"><div class="lb-details"><span class="lb-caption"></span><span class="lb-number"></span></div><div class="lb-closeContainer"><a class="lb-close"></a></div></div></div></div>').appendTo($('body'));
102+
103+
// The two root notes generated, #lightboxOverlay and #lightbox are given
104+
// tabindex attrs so they are focusable. We attach our keyboard event
105+
// listeners to these two elements, and not the document. Clicking anywhere
106+
// while Lightbox is opened will keep the focus on or inside one of these
107+
// two elements.
108+
//
109+
// We do this so we can prevent propogation of the Esc keypress when
110+
// Lightbox is open. This prevents it from intefering with other components
111+
// on the page below.
112+
//
113+
// Github issue: https://github.com/lokesh/lightbox2/issues/663
114+
$('<div id="lightboxOverlay" tabindex="-1" class="lightboxOverlay"></div><div id="lightbox" tabindex="-1" class="lightbox"><div class="lb-outerContainer"><div class="lb-container"><img class="lb-image" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt=""/><div class="lb-nav"><a class="lb-prev" aria-label="Previous image" href="" ></a><a class="lb-next" aria-label="Next image" href="" ></a></div><div class="lb-loader"><a class="lb-cancel"></a></div></div></div><div class="lb-dataContainer"><div class="lb-data"><div class="lb-details"><span class="lb-caption"></span><span class="lb-number"></span></div><div class="lb-closeContainer"><a class="lb-close"></a></div></div></div></div>').appendTo($('body'));
103115

104116
// Cache jQuery objects
105117
this.$lightbox = $('#lightbox');
@@ -387,6 +399,10 @@
387399
self.$lightbox.find('.lb-dataContainer').width(newWidth);
388400
self.$lightbox.find('.lb-prevLink').height(newHeight);
389401
self.$lightbox.find('.lb-nextLink').height(newHeight);
402+
403+
// Set focus on one of the two root nodes so keyboard events are captured.
404+
self.$overlay.focus();
405+
390406
self.showImage();
391407
}
392408

@@ -493,11 +509,13 @@
493509
};
494510

495511
Lightbox.prototype.enableKeyboardNav = function() {
496-
$(document).on('keyup.keyboard', $.proxy(this.keyboardAction, this));
512+
this.$lightbox.on('keyup.keyboard', $.proxy(this.keyboardAction, this));
513+
this.$overlay.on('keyup.keyboard', $.proxy(this.keyboardAction, this));
497514
};
498515

499516
Lightbox.prototype.disableKeyboardNav = function() {
500-
$(document).off('.keyboard');
517+
this.$lightbox.off('.keyboard');
518+
this.$overlay.off('.keyboard');
501519
};
502520

503521
Lightbox.prototype.keyboardAction = function(event) {
@@ -507,6 +525,8 @@
507525

508526
var keycode = event.keyCode;
509527
if (keycode === KEYCODE_ESC) {
528+
// Prevent bubbling so as to not affect other components on the page.
529+
event.stopPropagation();
510530
this.end();
511531
} else if (keycode === KEYCODE_LEFTARROW) {
512532
if (this.currentImageIndex !== 0) {

0 commit comments

Comments
 (0)