Making PSWP accessibility friendly #2091
lnfel
started this conversation in
Show and tell
Replies: 1 comment 1 reply
-
If you're in a situation where you have to nest anchor elements into each other, you probably need to rethink the dom structure. Also, you don't have to use a hidden anchor element just to let PhotoSwipe know which data to use, you can use const lightbox = new PhotoSwipeLightbox({
gallery: '.project-grid',
children: 'li.project-card',
pswpModule: PhotoSwipe
});
lightbox.addFilter('domItemData', (itemData, element, linkEl) => {
if (element) {
itemData.src = element.dataset.imageUrl;
itemData.width = Number(element.dataset.imageWidth);
// itemData.height = etc...
}
return itemData;
});
lightbox.init(); <li data-image-url="" data-image-width="" ... |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Just encountered the same issue with #1717 today, since the container element is an acnhor tag it messes up the announcements by assistive technology. I played around a bit and found out that we can use other element as the
children
ofgallery
:Our pswp gallery is the element with a class of
project-grid
and the children areli
elements with a class ofproject-card
. By default PSWP tries to look inside of the children for the very first anchor element, this is why it is important we ad the hidden anchor link as the very first child of the registered pswp children element.Now if we click on the
li
element, it will trigger PSWP and reveal the image and animate it on screen. But one issue here is that PSWP will only listen for click events and not keyboard events on the registered element and only anchor element can do navigation when pressing Enter or Space key which PSWP listens to. This is where we add tabindex and attach our custom keydown listener onli
element as denoted by the markupon:keydown={triggerPSWP}
, below is the code to trigger a click event on the hidden anchor link:With this PSWP is now accessible friendly! 🎉
Beta Was this translation helpful? Give feedback.
All reactions