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(ComposedModal): add accessible name and aria-label prop #7609

Merged
merged 5 commits into from
Jan 27, 2021
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
3 changes: 3 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,9 @@ Map {
"selectorPrimaryFocus": "[data-modal-primary-focus]",
},
"propTypes": Object {
"aria-label": Object {
"type": "string",
},
"children": Object {
"type": "node",
},
Expand Down
20 changes: 17 additions & 3 deletions packages/react/src/components/ComposedModal/ComposedModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export default class ComposedModal extends Component {
endSentinel = React.createRef();

static propTypes = {
/**
* Specify the aria-label for bx--modal-container
*/
['aria-label']: PropTypes.string,

/**
* Specify the content to be placed in the ComposedModal
*/
Expand Down Expand Up @@ -216,6 +221,7 @@ export default class ComposedModal extends Component {
render() {
const { open } = this.state;
const {
['aria-label']: ariaLabel,
className,
containerClassName,
children,
Expand All @@ -239,9 +245,12 @@ export default class ComposedModal extends Component {
[containerClassName]: containerClassName,
});

// Generate aria-label based on Modal Header label if one is not provided (L253)
let generatedAriaLabel;
const childrenWithProps = React.Children.toArray(children).map((child) => {
switch (child.type) {
case React.createElement(ModalHeader).type:
generatedAriaLabel = child.props.label;
return React.cloneElement(child, {
closeModal: this.closeModal,
});
Expand Down Expand Up @@ -273,7 +282,12 @@ export default class ComposedModal extends Component {
className={`${prefix}--visually-hidden`}>
Focus sentinel
</span>
<div ref={this.innerModal} className={containerClass} role="dialog">
<div
ref={this.innerModal}
className={containerClass}
role="dialog"
aria-modal="true"
aria-label={ariaLabel ? ariaLabel : generatedAriaLabel}>
{childrenWithProps}
</div>
{/* Non-translatable: Focus-wrap code makes this `<span>` not actually read by screen readers */}
Expand Down Expand Up @@ -403,9 +417,9 @@ export class ModalHeader extends Component {

return (
<div className={headerClass} {...other}>
{label && <p className={labelClass}>{label}</p>}
{label && <h2 className={labelClass}>{label}</h2>}

{title && <p className={titleClass}>{title}</p>}
{title && <h3 className={titleClass}>{title}</h3>}

{children}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports[`<ComposedModal /> renders 1`] = `
Focus sentinel
</span>
<div
aria-modal="true"
className="bx--modal-container"
role="dialog"
/>
Expand Down