Skip to content

Commit

Permalink
fix: a11y warning
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliaghisini committed Feb 25, 2025
1 parent ecec497 commit 688bea5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/components/Blocks/Listing/Carousel/CarouselTemplate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { injectLazyLibs } from '@plone/volto/helpers/Loadable/Loadable';
import config from '@plone/volto/registry';

import './carouselTemplate.scss';
import { on } from 'process';
const messages = defineMessages({
carouselItemAriaLabel: {
id: 'carousel-item-aria-label',
Expand All @@ -48,7 +49,7 @@ const messages = defineMessages({

const Slide = (props) => {
const intl = useIntl();
const { index, appearance, appearanceProp, onKeyDown } = props;
const { index, appearance, appearanceProp, onKeyDown, ...otherProps } = props;

const appearances = config.blocks.blocksConfig.listing.variations.filter(
(v) => v.id === 'carousel',
Expand All @@ -57,14 +58,15 @@ const Slide = (props) => {

return (
<SingleSlideWrapper
{...otherProps}
index={index}
onKeyDown={onKeyDown}
aria-label={
(props['aria-label'] ? props['aria-label'] + ' - ' : '') +
intl.formatMessage(messages.carouselItemAriaLabel)
}
>
<div className={'slide-wrapper'} role="presentation">
<div className="slide-wrapper" role="presentation">
<SlideItemAppearance {...props} {...appearanceProp} intl={intl} />
</div>
</SingleSlideWrapper>
Expand Down Expand Up @@ -106,6 +108,7 @@ const CarouselTemplate = (props) => {
SliderNextArrow,
SliderPrevArrow,
handleSlideKeydown,
HiddenSlideFocus,
} = useSlider(userAutoplay, setUserAutoplay, block_id);

const toggleAutoplay = () => {
Expand Down Expand Up @@ -185,6 +188,9 @@ const CarouselTemplate = (props) => {
appendDots: renderCustomDots,
// Custom handling of focus for a11y
afterChange: focusSlide,
onInit: () => {
HiddenSlideFocus();
},
responsive: [
{
breakpoint: 980,
Expand Down
2 changes: 0 additions & 2 deletions src/components/Slider/SingleSlideWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const SingleSlideWrapper = (props) => {
//e.preventDefault();
//e.stopPropagation();
}}
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
tabIndex={0}
>
{children}
</div>
Expand Down
27 changes: 26 additions & 1 deletion src/components/Slider/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ export const useSlider = (userAutoplay, setUserAutoplay, block_id) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const HiddenSlideFocus = () => {
const slides = document.querySelectorAll(
`${sliderElementSelector} .slick-slide`,
);

//set tab index to -1 for hidden slides

slides?.forEach((slide) => {
const focusableElements = slide.querySelectorAll(
'button:not([disabled]), [href], [tabindex="0"]',
);

focusableElements.forEach((element) => {
const tabIndexValue =
element.closest('.slick-active') !== null ? '0' : '-1';

element.setAttribute('tabIndex', tabIndexValue);
});
});
};

const focusSlide = (slideIndex) => {
if (!sliderElement) return;
const sliderIsVisible = sliderContainer?.classList?.contains('visible');
Expand All @@ -52,8 +73,10 @@ export const useSlider = (userAutoplay, setUserAutoplay, block_id) => {
).some((node) => node.contains(document.activeElement));

if (focusedSliderElement) {
slide.setAttribute('tabindex', '0');
slide.focus();
}
HiddenSlideFocus();
};

const visibleSlide = (selector) => {
Expand Down Expand Up @@ -160,7 +183,8 @@ export const useSlider = (userAutoplay, setUserAutoplay, block_id) => {

if (shiftKey) {
if (prevIndex != null) {
slider.current.slickGoTo(prevIndex);
slider.current.slickGoTo(prevIndex) ||
focusableSlideElements[focusableSlideElements.length - 1].focus();
} else {
document.getElementById('sliderPrevArrow_' + block_id).focus();
}
Expand Down Expand Up @@ -197,5 +221,6 @@ export const useSlider = (userAutoplay, setUserAutoplay, block_id) => {
SliderNextArrow,
SliderPrevArrow,
handleSlideKeydown,
HiddenSlideFocus,
};
};
1 change: 1 addition & 0 deletions src/components/View/commons/Gallery/Gallery.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const Gallery = ({
nextArrow: <NextArrow />,
prevArrow: <PrevArrow />,
speed: 500,
accessibility: true,
...getResponsiveSettings(nItems, slidesToShow, slidesToScroll),
responsive: [
{
Expand Down

0 comments on commit 688bea5

Please sign in to comment.