Skip to content

Commit 572223a

Browse files
committed
feat: vertices now do not appear when they are out of bounds
1 parent dbf54ce commit 572223a

1 file changed

Lines changed: 102 additions & 55 deletions

File tree

components/utils/DraggableVertice.js

Lines changed: 102 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef } from "react";
1+
import React, { useRef, useState, useEffect } from "react";
22

33
// react-draggable npm
44
import Draggable from "react-draggable";
@@ -25,25 +25,68 @@ const CircleVertice = styled.div`
2525

2626
const DraggableVertice = (props) => {
2727

28-
let x;
29-
let y;
28+
const [showVertice, setShowVertice] = useState(true);
3029

31-
// Calculates x coordinates based on percentage or pixels
32-
if (props.x.includes("%")) {
33-
x = parseFloat(props.x) * 280.0 / 100.0;
34-
} else if (props.x.includes("px")) {
35-
x = parseFloat(props.x);
36-
}
30+
const [x, setX] = useState(0);
31+
const [y, setY] = useState(0);
3732

38-
// Calulates y coordinates based on percentage or pixels
39-
if (props.y.includes("%")) {
40-
y = parseFloat(props.y) * 280.0 / 100.0;
41-
} else if (props.y.includes("px")) {
42-
y = parseFloat(props.y);
43-
}
33+
useEffect(() => {
34+
35+
let xValue;
36+
let yValue;
37+
38+
// Calculates x coordinates based on percentage or pixels
39+
// Determines whether to show them or not depending on if it goes out of the border
40+
if (props.x.includes("%")) {
41+
setX(parseFloat(props.x) * 280.0 / 100.0);
42+
xValue = parseFloat(props.x.substring(0, props.x.indexOf("%") + 1));
43+
44+
if (xValue > 100) {
45+
setShowVertice(false);
46+
} else {
47+
setShowVertice(true);
48+
}
49+
50+
} else if (props.x.includes("px")) {
51+
setX(x = parseFloat(props.x));
52+
xValue = parseFloat(props.x.substring(0, props.x.indexOf("px") + 2));
53+
54+
if (xValue > 280) {
55+
setShowVertice(false);
56+
} else {
57+
setShowVertice(true);
58+
}
59+
60+
}
61+
62+
// Calulates y coordinates based on percentage or pixels
63+
if (props.y.includes("%")) {
64+
setY(parseFloat(props.y) * 280.0 / 100.0);
65+
yValue = parseFloat(props.y.substring(0, props.y.indexOf("%") + 1));
66+
67+
if (yValue > 100) {
68+
setShowVertice(false);
69+
} else {
70+
setShowVertice(true);
71+
}
72+
73+
} else if (props.y.includes("px")) {
74+
setY(parseFloat(props.y));
75+
yValue = parseFloat(props.y.substring(0, props.y.indexOf("px") + 2));
76+
77+
if (yValue > 280) {
78+
setShowVertice(false);
79+
} else {
80+
setShowVertice(true);
81+
}
82+
83+
}
84+
}, [props])
85+
86+
4487

4588
// Handles when to show the close button
46-
const show = props.focusNumber === props.number;
89+
const showClose = props.focusNumber === props.number;
4790
const target = useRef(null);
4891

4992
const handleDrag = (e, data) => {
@@ -57,45 +100,49 @@ const DraggableVertice = (props) => {
57100

58101
return(
59102
<>
60-
<Draggable
61-
bounds="parent"
62-
handle=".handle"
63-
position={{x: x, y: y}}
64-
grid={[2.8, 2.8]}
65-
onDrag={(e, data) => {handleDrag(e, data); props.setFocusNumber(-1)}}
66-
>
67-
<CircleVertice
68-
className="handle"
69-
onClick={() => {
70-
if (show === false) {
71-
props.setFocusNumber(props.number);
72-
} else {
73-
props.setFocusNumber(-1);
74-
}
75-
}}
76-
onTouchStart={() => {
77-
if (show === false) {
78-
props.setFocusNumber(props.number);
79-
} else {
80-
props.setFocusNumber(-1);
81-
}
82-
}}
83-
ref={target}
84-
/>
85-
</Draggable>
86-
87-
<Overlay target={target.current}
88-
show={show}
89-
placement={x > 250 ? "left" : "right"}>
90-
<Tooltip>
91-
<FiDelete
92-
size="24px"
93-
id={"deleteButton" + props.number}
94-
onMouseUp={handleDelete}
95-
style={{ cursor: "pointer" }}
96-
/>
97-
</Tooltip>
98-
</Overlay>
103+
{showVertice ?
104+
<>
105+
<Draggable
106+
bounds="parent"
107+
handle=".handle"
108+
position={{x: x, y: y}}
109+
grid={[2.8, 2.8]}
110+
onDrag={(e, data) => {handleDrag(e, data); props.setFocusNumber(-1)}}
111+
>
112+
<CircleVertice
113+
className="handle"
114+
onClick={() => {
115+
if (showClose === false) {
116+
props.setFocusNumber(props.number);
117+
} else {
118+
props.setFocusNumber(-1);
119+
}
120+
}}
121+
onTouchStart={() => {
122+
if (showClose === false) {
123+
props.setFocusNumber(props.number);
124+
} else {
125+
props.setFocusNumber(-1);
126+
}
127+
}}
128+
ref={target}
129+
/>
130+
</Draggable>
131+
132+
<Overlay target={target.current}
133+
show={showClose}
134+
placement={x > 250 ? "left" : "right"}>
135+
<Tooltip>
136+
<FiDelete
137+
size="24px"
138+
id={"deleteButton" + props.number}
139+
onMouseUp={handleDelete}
140+
style={{ cursor: "pointer" }}
141+
/>
142+
</Tooltip>
143+
</Overlay>
144+
</> : null
145+
}
99146
</>
100147
);
101148

0 commit comments

Comments
 (0)