You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

edge-handle.tsx 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import useBoundsEvents from 'hooks/useBoundsEvents'
  2. import styled from 'styles'
  3. import { Edge, Bounds } from 'types'
  4. export default function EdgeHandle({
  5. size,
  6. bounds,
  7. edge,
  8. }: {
  9. size: number
  10. bounds: Bounds
  11. edge: Edge
  12. }): JSX.Element {
  13. const events = useBoundsEvents(edge)
  14. const isHorizontal = edge === Edge.Top || edge === Edge.Bottom
  15. const isFarEdge = edge === Edge.Right || edge === Edge.Bottom
  16. const { height, width } = bounds
  17. return (
  18. <StyledEdge
  19. edge={edge}
  20. x={isHorizontal ? size / 2 : (isFarEdge ? width + 1 : -1) - size / 2}
  21. y={isHorizontal ? (isFarEdge ? height + 1 : -1) - size / 2 : size / 2}
  22. width={isHorizontal ? Math.max(0, width + 1 - size) : size}
  23. height={isHorizontal ? size : Math.max(0, height + 1 - size)}
  24. {...events}
  25. />
  26. )
  27. }
  28. const StyledEdge = styled('rect', {
  29. stroke: 'none',
  30. fill: 'none',
  31. variants: {
  32. edge: {
  33. [Edge.Top]: { cursor: 'ns-resize' },
  34. [Edge.Right]: { cursor: 'ew-resize' },
  35. [Edge.Bottom]: { cursor: 'ns-resize' },
  36. [Edge.Left]: { cursor: 'ew-resize' },
  37. },
  38. },
  39. })