@@ -10,10 +10,9 @@ import Field from 'components/Field/Field.react';
10
10
import Icon from 'components/Icon/Icon.react' ;
11
11
import Popover from 'components/Popover/Popover.react' ;
12
12
import Position from 'lib/Position' ;
13
- import React from 'react' ;
13
+ import React , { useState , useEffect , useRef , useCallback } from 'react' ;
14
14
import PropTypes from 'lib/PropTypes' ;
15
15
import styles from 'components/Modal/Modal.scss' ;
16
- import { useState , useEffect , useRef } from 'react' ;
17
16
18
17
const origin = new Position ( 0 , 0 ) ;
19
18
const buttonColors = {
@@ -39,54 +38,56 @@ const Modal = ({
39
38
progress = false ,
40
39
customFooter,
41
40
textModal = false ,
42
- width : initialWidth = 500 ,
41
+ width,
42
+ initialWidth = width ?? 500 ,
43
43
continueText,
44
44
onContinue,
45
45
showContinue,
46
46
buttonsInCenter = React . Children . count ( children ) === 0 ,
47
47
} ) => {
48
-
49
48
const modalRef = useRef ( null ) ;
50
-
51
49
const [ currentWidth , setCurrentWidth ] = useState ( initialWidth ) ;
52
50
const resizing = useRef ( false ) ;
53
51
54
- const handleMouseDown = ( e ) => {
55
- e . preventDefault ( ) ;
56
- resizing . current = true ;
57
- if ( modalRef . current ) {
58
- modalRef . current . classList . add ( styles . noTransition ) ;
59
- }
60
- } ;
52
+ const handleMouseDown = e => {
53
+ e . preventDefault ( ) ;
54
+ resizing . current = true ;
55
+ if ( modalRef . current ) {
56
+ modalRef . current . classList . add ( styles . noTransition ) ;
57
+ }
58
+ } ;
61
59
62
- const handleMouseMove = ( e ) => {
63
- if ( ! resizing . current || ! modalRef . current ) {
64
- return ;
65
- }
60
+ const handleMouseMove = useCallback (
61
+ e => {
62
+ if ( ! resizing . current || ! modalRef . current ) {
63
+ return ;
64
+ }
66
65
67
- const modalLeft = modalRef . current . getBoundingClientRect ( ) . left ;
68
- const newWidth = e . clientX - modalLeft ;
66
+ const modalLeft = modalRef . current . getBoundingClientRect ( ) . left ;
67
+ const newWidth = e . clientX - modalLeft ;
69
68
70
- const clampedWidth = Math . min ( window . innerWidth , Math . max ( initialWidth , newWidth ) ) ;
71
- setCurrentWidth ( clampedWidth ) ;
72
- } ;
69
+ const clampedWidth = Math . min ( window . innerWidth , Math . max ( initialWidth , newWidth ) ) ;
70
+ setCurrentWidth ( clampedWidth ) ;
71
+ } ,
72
+ [ initialWidth ]
73
+ ) ;
73
74
74
- const handleMouseUp = ( ) => {
75
- resizing . current = false ;
76
- if ( modalRef . current ) {
77
- modalRef . current . classList . remove ( styles . noTransition ) ;
78
- }
79
- } ;
75
+ const handleMouseUp = useCallback ( ( ) => {
76
+ resizing . current = false ;
77
+ if ( modalRef . current ) {
78
+ modalRef . current . classList . remove ( styles . noTransition ) ;
79
+ }
80
+ } , [ ] ) ;
80
81
81
- useEffect ( ( ) => {
82
- document . addEventListener ( 'mousemove' , handleMouseMove ) ;
83
- document . addEventListener ( 'mouseup' , handleMouseUp ) ;
82
+ useEffect ( ( ) => {
83
+ document . addEventListener ( 'mousemove' , handleMouseMove ) ;
84
+ document . addEventListener ( 'mouseup' , handleMouseUp ) ;
84
85
85
- return ( ) => {
86
- document . removeEventListener ( 'mousemove' , handleMouseMove ) ;
87
- document . removeEventListener ( 'mouseup' , handleMouseUp ) ;
88
- } ;
89
- } , [ ] ) ;
86
+ return ( ) => {
87
+ document . removeEventListener ( 'mousemove' , handleMouseMove ) ;
88
+ document . removeEventListener ( 'mouseup' , handleMouseUp ) ;
89
+ } ;
90
+ } , [ handleMouseMove , handleMouseUp ] ) ;
90
91
91
92
if ( children ) {
92
93
children = React . Children . map ( children , c => {
@@ -125,7 +126,11 @@ useEffect(() => {
125
126
126
127
return (
127
128
< Popover fadeIn = { true } fixed = { true } position = { origin } modal = { true } color = "rgba(17,13,17,0.8)" >
128
- < div ref = { modalRef } className = { [ styles . modal , styles [ type ] ] . join ( ' ' ) } style = { { width : currentWidth } } >
129
+ < div
130
+ ref = { modalRef }
131
+ className = { [ styles . modal , styles [ type ] ] . join ( ' ' ) }
132
+ style = { { width : currentWidth } }
133
+ >
129
134
< div className = { styles . header } >
130
135
< div
131
136
style = { {
@@ -178,7 +183,8 @@ Modal.propTypes = {
178
183
progress : PropTypes . bool . describe ( 'Passed to the confirm button.' ) ,
179
184
customFooter : PropTypes . node . describe ( 'used to fill any custom footer use case.' ) ,
180
185
textModal : PropTypes . bool . describe ( 'Used for modals that contain only text to pad the text.' ) ,
181
- width : PropTypes . number . describe ( 'custom width of modal.' ) ,
186
+ width : PropTypes . number . describe ( 'custom width of modal (legacy prop, prefer initialWidth).' ) ,
187
+ initialWidth : PropTypes . number . describe ( 'custom width of modal.' ) ,
182
188
buttonsInCenter : PropTypes . bool . describe (
183
189
'If true, the buttons will appear in the center of the modal, instead of to the right. By default, the buttons appear on the right unless the modal contains no children, in which case they appear in the center.'
184
190
) ,
0 commit comments