File tree 7 files changed +56
-3
lines changed
7 files changed +56
-3
lines changed Original file line number Diff line number Diff line change 28
28
"react/no-find-dom-node": [0],
29
29
"react/jsx-closing-bracket-location": [0],
30
30
"react/jsx-filename-extension": ["error", {"extensions": [".js"]}],
31
+ "react/forbid-prop-types": [1, {"forbid": ["any"]}],
31
32
"react/require-default-props": 0
32
33
}
33
34
}
Original file line number Diff line number Diff line change @@ -76,6 +76,28 @@ Modal.setAppElement(appElement);
76
76
Modal .setAppElement (' #your-app-element' );
77
77
```
78
78
79
+ ### Additional Aria Attributes
80
+
81
+ Use the property ` aria ` to pass any additional aria attributes. It accepts
82
+ an object where the keys are the names of the attributes without the prefix
83
+ ` aria- ` .
84
+
85
+ Example:
86
+
87
+ ``` js
88
+ < Modal
89
+ isOpen= {modalIsOpen}
90
+ aria= {{
91
+ labelledby: " heading" ,
92
+ describedby: " full_description"
93
+ }}>
94
+ < h1 id= " heading" > H1 < / h1>
95
+ < div id= " full_description" >
96
+ < p> Description goes here.< / p>
97
+ < / div>
98
+ < / Modal>
99
+ ```
100
+
79
101
## Styles
80
102
81
103
Styles are passed as an object with 2 keys, 'overlay' and 'content' like so
Original file line number Diff line number Diff line change @@ -82,6 +82,13 @@ import ReactModal from 'react-modal';
82
82
Function that will be called to get the parent element that the modal will be attached to.
83
83
*/
84
84
parentSelector= {() => document .body }
85
+ /*
86
+ Additional aria attributes (optional).
87
+ */
88
+ aria= {{
89
+ labelledby: " heading" ,
90
+ describedby: " full_description"
91
+ }}
85
92
/ >
86
93
```
87
94
Original file line number Diff line number Diff line change @@ -69,12 +69,19 @@ class App extends Component {
69
69
</ Modal >
70
70
< Modal ref = "mymodal2"
71
71
id = "test2"
72
+ aria = { {
73
+ labelledby : "heading" ,
74
+ describedby : "fulldescription"
75
+ } }
72
76
closeTimeoutMS = { 150 }
73
77
contentLabel = "modalB"
74
78
isOpen = { modal2 }
75
79
onAfterOpen = { ( ) => { } }
76
80
onRequestClose = { this . toggleModal_2 } >
77
- < p > test</ p >
81
+ < h1 id = "heading" > This is the modal 2!</ h1 >
82
+ < div id = "fulldescription" tabIndex = "0" role = "document" >
83
+ < p > This is a description of what it does: nothing :)</ p >
84
+ </ div >
78
85
</ Modal >
79
86
</ div >
80
87
) ;
Original file line number Diff line number Diff line change @@ -239,7 +239,7 @@ describe('State', () => {
239
239
unmountModal ( ) ;
240
240
expect ( ! isBodyWithReactModalOpenClass ( ) ) . toBeTruthy ( ) ;
241
241
} ) ;
242
-
242
+
243
243
it ( 'should not add classes to document.body for unopened modals' , ( ) => {
244
244
renderModal ( { isOpen : true } ) ;
245
245
expect ( isBodyWithReactModalOpenClass ( ) ) . toBeTruthy ( ) ;
@@ -259,6 +259,14 @@ describe('State', () => {
259
259
expect ( isBodyWithReactModalOpenClass ( ) ) . toBeTruthy ( ) ;
260
260
} ) ;
261
261
262
+ it ( 'additional aria attributes' , ( ) => {
263
+ const modal = renderModal ( { isOpen : true , aria : { labelledby : "a" } } , 'hello' ) ;
264
+ expect (
265
+ mcontent ( modal ) . getAttribute ( 'aria-labelledby' )
266
+ ) . toEqual ( "a" ) ;
267
+ unmountModal ( ) ;
268
+ } ) ;
269
+
262
270
it ( 'adding/removing aria-hidden without an appElement will try to fallback to document.body' , ( ) => {
263
271
ariaAppHider . documentNotReadyOrSSRTesting ( ) ;
264
272
const node = document . createElement ( 'div' ) ;
Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ export default class Modal extends Component {
53
53
ariaHideApp : PropTypes . bool ,
54
54
shouldCloseOnOverlayClick : PropTypes . bool ,
55
55
parentSelector : PropTypes . func ,
56
+ aria : PropTypes . object ,
56
57
role : PropTypes . string ,
57
58
contentLabel : PropTypes . string . isRequired
58
59
} ;
Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ export default class ModalPortal extends Component {
51
51
shouldCloseOnOverlayClick : PropTypes . bool ,
52
52
role : PropTypes . string ,
53
53
contentLabel : PropTypes . string ,
54
+ aria : PropTypes . object ,
54
55
children : PropTypes . node
55
56
} ;
56
57
@@ -254,6 +255,11 @@ export default class ModalPortal extends Component {
254
255
`${ className } ${ additional } ` : className ;
255
256
}
256
257
258
+ ariaAttributes = items => Object . keys ( items ) . reduce ( ( acc , name ) => {
259
+ acc [ `aria-${ name } ` ] = items [ name ] ;
260
+ return acc ;
261
+ } , { } ) ;
262
+
257
263
render ( ) {
258
264
const { className, overlayClassName, defaultStyles } = this . props ;
259
265
const contentStyles = className ? { } : defaultStyles . content ;
@@ -273,7 +279,8 @@ export default class ModalPortal extends Component {
273
279
onKeyDown = { this . handleKeyDown }
274
280
onClick = { this . handleContentOnClick }
275
281
role = { this . props . role }
276
- aria-label = { this . props . contentLabel } >
282
+ aria-label = { this . props . contentLabel }
283
+ { ...this . ariaAttributes ( this . props . aria || { } ) } >
277
284
{ this . props . children }
278
285
</ div >
279
286
</ div >
You can’t perform that action at this time.
0 commit comments