Skip to content

Commit

Permalink
[added] Add contentLabel prop to put aria-label on modal content
Browse files Browse the repository at this point in the history
This makes the modal better follow aria guidelines for dialogs.
It also gives screenreader users some identity for the modal after
it opens.

closes reactjs#236
  • Loading branch information
claydiffrient committed Oct 15, 2016
1 parent 11c1fd6 commit 3d8e5a0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ npm install --save react-modal
onRequestClose={requestCloseFn}
closeTimeoutMS={n}
style={customStyle}
contentLabel="Modal"
>
<h1>Modal Content</h1>
<p>Etc.</p>
Expand Down Expand Up @@ -136,7 +137,9 @@ var App = React.createClass({
isOpen={this.state.modalIsOpen}
onAfterOpen={this.afterOpenModal}
onRequestClose={this.closeModal}
style={customStyles} >
style={customStyles}
contentLabel="Example Modal"
>

<h2 ref="subtitle">Hello</h2>
<button onClick={this.closeModal}>close</button>
Expand Down Expand Up @@ -171,7 +174,9 @@ pass the 'shouldCloseOnOverlayClick' prop with 'false' value.
onRequestClose={requestCloseFn}
closeTimeoutMS={n}
shouldCloseOnOverlayClick={false}
style={customStyle}>
style={customStyle}
contentLabel="No Overlay Click Modal"
>

<h1>Force Modal</h1>
<p>Modal cannot be closed when clicking the overlay area</p>
Expand Down
3 changes: 2 additions & 1 deletion lib/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ var Modal = React.createClass({
closeTimeoutMS: React.PropTypes.number,
ariaHideApp: React.PropTypes.bool,
shouldCloseOnOverlayClick: React.PropTypes.bool,
role: React.PropTypes.string
role: React.PropTypes.string,
contentLabel: React.PropTypes.string.isRequired
},

getDefaultProps: function () {
Expand Down
3 changes: 2 additions & 1 deletion lib/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ var ModalPortal = module.exports = React.createClass({
onKeyDown: this.handleKeyDown,
onMouseDown: this.handleContentMouseDown,
onMouseUp: this.handleContentMouseUp,
role: this.props.role
role: this.props.role,
"aria-label": this.props.contentLabel
},
this.props.children
)
Expand Down
7 changes: 7 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ describe('Modal', function () {
unmountModal();
});

it('renders the modal with a aria-label based on the contentLabel prop', function () {
var child = 'I am a child of Modal, and he has sent me here...';
var component = renderModal({isOpen: true, contentLabel: 'Special Modal'}, child);
equal(component.portal.refs.content.getAttribute('aria-label'), 'Special Modal');
unmountModal();
});

it('has default props', function() {
var node = document.createElement('div');
Modal.setAppElement(document.createElement('div'));
Expand Down

0 comments on commit 3d8e5a0

Please sign in to comment.