-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResizeHandle.jsx
105 lines (99 loc) · 2.31 KB
/
ResizeHandle.jsx
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
import * as React from "react";
import { Box } from "@mui/material";
import PropTypes from "prop-types";
import { getCursor } from "./utils";
/**
* Resize handle for a given location on a rectangle (corner or side defined by a compass direction).
*/
export default function ResizeHandle({ onResize, direction, rotateAngle }) {
// get cursor type for direction
const cursor = getCursor(rotateAngle, direction);
// callback for resize
const handleResize = React.useCallback(
event => {
onResize(event, cursor, direction);
},
[onResize, cursor, direction]
);
// get the resize handle position styles
const getOffset = direction => {
const offset = "-5px";
const center = "calc(50% - 5px)";
switch (direction) {
case "n":
return {
left: "50%",
marginLeft: offset,
top: offset
};
case "ne":
return {
right: offset,
top: offset
};
case "e":
return {
right: offset,
top: center
};
case "se":
return {
bottom: offset,
right: offset
};
case "s":
return {
bottom: offset,
left: center
};
case "sw":
return {
bottom: offset,
left: offset
};
case "w":
return {
left: offset,
top: center
};
case "nw":
return {
left: offset,
top: offset
};
default:
return {};
}
};
return (
<Box
sx={{
background: theme => theme.palette.primary.main,
cursor: `${cursor}-resize`,
height: "10px",
outline: theme => `2px solid ${theme.palette.background.paper}`,
position: "absolute",
width: "10px",
...getOffset(direction)
}}
onMouseDown={handleResize}
onDragStart={e => e.preventDefault()}
onDrag={e => e.preventDefault()}
/>
);
}
ResizeHandle.propTypes = {
/**
* Compass direction for the resize handle
*/
direction: PropTypes.oneOf(["n", "ne", "e", "se", "s", "sw", "w", "nw"]),
/**
* Callback for resize
*/
onResize: PropTypes.func,
/**
* Rotation angle of the canvas
* (used to determine the cursor type)
*/
rotateAngle: PropTypes.number
};