2021-06-15 11:58:51 +00:00
|
|
|
import useBoundsEvents from 'hooks/useBoundsEvents'
|
2021-05-30 13:39:53 +00:00
|
|
|
import styled from 'styles'
|
|
|
|
import { Edge, Bounds } from 'types'
|
2021-05-22 15:45:24 +00:00
|
|
|
|
|
|
|
export default function EdgeHandle({
|
|
|
|
size,
|
|
|
|
bounds,
|
|
|
|
edge,
|
|
|
|
}: {
|
|
|
|
size: number
|
|
|
|
bounds: Bounds
|
|
|
|
edge: Edge
|
2021-06-21 21:35:28 +00:00
|
|
|
}): JSX.Element {
|
2021-06-15 11:58:51 +00:00
|
|
|
const events = useBoundsEvents(edge)
|
2021-05-22 15:45:24 +00:00
|
|
|
|
|
|
|
const isHorizontal = edge === Edge.Top || edge === Edge.Bottom
|
|
|
|
const isFarEdge = edge === Edge.Right || edge === Edge.Bottom
|
|
|
|
|
2021-06-04 21:21:03 +00:00
|
|
|
const { height, width } = bounds
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
return (
|
|
|
|
<StyledEdge
|
|
|
|
edge={edge}
|
2021-06-04 21:21:03 +00:00
|
|
|
x={isHorizontal ? size / 2 : (isFarEdge ? width + 1 : -1) - size / 2}
|
|
|
|
y={isHorizontal ? (isFarEdge ? height + 1 : -1) - size / 2 : size / 2}
|
|
|
|
width={isHorizontal ? Math.max(0, width + 1 - size) : size}
|
|
|
|
height={isHorizontal ? size : Math.max(0, height + 1 - size)}
|
2021-05-22 15:45:24 +00:00
|
|
|
{...events}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-30 13:39:53 +00:00
|
|
|
const StyledEdge = styled('rect', {
|
|
|
|
stroke: 'none',
|
|
|
|
fill: 'none',
|
2021-05-22 15:45:24 +00:00
|
|
|
variants: {
|
|
|
|
edge: {
|
2021-05-30 13:39:53 +00:00
|
|
|
[Edge.Top]: { cursor: 'ns-resize' },
|
|
|
|
[Edge.Right]: { cursor: 'ew-resize' },
|
|
|
|
[Edge.Bottom]: { cursor: 'ns-resize' },
|
|
|
|
[Edge.Left]: { cursor: 'ew-resize' },
|
2021-05-22 15:45:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|