1 | import { registerAction, dragAction, pinchAction, wheelAction, scrollAction, moveAction, hoverAction } from '@use-gesture/core/actions';
|
---|
2 | export * from '@use-gesture/core/actions';
|
---|
3 | import React from 'react';
|
---|
4 | import { Controller, parseMergedHandlers } from '@use-gesture/core';
|
---|
5 | export * from '@use-gesture/core/utils';
|
---|
6 | export * from '@use-gesture/core/types';
|
---|
7 |
|
---|
8 | function useRecognizers(handlers, config = {}, gestureKey, nativeHandlers) {
|
---|
9 | const ctrl = React.useMemo(() => new Controller(handlers), []);
|
---|
10 | ctrl.applyHandlers(handlers, nativeHandlers);
|
---|
11 | ctrl.applyConfig(config, gestureKey);
|
---|
12 | React.useEffect(ctrl.effect.bind(ctrl));
|
---|
13 | React.useEffect(() => {
|
---|
14 | return ctrl.clean.bind(ctrl);
|
---|
15 | }, []);
|
---|
16 | if (config.target === undefined) {
|
---|
17 | return ctrl.bind.bind(ctrl);
|
---|
18 | }
|
---|
19 | return undefined;
|
---|
20 | }
|
---|
21 |
|
---|
22 | function useDrag(handler, config) {
|
---|
23 | registerAction(dragAction);
|
---|
24 | return useRecognizers({
|
---|
25 | drag: handler
|
---|
26 | }, config || {}, 'drag');
|
---|
27 | }
|
---|
28 |
|
---|
29 | function usePinch(handler, config) {
|
---|
30 | registerAction(pinchAction);
|
---|
31 | return useRecognizers({
|
---|
32 | pinch: handler
|
---|
33 | }, config || {}, 'pinch');
|
---|
34 | }
|
---|
35 |
|
---|
36 | function useWheel(handler, config) {
|
---|
37 | registerAction(wheelAction);
|
---|
38 | return useRecognizers({
|
---|
39 | wheel: handler
|
---|
40 | }, config || {}, 'wheel');
|
---|
41 | }
|
---|
42 |
|
---|
43 | function useScroll(handler, config) {
|
---|
44 | registerAction(scrollAction);
|
---|
45 | return useRecognizers({
|
---|
46 | scroll: handler
|
---|
47 | }, config || {}, 'scroll');
|
---|
48 | }
|
---|
49 |
|
---|
50 | function useMove(handler, config) {
|
---|
51 | registerAction(moveAction);
|
---|
52 | return useRecognizers({
|
---|
53 | move: handler
|
---|
54 | }, config || {}, 'move');
|
---|
55 | }
|
---|
56 |
|
---|
57 | function useHover(handler, config) {
|
---|
58 | registerAction(hoverAction);
|
---|
59 | return useRecognizers({
|
---|
60 | hover: handler
|
---|
61 | }, config || {}, 'hover');
|
---|
62 | }
|
---|
63 |
|
---|
64 | function createUseGesture(actions) {
|
---|
65 | actions.forEach(registerAction);
|
---|
66 | return function useGesture(_handlers, _config) {
|
---|
67 | const {
|
---|
68 | handlers,
|
---|
69 | nativeHandlers,
|
---|
70 | config
|
---|
71 | } = parseMergedHandlers(_handlers, _config || {});
|
---|
72 | return useRecognizers(handlers, config, undefined, nativeHandlers);
|
---|
73 | };
|
---|
74 | }
|
---|
75 |
|
---|
76 | function useGesture(handlers, config) {
|
---|
77 | const hook = createUseGesture([dragAction, pinchAction, scrollAction, wheelAction, moveAction, hoverAction]);
|
---|
78 | return hook(handlers, config || {});
|
---|
79 | }
|
---|
80 |
|
---|
81 | export { createUseGesture, useDrag, useGesture, useHover, useMove, usePinch, useScroll, useWheel };
|
---|