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(next modal): prevent auto aria-labelledby if aria-label is passed #11088

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
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ exports[`Disclosure ExpandableSection 1`] = `
>
<button
aria-controls="content-id"
aria-expanded="false"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updates to these snapshots were needed because the last v5 PR didn't update them and they were failing.

class="pf-v5-c-expandable-section__toggle"
id="toggle-id"
type="button"
Expand Down Expand Up @@ -103,6 +104,7 @@ exports[`ExpandableSection 1`] = `
>
<button
aria-controls="content-id"
aria-expanded="false"
class="pf-v5-c-expandable-section__toggle"
id="toggle-id"
type="button"
Expand Down Expand Up @@ -238,6 +240,7 @@ exports[`Renders Uncontrolled ExpandableSection 1`] = `
>
<button
aria-controls="content-id"
aria-expanded="false"
class="pf-v5-c-expandable-section__toggle"
id="toggle-id"
type="button"
Expand Down
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
Loading