Skip to content

Commit 469bcdd

Browse files
authored
fix(accordion): add aria-label support to Accordion component buttons (#20514)
* fix(accordion): add aria-label support to Accordion component buttons * fix(accordion): update snapshot test
1 parent 1211081 commit 469bcdd

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ Map {
247247
},
248248
"AccordionItem" => {
249249
"propTypes": {
250+
"aria-label": {
251+
"type": "string",
252+
},
250253
"children": {
251254
"type": "node",
252255
},

packages/react/src/components/Accordion/AccordionItem.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export interface AccordionItemProps {
3939
*/
4040
disabled?: boolean;
4141

42+
/**
43+
* Specify a custom label for the accordion button.
44+
* This is important for accessibility when the accordion has no visible title.
45+
*/
46+
'aria-label'?: AriaAttributes['aria-label'];
47+
4248
/**
4349
* The handler of the massaged `click` event.
4450
*/
@@ -95,6 +101,7 @@ export interface AccordionItemProps {
95101
export interface AccordionToggleProps {
96102
'aria-controls'?: AriaAttributes['aria-controls'];
97103
'aria-expanded'?: AriaAttributes['aria-expanded'];
104+
'aria-label'?: AriaAttributes['aria-label'];
98105
className?: string;
99106
disabled?: boolean;
100107
onClick?: MouseEventHandler<HTMLButtonElement>;
@@ -116,6 +123,7 @@ function AccordionItem({
116123
title = 'title',
117124
disabled: controlledDisabled,
118125
handleAnimationEnd,
126+
'aria-label': ariaLabel,
119127
...rest
120128
}: PropsWithChildren<AccordionItemProps>) {
121129
const [isOpen, setIsOpen] = useState(open);
@@ -187,6 +195,7 @@ function AccordionItem({
187195
disabled={disabled}
188196
aria-controls={id}
189197
aria-expanded={isOpen}
198+
aria-label={ariaLabel}
190199
className={`${prefix}--accordion__heading`}
191200
onClick={onClick}
192201
onKeyDown={onKeyDown}
@@ -224,6 +233,12 @@ AccordionItem.propTypes = {
224233
*/
225234
disabled: PropTypes.bool,
226235

236+
/**
237+
* Specify a custom label for the accordion button.
238+
* This is important for accessibility when the accordion has no visible title.
239+
*/
240+
'aria-label': PropTypes.string,
241+
227242
/**
228243
* The handler of the massaged `click` event.
229244
*/

packages/react/src/components/Accordion/__tests__/AccordionItem-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ describe('AccordionItem', () => {
8585

8686
expect(screen.getByText('Test title')).toBeInTheDocument();
8787
});
88+
89+
it('should respect aria-label prop', () => {
90+
render(
91+
<AccordionItem title="Test title" aria-label="Custom accordion label" />
92+
);
93+
94+
expect(screen.getByRole('button')).toHaveAttribute(
95+
'aria-label',
96+
'Custom accordion label'
97+
);
98+
});
8899
});
89100

90101
describe('behaves as expected', () => {

0 commit comments

Comments
 (0)