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

Keyboard events propagate to other components when lightGallery is open #1092

Open
unitedworx opened this issue Jul 15, 2021 · 3 comments
Open

Comments

@unitedworx
Copy link

Hi there

it seems that then i press the arrow buttons to navigate lightgallery when its open the keyboard evens will propagate to another gallery component which sits behind lightgallery and will move that gallery as well.

Is there a way to tell lightGallerry to stop propagation on those events?

I know i can disable the keyboard events on the other gallery component and manually bind the arrow keys fro it so i can check if lightgallery is open not to fire the events, however this is ugly!

If there is a way to tell lightGallery to stop propagating the keyboard events when its open it will be a clean and nice solution.

@unitedworx
Copy link
Author

I utilized events to disable keyboard navigation on the other slider when opening lightGallery and solved my problem. Clean enough solution!

keep up the good work

@stale stale bot added the v1 label Sep 13, 2021
Repository owner deleted a comment from stale bot Sep 14, 2021
@stale stale bot removed the v1 label Sep 14, 2021
@sachinchoolur
Copy link
Owner

Hey @unitedworx,

I'm sorry, somehow I missed this at all. Yes, I think we can prevent the propagation except for inline galleryies.
I'll make the changes. Thank you for reporting this issue

@neurot1k
Copy link

neurot1k commented Feb 2, 2022

The issue, at least in my case, is that none of the DOM elements within lightGallery have focus when it opens. Keyboard events originate from the element which has focus, so depending on what the user interacted last with on my site the event was bubbling up through the DOM from an unexpected place and another event handler was being called first.

In case anyone has the same issue my workaround for this is to focus one of the buttons in the toolbar when lightGallery opens and attach an event hander to the container to handle keyboard events myself.

this.lg = lightGallery(this.base, {
    keyPress: false,
    // other settings
});
this.base.addEventListener('lgAfterOpen', this.onOpen);

onOpen = () => {
    // Ensure a DOM element within lightgallery has focus so that keyboard events bubble through the container
    // element where our event listener is attached.
    const buttons = this.lg.$toolbar.firstElement.getElementsByTagName('button');
    if (buttons.length) {
        buttons[0].focus();
        this.lg.$container.firstElement.addEventListener('keydown', this.onKeyDown);
    }
};

onKeyDown = (e) => {
    e.stopPropagation();
    switch (e.keyCode) {
        case 27:  // Esc
            return this.lg.closeGallery();
        case 35:  // End
            return this.lg.slide(this.props.images.length - 1);
        case 36:  // Home
            return this.lg.slide(0);
        case 37:  // Left
            return this.lg.goToPrevSlide();
        case 39:  // Right
            return this.lg.goToNextSlide();
      }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants