Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Modal): Prevent auto aria-labelledby if aria-label is passed #11012

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions packages/react-core/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ class Modal extends React.Component<ModalProps, ModalState> {
isEmpty = (value: string | null | undefined) => value === null || value === undefined || value === '';

componentDidMount() {
const {
appendTo,
'aria-describedby': ariaDescribedby,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby
} = this.props;
const { appendTo } = this.props;
const target: HTMLElement = this.getElement(appendTo);
const container = document.createElement('div');
this.setState({ container });
Expand All @@ -131,11 +126,6 @@ class Modal extends React.Component<ModalProps, ModalState> {
} else {
target.classList.remove(css(styles.backdropOpen));
}

if (!ariaDescribedby && !ariaLabel && !ariaLabelledby) {
// eslint-disable-next-line no-console
console.error('Modal: Specify at least one of: aria-describedby, aria-label, aria-labelledby.');
}
}

componentDidUpdate() {
Expand Down
15 changes: 5 additions & 10 deletions packages/react-core/src/components/Modal/ModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,14 @@ export const ModalContent: React.FunctionComponent<ModalContentProps> = ({
return null;
}

const ariaLabelledbyFormatted = (): string => {
const idRefList: string[] = [];
if (ariaLabel && boxId) {
idRefList.push(ariaLabel && boxId);
}
const getAriaLabelledBy = (): string | undefined => {
if (ariaLabelledby) {
idRefList.push(ariaLabelledby);
return ariaLabelledby;
}
if (idRefList.length === 0) {
if (ariaLabel) {
return undefined;
} else {
return idRefList.join(' ');
}
return boxId;
};

const modalBox = (
Expand All @@ -92,7 +87,7 @@ export const ModalContent: React.FunctionComponent<ModalContentProps> = ({
position={position}
positionOffset={positionOffset}
aria-label={ariaLabel}
aria-labelledby={ariaLabelledbyFormatted()}
aria-labelledby={getAriaLabelledBy()}
aria-describedby={ariaDescribedby}
{...getOUIAProps(ModalContent.displayName, ouiaId, ouiaSafe)}
style={
Expand Down
14 changes: 0 additions & 14 deletions packages/react-core/src/components/Modal/__tests__/Modal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,6 @@ describe('Modal', () => {
expect(screen.queryByRole('button', { name: 'Close' })).toBeNull();
});

test('modal generates console error when no accessible name is provided', () => {
const props = {
onClose: jest.fn(),
isOpen: true,
children: 'modal content'
};
const consoleErrorMock = jest.fn();
global.console = { error: consoleErrorMock } as any;

render(<Modal {...props} />);

expect(consoleErrorMock).toHaveBeenCalled();
});

test('modal adds aria-hidden attribute to its siblings when open', () => {
render(<ModalWithSiblings />, { container: document.body.appendChild(target) });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports[`Modal Content Test description 1`] = `
class="pf-v6-l-bullseye"
>
<div
aria-labelledby="boxId"
aria-modal="true"
class="pf-v6-c-modal-box"
data-ouia-component-type="PF6/ModalContent"
Expand All @@ -34,6 +35,7 @@ exports[`Modal Content Test isOpen 1`] = `
class="pf-v6-l-bullseye"
>
<div
aria-labelledby="boxId"
aria-modal="true"
class="pf-v6-c-modal-box"
data-ouia-component-type="PF6/ModalContent"
Expand All @@ -59,6 +61,7 @@ exports[`Modal Content Test only body 1`] = `
class="pf-v6-l-bullseye"
>
<div
aria-labelledby="boxId"
aria-modal="true"
class="pf-v6-c-modal-box"
data-ouia-component-type="PF6/ModalContent"
Expand All @@ -84,6 +87,7 @@ exports[`Modal Content Test with onclose 1`] = `
class="pf-v6-l-bullseye"
>
<div
aria-labelledby="boxId"
aria-modal="true"
class="pf-v6-c-modal-box pf-m-lg"
data-ouia-component-type="PF6/ModalContent"
Expand Down
Loading