useGestureScale.ts 937 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { useEffect, useState } from 'react';
  2. import { usePinch } from 'react-use-gesture';
  3. const useGestureScale = (
  4. domTarget: RefObject<HTMLDivElement> | null,
  5. ): [number] => {
  6. const [zoom, set] = useState(0);
  7. const bind = usePinch(
  8. ({ offset: [d] }) => {
  9. set(d / 200);
  10. },
  11. {
  12. domTarget,
  13. eventOptions: { passive: false },
  14. distanceBounds: { min: -100, max: 150 },
  15. },
  16. );
  17. const notAllowOriginalEvent = (e: Event) => e.preventDefault();
  18. useEffect(() => {
  19. document.addEventListener('gesturestart', notAllowOriginalEvent);
  20. document.addEventListener('gesturechange', notAllowOriginalEvent);
  21. return () => {
  22. document.removeEventListener('gesturestart', notAllowOriginalEvent);
  23. document.removeEventListener('gesturechange', notAllowOriginalEvent);
  24. };
  25. }, []);
  26. useEffect(() => {
  27. bind();
  28. }, [bind]);
  29. return [zoom];
  30. };
  31. export default useGestureScale;