/
index.js
169 lines (152 loc) · 4.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* WordPress dependencies
*/
import { Component, createPortal } from '@wordpress/element';
import { withInstanceId } from '@wordpress/compose';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import ModalFrame from './frame';
import ModalHeader from './header';
import * as ariaHelper from './aria-helper';
// Used to count the number of open modals.
let parentElement,
openModalCount = 0;
class Modal extends Component {
constructor( props ) {
super( props );
this.prepareDOM();
}
/**
* Appends the modal's node to the DOM, so the portal can render the
* modal in it. Also calls the openFirstModal when this is the first modal to be
* opened.
*/
componentDidMount() {
openModalCount++;
if ( openModalCount === 1 ) {
this.openFirstModal();
}
}
/**
* Removes the modal's node from the DOM. Also calls closeLastModal when this is
* the last modal to be closed.
*/
componentWillUnmount() {
openModalCount--;
if ( openModalCount === 0 ) {
this.closeLastModal();
}
this.cleanDOM();
}
/**
* Prepares the DOM for the modals to be rendered.
*
* Every modal is mounted in a separate div appended to a parent div
* that is appended to the document body.
*
* The parent div will be created if it does not yet exist, and the
* separate div for this specific modal will be appended to that.
*/
prepareDOM() {
if ( ! parentElement ) {
parentElement = document.createElement( 'div' );
document.body.appendChild( parentElement );
}
this.node = document.createElement( 'div' );
parentElement.appendChild( this.node );
}
/**
* Removes the specific mounting point for this modal from the DOM.
*/
cleanDOM() {
parentElement.removeChild( this.node );
}
/**
* Prepares the DOM for this modal and any additional modal to be mounted.
*
* It appends an additional div to the body for the modals to be rendered in,
* it hides any other elements from screen-readers and adds an additional class
* to the body to prevent scrolling while the modal is open.
*/
openFirstModal() {
ariaHelper.hideApp( parentElement );
document.body.classList.add( this.props.bodyOpenClassName );
}
/**
* Cleans up the DOM after the last modal is closed and makes the app available
* for screen-readers again.
*/
closeLastModal() {
document.body.classList.remove( this.props.bodyOpenClassName );
ariaHelper.showApp();
}
/**
* Renders the modal.
*
* @return {WPElement} The modal element.
*/
render() {
const {
onRequestClose,
title,
icon,
closeButtonLabel,
children,
aria,
instanceId,
isDismissible,
isDismissable, //Deprecated
// Many of the documented props for Modal are passed straight through
// to the ModalFrame component and handled there.
...otherProps
} = this.props;
const headingId = aria.labelledby || `components-modal-header-${ instanceId }`;
if ( isDismissable ) {
deprecated( 'isDismissable prop of the Modal component', {
alternative: 'isDismissible prop (renamed) of the Modal component',
} );
}
// Disable reason: this stops mouse events from triggering tooltips and
// other elements underneath the modal overlay.
return createPortal(
<ModalFrame
onRequestClose={ onRequestClose }
aria={ {
labelledby: title ? headingId : null,
describedby: aria.describedby,
} }
{ ...otherProps }
>
<div className={ 'components-modal__content' } tabIndex="0" role="document">
<ModalHeader
closeLabel={ closeButtonLabel }
headingId={ headingId }
icon={ icon }
isDismissible={ isDismissible || isDismissable }
onClose={ onRequestClose }
title={ title }
/>
{ children }
</div>
</ModalFrame>,
this.node
);
}
}
Modal.defaultProps = {
bodyOpenClassName: 'modal-open',
role: 'dialog',
title: null,
focusOnMount: true,
shouldCloseOnEsc: true,
shouldCloseOnClickOutside: true,
isDismissible: true,
/* accessibility */
aria: {
labelledby: null,
describedby: null,
},
};
export default withInstanceId( Modal );