UNPKG

framer-motion

Version:

A simple and powerful JavaScript animation library

49 lines (46 loc) • 1.64 kB
import { Feature, PanSession, frame, addPointerEvent } from 'motion-dom'; import { noop } from 'motion-utils'; const asyncHandler = (handler) => (event, info) => { if (handler) { frame.update(() => handler(event, info), false, true); } }; class PanGesture extends Feature { constructor() { super(...arguments); this.removePointerDownListener = noop; } onPointerDown(pointerDownEvent) { this.session = new PanSession(pointerDownEvent, this.createPanHandlers(), { transformPagePoint: this.node.getTransformPagePoint(), // Fixes https://github.com/motiondivision/motion/issues/2270 contextWindow: this.node.current?.ownerDocument.defaultView, }); } createPanHandlers() { const { onPanSessionStart, onPanStart, onPan, onPanEnd } = this.node.getProps(); return { onSessionStart: asyncHandler(onPanSessionStart), onStart: asyncHandler(onPanStart), onMove: asyncHandler(onPan), onEnd: (event, info) => { delete this.session; if (onPanEnd) { frame.postRender(() => onPanEnd(event, info)); } }, }; } mount() { this.removePointerDownListener = addPointerEvent(this.node.current, "pointerdown", (event) => this.onPointerDown(event)); } update() { this.session?.updateHandlers(this.createPanHandlers()); } unmount() { this.removePointerDownListener(); this.session?.end(); } } export { PanGesture }; //# sourceMappingURL=index.mjs.map