@@ -69,43 +69,42 @@ export default class ModalPortal extends Component {
69
69
}
70
70
71
71
componentDidMount ( ) {
72
- // Focus needs to be set when mounting and already open
73
72
if ( this . props . isOpen ) {
74
- this . setFocusAfterRender ( true ) ;
75
73
this . open ( ) ;
76
74
}
77
75
}
78
76
79
- componentWillReceiveProps ( newProps ) {
77
+ componentDidUpdate ( prevProps , prevState ) {
80
78
if ( process . env . NODE_ENV !== "production" ) {
81
- if ( newProps . bodyOpenClassName !== this . props . bodyOpenClassName ) {
79
+ if ( prevProps . bodyOpenClassName !== this . props . bodyOpenClassName ) {
82
80
// eslint-disable-next-line no-console
83
81
console . warn (
84
82
'React-Modal: "bodyOpenClassName" prop has been modified. ' +
85
83
"This may cause unexpected behavior when multiple modals are open."
86
84
) ;
87
85
}
88
- if ( newProps . htmlOpenClassName !== this . props . htmlOpenClassName ) {
86
+ if ( prevProps . htmlOpenClassName !== this . props . htmlOpenClassName ) {
89
87
// eslint-disable-next-line no-console
90
88
console . warn (
91
89
'React-Modal: "htmlOpenClassName" prop has been modified. ' +
92
90
"This may cause unexpected behavior when multiple modals are open."
93
91
) ;
94
92
}
95
93
}
96
- // Focus only needs to be set once when the modal is being opened
97
- if ( ! this . props . isOpen && newProps . isOpen ) {
98
- this . setFocusAfterRender ( true ) ;
94
+
95
+ if ( this . props . isOpen && ! prevProps . isOpen ) {
99
96
this . open ( ) ;
100
- } else if ( this . props . isOpen && ! newProps . isOpen ) {
97
+ } else if ( ! this . props . isOpen && prevProps . isOpen ) {
101
98
this . close ( ) ;
102
99
}
103
- }
104
100
105
- componentDidUpdate ( ) {
106
- if ( this . focusAfterRender ) {
101
+ // Focus only needs to be set once when the modal is being opened
102
+ if (
103
+ this . props . shouldFocusAfterRender &&
104
+ this . state . isOpen &&
105
+ ! prevState . isOpen
106
+ ) {
107
107
this . focusContent ( ) ;
108
- this . setFocusAfterRender ( false ) ;
109
108
}
110
109
}
111
110
@@ -114,10 +113,6 @@ export default class ModalPortal extends Component {
114
113
clearTimeout ( this . closeTimer ) ;
115
114
}
116
115
117
- setFocusAfterRender = focus => {
118
- this . focusAfterRender = this . props . shouldFocusAfterRender && focus ;
119
- } ;
120
-
121
116
setOverlayRef = overlay => {
122
117
this . overlay = overlay ;
123
118
this . props . overlayRef && this . props . overlayRef ( overlay ) ;
0 commit comments