Skip to content

A platform for community discussion. Free, open, simple.

FIX: intercept escape in lightbox and prevent default (#22923)

David Battersbydb7581ce4987
diff --git a/app/assets/javascripts/discourse/app/components/d-lightbox.hbs b/app/assets/javascripts/discourse/app/components/d-lightbox.hbs
index fc7d3ca867d..ee80d481c1e 100644
--- a/app/assets/javascripts/discourse/app/components/d-lightbox.hbs
+++ b/app/assets/javascripts/discourse/app/components/d-lightbox.hbs
@@ -15,6 +15,7 @@
class="d-lightbox__content"
style={{this.CSSVars}}
aria-labelledby={{this.titleElementId}}
+ {{on "keydown" this.onKeydown passive=false capture=true}}
{{on "keyup" this.onKeyup passive=true capture=true}}
>
<div class="d-lightbox__focus-trap" tabindex="0"></div>
diff --git a/app/assets/javascripts/discourse/app/components/d-lightbox.js b/app/assets/javascripts/discourse/app/components/d-lightbox.js
index a2cb4d8a262..fa6bc0cae23 100644
--- a/app/assets/javascripts/discourse/app/components/d-lightbox.js
+++ b/app/assets/javascripts/discourse/app/components/d-lightbox.js
@@ -357,6 +357,14 @@ export default class DLightbox extends Component {
this.hasExpandedTitle = !this.hasExpandedTitle;
}
+ @bind
+ onKeydown(event) {
+ if (event.key === KEYBOARD_SHORTCUTS.CLOSE) {
+ event.preventDefault();
+ return this.close();
+ }
+ }
+
@bind
onKeyup({ key }) {
if (KEYBOARD_SHORTCUTS.PREVIOUS.includes(key)) {
@@ -367,10 +375,6 @@ export default class DLightbox extends Component {
return this.showNextItem();
}
- if (key === KEYBOARD_SHORTCUTS.CLOSE) {
- return this.close();
- }
-
if (key === KEYBOARD_SHORTCUTS.ZOOM) {
return this.toggleZoom();
}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/d-lightbox-test.js b/app/assets/javascripts/discourse/tests/acceptance/d-lightbox-test.js
index a5061fbfc88..edfbe94660e 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/d-lightbox-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/d-lightbox-test.js
@@ -626,7 +626,7 @@ acceptance("Experimental Lightbox - interaction", function (needs) {
requestFullscreenStub.restore();
exitFullscreenStub.restore();
- await triggerKeyEvent(SELECTORS.LIGHTBOX_CONTENT, "keyup", "Escape");
+ await triggerKeyEvent(SELECTORS.LIGHTBOX_CONTENT, "keydown", "Escape");
assert.dom(SELECTORS.LIGHTBOX_CONTENT).doesNotExist();
});