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.

useShapeEvents.ts 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { MutableRefObject, useCallback } from 'react'
  2. import state from 'state'
  3. import inputs from 'state/inputs'
  4. export default function useShapeEvents(
  5. id: string,
  6. rGroup: MutableRefObject<SVGElement>
  7. ) {
  8. const handlePointerDown = useCallback(
  9. (e: React.PointerEvent) => {
  10. e.stopPropagation()
  11. rGroup.current.setPointerCapture(e.pointerId)
  12. state.send('POINTED_SHAPE', inputs.pointerDown(e, id))
  13. },
  14. [id]
  15. )
  16. const handlePointerUp = useCallback(
  17. (e: React.PointerEvent) => {
  18. e.stopPropagation()
  19. rGroup.current.releasePointerCapture(e.pointerId)
  20. state.send('STOPPED_POINTING', inputs.pointerUp(e))
  21. },
  22. [id]
  23. )
  24. const handlePointerEnter = useCallback(
  25. (e: React.PointerEvent) => {
  26. state.send('HOVERED_SHAPE', inputs.pointerEnter(e, id))
  27. },
  28. [id]
  29. )
  30. const handlePointerMove = useCallback(
  31. (e: React.PointerEvent) => {
  32. state.send('MOVED_OVER_SHAPE', inputs.pointerEnter(e, id))
  33. },
  34. [id]
  35. )
  36. const handlePointerLeave = useCallback(
  37. () => state.send('UNHOVERED_SHAPE', { target: id }),
  38. [id]
  39. )
  40. return {
  41. onPointerDown: handlePointerDown,
  42. onPointerUp: handlePointerUp,
  43. onPointerEnter: handlePointerEnter,
  44. onPointerMove: handlePointerMove,
  45. onPointerLeave: handlePointerLeave,
  46. }
  47. }