Skip to content

Commit

Permalink
fix(ComposedModal): add accessible name and aria-label prop (#7609)
Browse files Browse the repository at this point in the history
* fix(ComposedModal): add accessible name and aria-label prop

* chore(tests): update snapshots

* fix(ComposedModal): generate aria-label only if one is not provided

Co-authored-by: Andrea N. Cardona <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 27, 2021
1 parent 73a5b71 commit 96aa677
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
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

0 comments on commit 96aa677

Please sign in to comment.