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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import useHandleEvents from "hooks/useBoundsHandleEvents"
  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. }) {
  13. const events = useHandleEvents(edge)
  14. const isHorizontal = edge === Edge.Top || edge === Edge.Bottom
  15. const isFarEdge = edge === Edge.Right || edge === Edge.Bottom
  16. return (
  17. <StyledEdge
  18. edge={edge}
  19. x={isHorizontal ? size / 2 : (isFarEdge ? bounds.width : 0) - size / 2}
  20. y={isHorizontal ? (isFarEdge ? bounds.height : 0) - size / 2 : size / 2}
  21. width={isHorizontal ? Math.max(0, bounds.width - size) : size}
  22. height={isHorizontal ? size : Math.max(0, bounds.height - size)}
  23. {...events}
  24. />
  25. )
  26. }
  27. const StyledEdge = styled("rect", {
  28. stroke: "none",
  29. fill: "none",
  30. variants: {
  31. edge: {
  32. [Edge.Top]: { cursor: "ns-resize" },
  33. [Edge.Right]: { cursor: "ew-resize" },
  34. [Edge.Bottom]: { cursor: "ns-resize" },
  35. [Edge.Left]: { cursor: "ew-resize" },
  36. },
  37. },
  38. })