MCPcopy Create free account
hub / github.com/FormidableLabs/react-swipeable / getHandlers

Function getHandlers

src/index.ts:88–323  ·  view source on GitHub ↗
(
  set: Setter,
  handlerProps: { trackMouse: boolean | undefined }
)

Source from the content-addressed store, hash-verified

86}
87
88function getHandlers(
89 set: Setter,
90 handlerProps: { trackMouse: boolean | undefined }
91): [
92 {
93 ref: (element: HTMLElement | null) => void;
94 onMouseDown?: (event: React.MouseEvent) => void;
95 },
96 AttachTouch
97] {
98 const onStart = (event: HandledEvents) => {
99 const isTouch = "touches" in event;
100 // if more than a single touch don't track, for now...
101 if (isTouch && event.touches.length > 1) return;
102
103 set((state, props) => {
104 // setup mouse listeners on document to track swipe since swipe can leave container
105 if (props.trackMouse && !isTouch) {
106 document.addEventListener(mouseMove, onMove);
107 document.addEventListener(mouseUp, onUp);
108 }
109 const { clientX, clientY } = isTouch ? event.touches[0] : event;
110 const xy = rotateXYByAngle([clientX, clientY], props.rotationAngle);
111
112 props.onTouchStartOrOnMouseDown &&
113 props.onTouchStartOrOnMouseDown({ event });
114
115 return {
116 ...state,
117 ...initialState,
118 initial: xy.slice() as Vector2,
119 xy,
120 start: event.timeStamp || 0,
121 };
122 });
123 };
124
125 const onMove = (event: HandledEvents) => {
126 set((state, props) => {
127 const isTouch = "touches" in event;
128 // Discount a swipe if additional touches are present after
129 // a swipe has started.
130 if (isTouch && event.touches.length > 1) {
131 return state;
132 }
133
134 // if swipe has exceeded duration stop tracking
135 if (event.timeStamp - state.start > props.swipeDuration) {
136 return state.swiping ? { ...state, swiping: false } : state;
137 }
138
139 const { clientX, clientY } = isTouch ? event.touches[0] : event;
140 const [x, y] = rotateXYByAngle([clientX, clientY], props.rotationAngle);
141 const deltaX = x - state.xy[0];
142 const deltaY = y - state.xy[1];
143 const absX = Math.abs(deltaX);
144 const absY = Math.abs(deltaY);
145 const time = (event.timeStamp || 0) - state.start;

Callers 1

useSwipeableFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…