Skip to content

Commit 85c9cc7

Browse files
committed
Update Modal.react.js
1 parent d0c4c92 commit 85c9cc7

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

src/components/Modal/Modal.react.js

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import Field from 'components/Field/Field.react';
1010
import Icon from 'components/Icon/Icon.react';
1111
import Popover from 'components/Popover/Popover.react';
1212
import Position from 'lib/Position';
13-
import React from 'react';
13+
import React, { useState, useEffect, useRef, useCallback } from 'react';
1414
import PropTypes from 'lib/PropTypes';
1515
import styles from 'components/Modal/Modal.scss';
16-
import { useState, useEffect, useRef } from 'react';
1716

1817
const origin = new Position(0, 0);
1918
const buttonColors = {
@@ -39,54 +38,56 @@ const Modal = ({
3938
progress = false,
4039
customFooter,
4140
textModal = false,
42-
width: initialWidth = 500,
41+
width,
42+
initialWidth = width ?? 500,
4343
continueText,
4444
onContinue,
4545
showContinue,
4646
buttonsInCenter = React.Children.count(children) === 0,
4747
}) => {
48-
4948
const modalRef = useRef(null);
50-
5149
const [currentWidth, setCurrentWidth] = useState(initialWidth);
5250
const resizing = useRef(false);
5351

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+
};
6159

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+
}
6665

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;
6968

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+
);
7374

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+
}, []);
8081

81-
useEffect(() => {
82-
document.addEventListener('mousemove', handleMouseMove);
83-
document.addEventListener('mouseup', handleMouseUp);
82+
useEffect(() => {
83+
document.addEventListener('mousemove', handleMouseMove);
84+
document.addEventListener('mouseup', handleMouseUp);
8485

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]);
9091

9192
if (children) {
9293
children = React.Children.map(children, c => {
@@ -125,7 +126,11 @@ useEffect(() => {
125126

126127
return (
127128
<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+
>
129134
<div className={styles.header}>
130135
<div
131136
style={{
@@ -178,7 +183,8 @@ Modal.propTypes = {
178183
progress: PropTypes.bool.describe('Passed to the confirm button.'),
179184
customFooter: PropTypes.node.describe('used to fill any custom footer use case.'),
180185
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.'),
182188
buttonsInCenter: PropTypes.bool.describe(
183189
'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.'
184190
),

0 commit comments

Comments
 (0)