2020-03-27 09:02:11 +00:00
|
|
|
import React, { ReactNode, useState, useEffect, forwardRef, Ref } from "react";
|
2020-02-21 12:16:49 +00:00
|
|
|
import styled, { StyledComponent } from "styled-components";
|
2020-02-18 19:56:58 +00:00
|
|
|
import { useDrag } from "react-use-gesture";
|
|
|
|
|
import { Spring } from "react-spring/renderprops";
|
|
|
|
|
|
2020-03-20 11:17:30 +00:00
|
|
|
const ResizeWrapper = styled.div<{ pevents: boolean }>`
|
2020-02-18 19:56:58 +00:00
|
|
|
display: block;
|
2020-03-20 11:17:30 +00:00
|
|
|
& {
|
|
|
|
|
* {
|
|
|
|
|
pointer-events: ${props => !props.pevents && "none"};
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-18 19:56:58 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const getSnappedValues = (
|
|
|
|
|
x: number,
|
|
|
|
|
y: number,
|
|
|
|
|
snapGrid: { x: number; y: number },
|
|
|
|
|
) => {
|
|
|
|
|
return {
|
|
|
|
|
x: Math.round(x / snapGrid.x) * snapGrid.x,
|
|
|
|
|
y: Math.round(y / snapGrid.y) * snapGrid.y,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ResizableHandleProps = {
|
|
|
|
|
dragCallback: (x: number, y: number) => void;
|
2020-11-03 13:05:40 +00:00
|
|
|
component: StyledComponent<"div", Record<string, unknown>>;
|
|
|
|
|
onStart: () => void;
|
|
|
|
|
onStop: () => void;
|
2020-02-18 19:56:58 +00:00
|
|
|
snapGrid: {
|
|
|
|
|
x: number;
|
|
|
|
|
y: number;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ResizableHandle = (props: ResizableHandleProps) => {
|
|
|
|
|
const bind = useDrag(
|
|
|
|
|
({ first, last, dragging, movement: [mx, my], memo }) => {
|
|
|
|
|
const snapped = getSnappedValues(mx, my, props.snapGrid);
|
|
|
|
|
if (dragging && memo && (snapped.x !== memo.x || snapped.y !== memo.y)) {
|
|
|
|
|
props.dragCallback(snapped.x, snapped.y);
|
|
|
|
|
}
|
|
|
|
|
if (first) {
|
|
|
|
|
props.onStart();
|
|
|
|
|
}
|
|
|
|
|
if (last) {
|
|
|
|
|
props.onStop();
|
|
|
|
|
}
|
|
|
|
|
return snapped;
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-02-21 12:16:49 +00:00
|
|
|
|
|
|
|
|
return <props.component {...bind()} />;
|
2020-02-18 19:56:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ResizableProps = {
|
|
|
|
|
handles: {
|
2020-11-03 13:05:40 +00:00
|
|
|
left: StyledComponent<"div", Record<string, unknown>>;
|
|
|
|
|
top: StyledComponent<"div", Record<string, unknown>>;
|
|
|
|
|
bottom: StyledComponent<"div", Record<string, unknown>>;
|
|
|
|
|
right: StyledComponent<"div", Record<string, unknown>>;
|
|
|
|
|
bottomRight: StyledComponent<"div", Record<string, unknown>>;
|
|
|
|
|
topLeft: StyledComponent<"div", Record<string, unknown>>;
|
|
|
|
|
topRight: StyledComponent<"div", Record<string, unknown>>;
|
|
|
|
|
bottomLeft: StyledComponent<"div", Record<string, unknown>>;
|
2020-02-18 19:56:58 +00:00
|
|
|
};
|
|
|
|
|
componentWidth: number;
|
|
|
|
|
componentHeight: number;
|
|
|
|
|
children: ReactNode;
|
2020-11-03 13:05:40 +00:00
|
|
|
onStart: () => void;
|
|
|
|
|
onStop: (
|
|
|
|
|
size: { width: number; height: number },
|
|
|
|
|
position: { x: number; y: number },
|
|
|
|
|
) => void;
|
2020-02-18 19:56:58 +00:00
|
|
|
snapGrid: { x: number; y: number };
|
|
|
|
|
enable: boolean;
|
2020-11-03 13:05:40 +00:00
|
|
|
isColliding: (
|
|
|
|
|
size: { width: number; height: number },
|
|
|
|
|
position: { x: number; y: number },
|
|
|
|
|
) => boolean;
|
2020-03-27 09:02:11 +00:00
|
|
|
className?: string;
|
2020-02-18 19:56:58 +00:00
|
|
|
};
|
|
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
export const Resizable = forwardRef(
|
|
|
|
|
(props: ResizableProps, ref: Ref<HTMLDivElement>) => {
|
|
|
|
|
const [pointerEvents, togglePointerEvents] = useState(true);
|
|
|
|
|
const [newDimensions, set] = useState({
|
2020-02-18 19:56:58 +00:00
|
|
|
width: props.componentWidth,
|
|
|
|
|
height: props.componentHeight,
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
2020-03-27 09:02:11 +00:00
|
|
|
reset: false,
|
2020-02-18 19:56:58 +00:00
|
|
|
});
|
|
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const setNewDimensions = (rect: {
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
x: number;
|
|
|
|
|
y: number;
|
|
|
|
|
}) => {
|
|
|
|
|
const { width, height, x, y } = rect;
|
|
|
|
|
const isColliding = props.isColliding({ width, height }, { x, y });
|
|
|
|
|
if (!isColliding) {
|
|
|
|
|
set({ ...rect, reset: false });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
set({
|
|
|
|
|
width: props.componentWidth,
|
|
|
|
|
height: props.componentHeight,
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
reset: true,
|
|
|
|
|
});
|
|
|
|
|
}, [props.componentHeight, props.componentWidth]);
|
|
|
|
|
|
|
|
|
|
const handles = [
|
|
|
|
|
{
|
|
|
|
|
dragCallback: (x: number) => {
|
|
|
|
|
setNewDimensions({
|
|
|
|
|
width: props.componentWidth - x,
|
|
|
|
|
height: newDimensions.height,
|
|
|
|
|
x,
|
|
|
|
|
y: newDimensions.y,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
component: props.handles.left,
|
2020-02-18 19:56:58 +00:00
|
|
|
},
|
2020-03-27 09:02:11 +00:00
|
|
|
{
|
|
|
|
|
dragCallback: (x: number) => {
|
|
|
|
|
setNewDimensions({
|
|
|
|
|
width: props.componentWidth + x,
|
|
|
|
|
height: newDimensions.height,
|
|
|
|
|
x: newDimensions.x,
|
|
|
|
|
y: newDimensions.y,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
component: props.handles.right,
|
2020-02-18 19:56:58 +00:00
|
|
|
},
|
2020-03-27 09:02:11 +00:00
|
|
|
{
|
|
|
|
|
dragCallback: (x: number, y: number) => {
|
|
|
|
|
setNewDimensions({
|
|
|
|
|
width: newDimensions.width,
|
|
|
|
|
height: props.componentHeight - y,
|
|
|
|
|
y: y,
|
|
|
|
|
x: newDimensions.x,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
component: props.handles.top,
|
2020-02-18 19:56:58 +00:00
|
|
|
},
|
2020-03-27 09:02:11 +00:00
|
|
|
{
|
|
|
|
|
dragCallback: (x: number, y: number) => {
|
|
|
|
|
setNewDimensions({
|
|
|
|
|
width: newDimensions.width,
|
|
|
|
|
height: props.componentHeight + y,
|
|
|
|
|
x: newDimensions.x,
|
|
|
|
|
y: newDimensions.y,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
component: props.handles.bottom,
|
2020-02-19 10:00:03 +00:00
|
|
|
},
|
2020-03-27 09:02:11 +00:00
|
|
|
{
|
|
|
|
|
dragCallback: (x: number, y: number) => {
|
|
|
|
|
setNewDimensions({
|
|
|
|
|
width: props.componentWidth + x,
|
|
|
|
|
height: props.componentHeight + y,
|
|
|
|
|
x: newDimensions.x,
|
|
|
|
|
y: newDimensions.y,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
component: props.handles.bottomRight,
|
2020-02-19 10:00:03 +00:00
|
|
|
},
|
2020-03-27 09:02:11 +00:00
|
|
|
{
|
|
|
|
|
dragCallback: (x: number, y: number) => {
|
|
|
|
|
setNewDimensions({
|
|
|
|
|
width: props.componentWidth - x,
|
|
|
|
|
height: props.componentHeight + y,
|
|
|
|
|
x,
|
|
|
|
|
y: newDimensions.y,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
component: props.handles.bottomLeft,
|
2020-02-19 10:00:03 +00:00
|
|
|
},
|
2020-02-18 19:56:58 +00:00
|
|
|
{
|
2020-03-27 09:02:11 +00:00
|
|
|
dragCallback: (x: number, y: number) => {
|
|
|
|
|
setNewDimensions({
|
|
|
|
|
width: props.componentWidth + x,
|
|
|
|
|
height: props.componentHeight - y,
|
|
|
|
|
x: newDimensions.x,
|
|
|
|
|
y: y,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
component: props.handles.topRight,
|
2020-02-18 19:56:58 +00:00
|
|
|
},
|
|
|
|
|
{
|
2020-03-27 09:02:11 +00:00
|
|
|
dragCallback: (x: number, y: number) => {
|
|
|
|
|
setNewDimensions({
|
|
|
|
|
width: props.componentWidth - x,
|
|
|
|
|
height: props.componentHeight - y,
|
|
|
|
|
x: x,
|
|
|
|
|
y: y,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
component: props.handles.topLeft,
|
2020-02-18 19:56:58 +00:00
|
|
|
},
|
2020-03-27 09:02:11 +00:00
|
|
|
];
|
2020-02-18 19:56:58 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const onResizeStop = () => {
|
|
|
|
|
togglePointerEvents(true);
|
|
|
|
|
props.onStop(
|
|
|
|
|
{
|
|
|
|
|
width: newDimensions.width,
|
|
|
|
|
height: newDimensions.height,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
x: newDimensions.x,
|
|
|
|
|
y: newDimensions.y,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const renderHandles = handles.map((handle, index) => (
|
|
|
|
|
<ResizableHandle
|
|
|
|
|
{...handle}
|
|
|
|
|
key={index}
|
|
|
|
|
onStart={() => {
|
|
|
|
|
togglePointerEvents(false);
|
|
|
|
|
props.onStart();
|
|
|
|
|
}}
|
|
|
|
|
onStop={onResizeStop}
|
|
|
|
|
snapGrid={props.snapGrid}
|
|
|
|
|
/>
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Spring
|
|
|
|
|
from={{
|
|
|
|
|
width: props.componentWidth,
|
|
|
|
|
height: props.componentHeight,
|
|
|
|
|
}}
|
|
|
|
|
to={{
|
|
|
|
|
width: newDimensions.width,
|
|
|
|
|
height: newDimensions.height,
|
|
|
|
|
transform: `translate3d(${newDimensions.x}px,${newDimensions.y}px,0)`,
|
|
|
|
|
}}
|
|
|
|
|
config={{
|
|
|
|
|
clamp: true,
|
|
|
|
|
friction: 0,
|
|
|
|
|
tension: 999,
|
|
|
|
|
}}
|
|
|
|
|
immediate={newDimensions.reset ? true : false}
|
|
|
|
|
>
|
|
|
|
|
{_props => (
|
|
|
|
|
<ResizeWrapper
|
|
|
|
|
ref={ref}
|
|
|
|
|
style={_props}
|
|
|
|
|
className={props.className}
|
|
|
|
|
pevents={pointerEvents}
|
|
|
|
|
>
|
|
|
|
|
{props.children}
|
|
|
|
|
{props.enable && renderHandles}
|
|
|
|
|
</ResizeWrapper>
|
|
|
|
|
)}
|
|
|
|
|
</Spring>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-02-18 19:56:58 +00:00
|
|
|
|
|
|
|
|
export default Resizable;
|