Skip to content

Commit 742e728

Browse files
fix(next modal): prevent auto aria-labelledby if aria-label is passed (#11088)
1 parent 646ef0a commit 742e728

4 files changed

Lines changed: 10 additions & 35 deletions

File tree

packages/react-core/src/next/components/Modal/Modal.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,7 @@ class Modal extends React.Component<ModalProps, ModalState> {
114114
isEmpty = (value: string | null | undefined) => value === null || value === undefined || value === '';
115115

116116
componentDidMount() {
117-
const {
118-
appendTo,
119-
'aria-describedby': ariaDescribedby,
120-
'aria-label': ariaLabel,
121-
'aria-labelledby': ariaLabelledby
122-
} = this.props;
117+
const { appendTo } = this.props;
123118
const target: HTMLElement = this.getElement(appendTo);
124119
const container = document.createElement('div');
125120
this.setState({ container });
@@ -131,11 +126,6 @@ class Modal extends React.Component<ModalProps, ModalState> {
131126
} else {
132127
target.classList.remove(css(styles.backdropOpen));
133128
}
134-
135-
if (!ariaDescribedby && !ariaLabel && !ariaLabelledby) {
136-
// eslint-disable-next-line no-console
137-
console.error('Modal: Specify at least one of: aria-describedby, aria-label, aria-labelledby.');
138-
}
139129
}
140130

141131
componentDidUpdate() {

packages/react-core/src/next/components/Modal/ModalContent.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,14 @@ export const ModalContent: React.FunctionComponent<ModalContentProps> = ({
7070
return null;
7171
}
7272

73-
const ariaLabelledbyFormatted = (): string => {
74-
const idRefList: string[] = [];
75-
if (ariaLabel && boxId) {
76-
idRefList.push(ariaLabel && boxId);
77-
}
73+
const getAriaLabelledBy = (): string | undefined => {
7874
if (ariaLabelledby) {
79-
idRefList.push(ariaLabelledby);
75+
return ariaLabelledby;
8076
}
81-
if (idRefList.length === 0) {
77+
if (ariaLabel) {
8278
return undefined;
83-
} else {
84-
return idRefList.join(' ');
8579
}
80+
return boxId;
8681
};
8782

8883
const modalBox = (
@@ -92,7 +87,7 @@ export const ModalContent: React.FunctionComponent<ModalContentProps> = ({
9287
position={position}
9388
positionOffset={positionOffset}
9489
aria-label={ariaLabel}
95-
aria-labelledby={ariaLabelledbyFormatted()}
90+
aria-labelledby={getAriaLabelledBy()}
9691
aria-describedby={ariaDescribedby}
9792
{...getOUIAProps(ModalContent.displayName, ouiaId, ouiaSafe)}
9893
style={

packages/react-core/src/next/components/Modal/__tests__/Modal.test.tsx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ describe('Modal', () => {
8383
expect(screen.queryByRole('button', { name: 'Close' })).toBeNull();
8484
});
8585

86-
test('modal generates console error when no accessible name is provided', () => {
87-
const props = {
88-
onClose: jest.fn(),
89-
isOpen: true,
90-
children: 'modal content'
91-
};
92-
const consoleErrorMock = jest.fn();
93-
global.console = { error: consoleErrorMock } as any;
94-
95-
render(<Modal {...props} />);
96-
97-
expect(consoleErrorMock).toHaveBeenCalled();
98-
});
99-
10086
test('modal adds aria-hidden attribute to its siblings when open', () => {
10187
render(<ModalWithSiblings />, { container: document.body.appendChild(target) });
10288

packages/react-core/src/next/components/Modal/__tests__/__snapshots__/ModalContent.test.tsx.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ exports[`Modal Content Test description 1`] = `
99
class="pf-v5-l-bullseye"
1010
>
1111
<div
12+
aria-labelledby="boxId"
1213
aria-modal="true"
1314
class="pf-v5-c-modal-box"
1415
data-ouia-component-type="PF5/ModalContent"
@@ -34,6 +35,7 @@ exports[`Modal Content Test isOpen 1`] = `
3435
class="pf-v5-l-bullseye"
3536
>
3637
<div
38+
aria-labelledby="boxId"
3739
aria-modal="true"
3840
class="pf-v5-c-modal-box"
3941
data-ouia-component-type="PF5/ModalContent"
@@ -59,6 +61,7 @@ exports[`Modal Content Test only body 1`] = `
5961
class="pf-v5-l-bullseye"
6062
>
6163
<div
64+
aria-labelledby="boxId"
6265
aria-modal="true"
6366
class="pf-v5-c-modal-box"
6467
data-ouia-component-type="PF5/ModalContent"
@@ -84,6 +87,7 @@ exports[`Modal Content Test with onclose 1`] = `
8487
class="pf-v5-l-bullseye"
8588
>
8689
<div
90+
aria-labelledby="boxId"
8791
aria-modal="true"
8892
class="pf-v5-c-modal-box pf-m-lg"
8993
data-ouia-component-type="PF5/ModalContent"

0 commit comments

Comments
 (0)