Skip to content

Commit a5a2060

Browse files
authored
Merge pull request #1082 from google/fix/345-dialog-accessibility
Update Dialog for accessibility
2 parents b4c10cd + 3aa4f35 commit a5a2060

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

assets/js/components/dialog.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import FocusTrap from 'focus-trap-react';
2828
/**
2929
* WordPress dependencies
3030
*/
31+
import { withInstanceId } from '@wordpress/compose';
3132
import { Component, createRef } from '@wordpress/element';
3233
import { __ } from '@wordpress/i18n';
3334

@@ -61,16 +62,20 @@ class Dialog extends Component {
6162
subtitle,
6263
confirmButton,
6364
dependentModules,
65+
instanceId,
6466
} = this.props;
6567

68+
const labelledByID = `googlesitekit-dialog-label-${ instanceId }`;
69+
const describedByID = `googlesitekit-dialog-description-${ instanceId }`;
70+
6671
return (
6772
<div
6873
ref={ this.dialogRef }
6974
className={ `mdc-dialog ${ dialogActive ? 'mdc-dialog--open' : '' }` }
7075
role="alertdialog"
7176
aria-modal="true"
72-
aria-labelledby="remove-module-dialog"
73-
aria-describedby="remove-module-dialog-description"
77+
aria-labelledby={ title ? labelledByID : undefined }
78+
aria-describedby={ ( provides && provides.length ) ? describedByID : undefined }
7479
aria-hidden={ dialogActive ? 'false' : 'true' }
7580
tabIndex="-1"
7681
>
@@ -80,7 +85,7 @@ class Dialog extends Component {
8085
<div className="mdc-dialog__container">
8186
<div className="mdc-dialog__surface">
8287
{ title &&
83-
<h2 id="remove-module-dialog" className="mdc-dialog__title">
88+
<h2 id={ labelledByID } className="mdc-dialog__title">
8489
{ title }
8590
</h2>
8691
}
@@ -89,15 +94,17 @@ class Dialog extends Component {
8994
{ subtitle }
9095
</p>
9196
}
92-
<section id="remove-module-dialog-description" className="mdc-dialog__content">
93-
<ul className="mdc-list mdc-list--underlined mdc-list--non-interactive">
94-
{ provides && provides.map( ( attribute ) => (
95-
<li className="mdc-list-item" key={ attribute }>
96-
<span className="mdc-list-item__text">{ attribute }</span>
97-
</li>
98-
) ) }
99-
</ul>
100-
</section>
97+
{ ( provides && provides.length ) &&
98+
<section id={ describedByID } className="mdc-dialog__content">
99+
<ul className="mdc-list mdc-list--underlined mdc-list--non-interactive">
100+
{ provides.map( ( attribute ) => (
101+
<li className="mdc-list-item" key={ attribute }>
102+
<span className="mdc-list-item__text">{ attribute }</span>
103+
</li>
104+
) ) }
105+
</ul>
106+
</section>
107+
}
101108
{ dependentModules &&
102109
<p className="mdc-dialog__dependecies">
103110
<strong>{ __( 'Note: ', 'google-site-kit' ) }</strong>{ dependentModules }
@@ -140,4 +147,4 @@ Dialog.defaultProps = {
140147
confirmButton: null,
141148
};
142149

143-
export default Dialog;
150+
export default withInstanceId( Dialog );

0 commit comments

Comments
 (0)