tldraw/components/canvas/bounds/edge-handle.tsx

45 lines
1.1 KiB
TypeScript
Raw Normal View History

import useBoundsEvents from 'hooks/useBoundsEvents'
import styled from 'styles'
import { Edge, Bounds } from 'types'
export default function EdgeHandle({
size,
bounds,
edge,
}: {
size: number
bounds: Bounds
edge: Edge
}) {
const events = useBoundsEvents(edge)
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
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)}
{...events}
/>
)
}
const StyledEdge = styled('rect', {
stroke: 'none',
fill: 'none',
variants: {
edge: {
[Edge.Top]: { cursor: 'ns-resize' },
[Edge.Right]: { cursor: 'ew-resize' },
[Edge.Bottom]: { cursor: 'ns-resize' },
[Edge.Left]: { cursor: 'ew-resize' },
},
},
})