Skip to content

Commit bf2b62e

Browse files
author
Kubit
committed
Add aria-label to carousel component
1 parent 6764acc commit bf2b62e

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,15 +482,17 @@ describe('Carousel component', () => {
482482

483483
it('Should have aria-label in the document when prop is set', async () => {
484484
const { getByLabelText } = renderProvider(
485-
<CarouselUnControlled {...mockProps} aria-label="Mock aria-label" />
485+
<CarouselUnControlled {...mockProps} aria-label='Mock aria-label'/>
486486
);
487487

488488
const label = getByLabelText('Mock aria-label');
489489
expect(label).toBeInTheDocument();
490490
});
491491

492492
it('Should not have aria-label in the document when prop is not set', async () => {
493-
const { queryByLabelText } = renderProvider(<CarouselUnControlled {...mockProps} />);
493+
const { queryByLabelText } = renderProvider(
494+
<CarouselUnControlled {...mockProps} />
495+
);
494496

495497
const label = queryByLabelText('');
496498
expect(label).not.toBeInTheDocument();

src/components/carousel/carousel.styled.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ export const ArrowRightIconContainer = styled.div<{
158158
top: 0;
159159
${({ styles }) =>
160160
getStyles(styles[CarouselArrowStateType.DEFAULT]?.arrowRightIconButtonContainer)};
161+
&:focus {
162+
${props =>
163+
getStyles(props.styles[CarouselArrowStateType.DEFAULT]?.arrowLeftIconButtonContainer)}
164+
}
161165
&:hover {
162166
${props =>
163167
getStyles(props.styles[CarouselArrowStateType.HOVER]?.arrowRightIconButtonContainer)}

src/components/carousel/carouselUnControlled.tsx

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@ const CarouselUnControlledComponent = <V extends string | unknown>(
4242
};
4343

4444
const onLeftSwipe = () => {
45-
setCurrentPage(prevPage => {
46-
let newPage = prevPage;
47-
if (allowShift.current && (circular || prevPage !== 0)) {
48-
newPage = prevPage === 0 ? numPages - 1 : prevPage - 1;
49-
handlePageChange(-1);
50-
onPageChange?.(newPage);
51-
// onTransition is called internally when drag starts
52-
}
53-
return newPage;
54-
});
45+
if (allowShift.current && (circular || innerCurrentPage.current !== 0)) {
46+
const newPage = innerCurrentPage.current === 0 ? numPages - 1 : innerCurrentPage.current - 1;
47+
handlePageChange(-1);
48+
onPageChange?.(newPage);
49+
// onTransition is called internally when drag starts
50+
setCurrentPage(newPage);
51+
}
5552
};
5653

5754
const handleClickRightArrow = e => {
@@ -67,16 +64,13 @@ const CarouselUnControlledComponent = <V extends string | unknown>(
6764
};
6865

6966
const onRightSwipe = () => {
70-
setCurrentPage(prevPage => {
71-
let newPage = prevPage;
72-
if (allowShift.current && (circular || prevPage !== numPages - 1)) {
73-
newPage = (newPage + 1) % numPages;
74-
handlePageChange(1);
75-
onPageChange?.(newPage);
76-
// onTransition is called internally when drag starts
77-
}
78-
return newPage;
79-
});
67+
if (allowShift.current && (circular || innerCurrentPage.current !== numPages - 1)) {
68+
const newPage = (innerCurrentPage.current + 1) % numPages;
69+
handlePageChange(1);
70+
onPageChange?.(newPage);
71+
setCurrentPage(newPage);
72+
// onTransition is called internally when drag starts
73+
}
8074
};
8175

8276
const onKeyDown: React.KeyboardEventHandler<HTMLDivElement> = event => {

src/components/carousel/stories/carousel.stories.tsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { themesObject, variantsObject } from '@/designSystem/themesObject';
88
import { DeviceBreakpointsType, ROLES } from '@/types';
99

1010
import { CarouselUnControlled as Story } from '../carouselUnControlled';
11-
import { CarouselAlignType } from '../types';
11+
import { CarouselAlignType, ICarouselUnControlled } from '../types';
1212
import { argtypes } from './argtypes';
1313

1414
const themeSelected = localStorage.getItem('themeSelected') || 'kubit';
@@ -30,96 +30,106 @@ export default meta;
3030

3131
type Story = StoryObj<typeof meta> & { args: { themeArgs?: object } };
3232

33-
const storyArgs = {
33+
const storyArgs: ICarouselUnControlled = {
3434
variant: Object.values(variantsObject[themeSelected].CarouselVariantType || {})[0] as string,
3535
elements: [
3636
<ReplaceContent
3737
key={0}
38-
aria-label="1 of 10"
38+
aria-label="Slide 1 of 10"
3939
aria-roledescription="slide"
4040
id="carousel-item-1"
4141
role={ROLES.GROUP}
42+
tabIndex={0}
4243
>
4344
First Slide
4445
</ReplaceContent>,
4546
<ReplaceContent
4647
key={1}
47-
aria-label="2 of 10"
48+
aria-label="Slide 2 of 10"
4849
aria-roledescription="slide"
4950
id="carousel-item-2"
5051
role={ROLES.GROUP}
52+
tabIndex={0}
5153
>
5254
Second Slide
5355
</ReplaceContent>,
5456
<ReplaceContent
5557
key={2}
56-
aria-label="3 of 10"
58+
aria-label="Slide 3 of 10"
5759
aria-roledescription="slide"
5860
id="carousel-item-3"
5961
role={ROLES.GROUP}
62+
tabIndex={0}
6063
>
6164
Third Slide
6265
</ReplaceContent>,
6366
<ReplaceContent
6467
key={3}
65-
aria-label="4 of 10"
68+
aria-label="Slide 4 of 10"
6669
aria-roledescription="slide"
6770
id="carousel-item-4"
6871
role={ROLES.GROUP}
72+
tabIndex={0}
6973
>
7074
Fourth Slide
7175
</ReplaceContent>,
7276
<ReplaceContent
7377
key={4}
74-
aria-label="5 of 10"
78+
aria-label="Slide 5 of 10"
7579
aria-roledescription="slide"
7680
id="carousel-item-5"
7781
role={ROLES.GROUP}
82+
tabIndex={0}
7883
>
79-
Fifth Slice
84+
Fifth Slide
8085
</ReplaceContent>,
8186
<ReplaceContent
8287
key={5}
83-
aria-label="6 of 10"
88+
aria-label="Slide 6 of 10"
8489
aria-roledescription="slide"
8590
id="carousel-item-6"
8691
role={ROLES.GROUP}
92+
tabIndex={0}
8793
>
8894
Sixth Slide
8995
</ReplaceContent>,
9096
<ReplaceContent
9197
key={6}
92-
aria-label="7 of 10"
98+
aria-label="Slide 7 of 10"
9399
aria-roledescription="slide"
94100
id="carousel-item-7"
95101
role={ROLES.GROUP}
102+
tabIndex={0}
96103
>
97104
Seventh Slide
98105
</ReplaceContent>,
99106
<ReplaceContent
100107
key={7}
101-
aria-label="8 of 10"
108+
aria-label="Slide 8 of 10"
102109
aria-roledescription="slide"
103110
id="carousel-item-8"
104111
role={ROLES.GROUP}
112+
tabIndex={0}
105113
>
106114
Eighth Slide
107115
</ReplaceContent>,
108116
<ReplaceContent
109117
key={8}
110-
aria-label="9 of 10"
118+
aria-label="Slide 9 of 10"
111119
aria-roledescription="slide"
112120
id="carousel-item-9"
113121
role={ROLES.GROUP}
122+
tabIndex={0}
114123
>
115124
Ninth Slide
116125
</ReplaceContent>,
117126
<ReplaceContent
118127
key={9}
119-
aria-label="10 of 10"
128+
aria-label="Slide 10 of 10"
120129
aria-roledescription="slide"
121130
id="carousel-item-10"
122131
role={ROLES.GROUP}
132+
tabIndex={0}
123133
>
124134
Tenth Slide
125135
</ReplaceContent>,

0 commit comments

Comments
 (0)