Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Lightbox being opened with improper keypress #2258

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 32 additions & 25 deletions src/js/coblocks-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
} );

close.addEventListener( 'click', function() {
removeKeyboardListener();
wrapper.style.display = 'none';
} );

Expand Down Expand Up @@ -156,31 +157,37 @@
counter.textContent = `${ ( index + 1 ) } / ${ images.length }`;
}

function setKeyboardListener( ) {
document.onkeydown = function( e ) {
const lightboxDisplayValue = wrapper;
const lightboxIsOpen = ( typeof lightboxDisplayValue !== 'undefined' && lightboxDisplayValue !== 'none' );
if ( lightboxIsOpen ) {
e = e || window.event;
switch ( e.keyCode ) {
case 27 : // Esc key
close.click();
break;
case 37 : // Arrow left or 'A' key.
arrowLeftContainer.click();
break;
case 65 : // 'A' key.
arrowLeftContainer.click();
break;
case 39 : // Arrow right.
arrowRightContainer.click();
break;
case 68 : // 'D' key.
arrowRightContainer.click();
break;
}
const setKeyboardListener = () => document.addEventListener( 'keydown', keyPressEventListener );
const removeKeyboardListener = () => document.removeEventListener( 'keydown', keyPressEventListener );

const keyPressEventListener = ( event ) => {
if ( ! event || ! event?.code ) {
return;
}

const lightboxDisplayValue = wrapper;
const lightboxIsOpen = ( typeof lightboxDisplayValue !== 'undefined' && lightboxDisplayValue?.style?.display === 'flex' );

if ( lightboxIsOpen ) {
const code = event.code;
switch ( code ) {
case 'Escape' : // Esc key
close.click();
break;
case 'ArrowLeft' : // Arrow left.
arrowLeftContainer.click();
break;
case 'KeyA' : // 'A' key.
arrowLeftContainer.click();
break;
case 'ArrowRight' : // Arrow right.
arrowRightContainer.click();
break;
case 'KeyD' : // 'D' key.
arrowRightContainer.click();
break;
}
};
}
}
};
}
}() );