basicLightbox icon indicating copy to clipboard operation
basicLightbox copied to clipboard

Managing focus

Open acnorrisuk opened this issue 6 years ago • 9 comments
trafficstars

Looking at the demos it looks like there is some focus management missing on the modals. Here's what needs to be added to fix it:

  • When a modal is open, focus should be sent to the modal (either on the element itself via .focus() and tabindex="-1" or sent to the first interactive element.

  • When a modal is open, focus should be trapped inside the modal (so you can't access elements behind it when tabbing with the keyboard).

  • When a modal is open, pressing the escape key should close the modal.

  • It would be nice if the modals had an explicit close button with an accessible name (e.g. <button aria-label="close">X</button>.

  • When a modal is closed, focus should be send back to the element which opened it.

An example of an accessible modal: http://edenspiekermann.github.io/a11y-dialog/example/

The WAI-ARIA authoring practices guide on modals: https://www.w3.org/TR/wai-aria-practices/examples/dialog-modal/dialog.html

acnorrisuk avatar Aug 14 '19 14:08 acnorrisuk

Thanks for the suggestions! :) I will see what I can do while keeping the module simple.

electerious avatar Aug 23 '19 21:08 electerious

Another vote for adding support for pressing the ESC key to close, that's kind of a convention with many lightbox/popups

adam-jones-net avatar Aug 04 '20 14:08 adam-jones-net

@arkid This should do the job for now:

document.body.addEventListener('keypress', (e) => {
  if (e.key === "Escape") instance.close()
})

electerious avatar Aug 05 '20 06:08 electerious

@arkid This should do the job for now:

document.body.addEventListener('keypress', (e) => {
  if (e.key === "Escape") instance.close()
})

Thanks for this yes I was going to implement that at some point too. Although not necessary, for the record, anyone wanting to do via jQuery could do:

$(document).keyup(function(e) {
    if (e.keyCode == 27 && instance) {
        instance.close();
    }
})

adam-jones-net avatar Aug 05 '20 13:08 adam-jones-net

I think there may also be a bug on focusing on input elements inside the lightbox: if in the onShow callback I add something like instance.element().querySelector("input#myinput").focus(); it doesn't focus on the element.

yliharma avatar Sep 09 '21 15:09 yliharma

@yliharma I'm struggling to force focus on an element as well. Using very similar code. Did you get this figured out?

JiveDig avatar Sep 28 '22 18:09 JiveDig

I got it to work via:

instance.show(() => {
	instance.element().querySelector( '.basicLightbox__placeholder > *:first-child' ).focus();
});

For whatever reason, I couldn't get it to work with the onShow callback.

JiveDig avatar Sep 28 '22 18:09 JiveDig

I got it to work via:

instance.show(() => {
	instance.element().querySelector( '.basicLightbox__placeholder > *:first-child' ).focus();
});

For whatever reason, I couldn't get it to work with the onShow callback.

It works!! We'll never know why, but thank you :)

yliharma avatar Jan 03 '23 13:01 yliharma