Skip to content

Commit ca5d16c

Browse files
author
Hector Arce De Las Heras
committed
Enhancing Accessibility and Usability of Carousel Component
This commit focuses on improving the accessibility and usability of the Carousel component. Aria attributes have been added to enhance screen reader compatibility, test cases have been modified to reflect these changes, and code has been refactored for better readability and maintainability. Specific updates include the addition of a ScreenReaderOnly component, updates to the Carousel story, and the inclusion of screenReaderText in the ICarouselStandAlone interface. Additionally, the MediaProgressBar component and its tests have been updated for better screen reader compatibility.
1 parent bb8021d commit ca5d16c

File tree

6 files changed

+193
-136
lines changed

6 files changed

+193
-136
lines changed

src/components/carousel/__tests__/carousel.test.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const mockProps: ICarouselUnControlled = {
5757
pageControlArrowsControlVariant: 'DEFAULT',
5858
};
5959

60+
const numPages = Math.ceil(mockProps.elements.length / (mockProps.numElementsPerPage as number));
61+
6062
describe('Carousel component', () => {
6163
it('Carousel', async () => {
6264
const { container, getByTestId } = renderProvider(<CarouselUnControlled {...mockProps} />);
@@ -390,7 +392,7 @@ describe('Carousel component', () => {
390392
allowShift: {
391393
current: true,
392394
},
393-
numPages: Math.ceil(mockProps.elements.length / (mockProps.numElementsPerPage as number)),
395+
numPages,
394396
innerCurrentPage: { current: 0 },
395397
});
396398
jest.spyOn(CarouselHooks, 'useCarousel').mockImplementation(mockMockUseCarousel);
@@ -434,7 +436,7 @@ describe('Carousel component', () => {
434436
allowShift: {
435437
current: true,
436438
},
437-
numPages: Math.ceil(mockProps.elements.length / (mockProps.numElementsPerPage as number)),
439+
numPages,
438440
innerCurrentPage: { current: 0 },
439441
});
440442
jest.spyOn(CarouselHooks, 'useCarousel').mockImplementation(mockMockUseCarousel);
@@ -449,11 +451,14 @@ describe('Carousel component', () => {
449451

450452
const rightArrow = getByLabelText(pageControlAutomateConfig.rightArrow.icon.altText);
451453
const leftArrow = getByLabelText(pageControlAutomateConfig.leftArrow.icon.altText);
454+
const indicator1 = getByLabelText(`Bar 1 of ${numPages}`);
452455

453456
fireEvent.click(rightArrow);
454457
expect(mockHandlePageChange).toHaveBeenCalledWith(1);
455458

456459
fireEvent.click(leftArrow);
457460
expect(mockHandlePageChange).toHaveBeenCalledWith(-1);
461+
462+
expect(indicator1).toBeInTheDocument();
458463
});
459464
});

src/components/carousel/carousel.styled.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getStyles } from '@/utils/getStyles/getStyles';
44

55
import { CarouselArrowStateType, CarouselPropsStylesType } from './types';
66

7-
export const RootStyled = styled.div<{
7+
export const RootStyled = styled.section<{
88
styles: CarouselPropsStylesType;
99
allowModifySliceWidth?: boolean;
1010
}>`

src/components/carousel/carouselStandAlone.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import * as React from 'react';
44
import { ElementOrIcon } from '@/components/elementOrIcon';
55
import { PageControl } from '@/components/pageControl';
66
import { useId } from '@/hooks';
7+
import { AriaLiveOptionType } from '@/types';
78

89
import { PageControlArrowControlType } from '../pageControl/types';
910
import { PageControlAutomate } from '../pageControlAutomate';
11+
import { ScreenReaderOnly } from '../screenReaderOnly';
1012
import {
1113
ArrowAndCarouselWrapperStyled,
1214
ArrowLeftIconContainer,
@@ -19,6 +21,7 @@ import {
1921
} from './carousel.styled';
2022
import { ExtraPaddingArrowStandAlone } from './components';
2123
import { ICarouselStandAlone } from './types';
24+
import { buildScreenReaderText } from './utils';
2225

2326
const CarouselStandAloneComponent = (
2427
{
@@ -47,6 +50,7 @@ const CarouselStandAloneComponent = (
4750
onMouseOut,
4851
onMouseOver,
4952
playing,
53+
screenReaderText,
5054
...props
5155
}: ICarouselStandAlone,
5256
ref: React.ForwardedRef<HTMLDivElement> | undefined | null
@@ -78,6 +82,7 @@ const CarouselStandAloneComponent = (
7882
ref={ref}
7983
allowModifySliceWidth={allowModifySliceWidth}
8084
aria-labelledby={props['aria-labelledby']}
85+
aria-roledescription="carousel"
8186
data-testid={`${dataTestId}Wrapper`}
8287
styles={styles}
8388
onKeyDown={onKeyDown}
@@ -107,6 +112,7 @@ const CarouselStandAloneComponent = (
107112
>
108113
<CarouselContentStyled
109114
ref={carouselContentRef}
115+
aria-live={playing ? AriaLiveOptionType.OFF : AriaLiveOptionType.POLITE}
110116
centerMode={centerMode}
111117
data-testid={`${dataTestId}Content`}
112118
disableSwipe={disableSwipe}
@@ -135,7 +141,6 @@ const CarouselStandAloneComponent = (
135141
onClick={onRightArrowClick}
136142
/>
137143
</CarouselContainerStyled>
138-
139144
{displayArrowsOnCarousel && numPages > 1 && (
140145
<ArrowRightIconContainer data-disabled={rightArrowDisabled} styles={styles}>
141146
<ElementOrIcon
@@ -151,7 +156,7 @@ const CarouselStandAloneComponent = (
151156
</ArrowRightIconContainer>
152157
)}
153158
</ArrowAndCarouselWrapperStyled>
154-
{hasPagination && numPages > 1 && (
159+
{hasPagination && pageControlArrowsControlVariant && pageControlVariant && numPages > 1 && (
155160
<PageControlContainerStyled styles={styles}>
156161
<PageControl
157162
arrowsControlVariant={pageControlArrowsControlVariant}
@@ -180,6 +185,12 @@ const CarouselStandAloneComponent = (
180185
/>
181186
</PageControlAutomateContainerStyled>
182187
)}
188+
<ScreenReaderOnly
189+
ariaLive={playing ? AriaLiveOptionType.OFF : AriaLiveOptionType.POLITE}
190+
dataTestId={`${dataTestId}CarouselScreenReader`}
191+
>
192+
{buildScreenReaderText(currentPage, numPages, screenReaderText)}
193+
</ScreenReaderOnly>
183194
</RootStyled>
184195
);
185196
};

0 commit comments

Comments
 (0)