Skip to content

Commit

Permalink
fix(next modal): prevent auto aria-labelledby if aria-label is passed (
Browse files Browse the repository at this point in the history
  • Loading branch information
wise-king-sullyman authored Oct 9, 2024
1 parent 646ef0a commit 742e728
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 35 deletions.
12 changes: 1 addition & 11 deletions packages/react-core/src/next/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/next/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
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-v5-l-bullseye"
>
<div
aria-labelledby="boxId"
aria-modal="true"
class="pf-v5-c-modal-box"
data-ouia-component-type="PF5/ModalContent"
Expand All @@ -34,6 +35,7 @@ exports[`Modal Content Test isOpen 1`] = `
class="pf-v5-l-bullseye"
>
<div
aria-labelledby="boxId"
aria-modal="true"
class="pf-v5-c-modal-box"
data-ouia-component-type="PF5/ModalContent"
Expand All @@ -59,6 +61,7 @@ exports[`Modal Content Test only body 1`] = `
class="pf-v5-l-bullseye"
>
<div
aria-labelledby="boxId"
aria-modal="true"
class="pf-v5-c-modal-box"
data-ouia-component-type="PF5/ModalContent"
Expand All @@ -84,6 +87,7 @@ exports[`Modal Content Test with onclose 1`] = `
class="pf-v5-l-bullseye"
>
<div
aria-labelledby="boxId"
aria-modal="true"
class="pf-v5-c-modal-box pf-m-lg"
data-ouia-component-type="PF5/ModalContent"
Expand Down

0 comments on commit 742e728

Please sign in to comment.