swiper icon indicating copy to clipboard operation
swiper copied to clipboard

[Thumbs] Enter or space doesn't activate slide

Open TimonVS opened this issue 4 years ago • 7 comments

  • Swiper Version: 6.5.0
  • Platform/Target and Browser Versions: macOS, any browser (latest versions)

What You Did

Pressing enter or space when a thumbnail slide is focused doesn't activate the slide like it should, even when the slides themselves are button elements. A similar issue was posted almost a year ago on Stack Overflow: https://stackoverflow.com/questions/61110557/swiper-js-enter-button-doesnt-do-anything-on-the-thumbnail-for-accessibility.

Code example: https://codesandbox.io/s/swiper-thumbs-gallery-forked-229z5?file=/index.html

Expected Behavior

Pressing enter or space when a thumbnail slide is focused should activate the slide.

Actual Behavior

Nothing happens when enter or space is pressed when a thumbnail slide is focused.

TimonVS avatar Mar 12 '21 11:03 TimonVS

As a workaround you can just assign keydown event listener to thumbs slides, watch for Enter key down and trigger click on thumb

nolimits4web avatar Mar 12 '21 11:03 nolimits4web

@nolimits4web Thanks for the quick reply. ~~I've tried adding a keydown listener to the thumbs slider, but it doesn't trigger. I've updated the sandbox: https://codesandbox.io/s/swiper-thumbs-gallery-forked-229z5?file=/index.html. Am I missing something?~~

Ah this works:

const s = document.querySelector('.swiper-container').swiper;

s.$el.on('keydown', () => console.log('key down'))

TimonVS avatar Mar 12 '21 12:03 TimonVS

I have a working workaround here: https://codesandbox.io/s/swiper-thumbs-gallery-forked-2d1up?file=/index.html.

Basically, add data-slide-index to all thumbs and add a keydown listener:

galleryThumbs.$el.on("keydown", (e) => {
  if (e.keyCode !== 13 && e.keyCode !== 32) return;

  var slideIndex = e.target.dataset.slideIndex;

  if (!slideIndex) return;

  galleryThumbs.slideTo(slideIndex);
  galleryTop.slideTo(slideIndex);
});

TimonVS avatar Mar 12 '21 13:03 TimonVS

Hi,

A simpler way is to change the div to a button. No js needed.

eberhapa avatar Sep 08 '22 07:09 eberhapa

A simpler way is to change the div to a button. No js needed.

Do you have an example? Still unable to fix in React without mentioned

<Swiper {...swiperOptions}>
    {items.map((item) => (
        <SwiperSlide
            tag={'button'}     <---- change from div to button
            key={item.id}
        >
            <Component item={item} />
        </SwiperSlide>
    ))}
</Swiper>

ScorpAL avatar Sep 22 '22 14:09 ScorpAL

A simpler way is to change the div to a button. No js needed.

Do you have an example? Still unable to fix in React without mentioned

<Swiper {...swiperOptions}>
    {items.map((item) => (
        <SwiperSlide
            tag={'button'}     <---- change from div to button
            key={item.id}
        >
            <Component item={item} />
        </SwiperSlide>
    ))}
</Swiper>

Everything I did is: <button class="swiper-button-prev"></button> <button class="swiper-button-next"></button>

Maybe you also have to add the 'A11y' module but I don't know.

eberhapa avatar Sep 23 '22 06:09 eberhapa

Resolved with this one... but I am still not happy with that as Mouse click works by default without this changes.

const HandleSlideKeyDown = (e: KeyboardEvent<HTMLElement>, index: number) => {
    if (e.key !== 'Enter' && e.key !== ' ') return;
    mySwiper?.slideTo(index);
};

<Swiper {...swiperOptions}>
    {items.map((item, index) => (
        <SwiperSlide
            tag={'button'}
            key={item.id}
            onKeyDown={(e) => HandleSlideKeyDown(e, index)}
        >
            <Component item={item} />
        </SwiperSlide>
    ))}
</Swiper>

ScorpAL avatar Nov 28 '22 13:11 ScorpAL