Blame view

天文台pc/tianwentai-ui/node_modules/framer-motion/dist/es/gestures/drag/use-drag-controls.mjs 2.83 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  import { useConstant } from '../../utils/use-constant.mjs';
  
  /**
   * Can manually trigger a drag gesture on one or more `drag`-enabled `motion` components.
   *
   * ```jsx
   * const dragControls = useDragControls()
   *
   * function startDrag(event) {
   *   dragControls.start(event, { snapToCursor: true })
   * }
   *
   * return (
   *   <>
   *     <div onPointerDown={startDrag} />
   *     <motion.div drag="x" dragControls={dragControls} />
   *   </>
   * )
   * ```
   *
   * @public
   */
  class DragControls {
      constructor() {
          this.componentControls = new Set();
      }
      /**
       * Subscribe a component's internal `VisualElementDragControls` to the user-facing API.
       *
       * @internal
       */
      subscribe(controls) {
          this.componentControls.add(controls);
          return () => this.componentControls.delete(controls);
      }
      /**
       * Start a drag gesture on every `motion` component that has this set of drag controls
       * passed into it via the `dragControls` prop.
       *
       * ```jsx
       * dragControls.start(e, {
       *   snapToCursor: true
       * })
       * ```
       *
       * @param event - PointerEvent
       * @param options - Options
       *
       * @public
       */
      start(event, options) {
          this.componentControls.forEach((controls) => {
              controls.start(event.nativeEvent || event, options);
          });
      }
      /**
       * Cancels a drag gesture.
       *
       * ```jsx
       * dragControls.cancel()
       * ```
       *
       * @public
       */
      cancel() {
          this.componentControls.forEach((controls) => {
              controls.cancel();
          });
      }
      /**
       * Stops a drag gesture.
       *
       * ```jsx
       * dragControls.stop()
       * ```
       *
       * @public
       */
      stop() {
          this.componentControls.forEach((controls) => {
              controls.stop();
          });
      }
  }
  const createDragControls = () => new DragControls();
  /**
   * Usually, dragging is initiated by pressing down on a `motion` component with a `drag` prop
   * and moving it. For some use-cases, for instance clicking at an arbitrary point on a video scrubber, we
   * might want to initiate that dragging from a different component than the draggable one.
   *
   * By creating a `dragControls` using the `useDragControls` hook, we can pass this into
   * the draggable component's `dragControls` prop. It exposes a `start` method
   * that can start dragging from pointer events on other components.
   *
   * ```jsx
   * const dragControls = useDragControls()
   *
   * function startDrag(event) {
   *   dragControls.start(event, { snapToCursor: true })
   * }
   *
   * return (
   *   <>
   *     <div onPointerDown={startDrag} />
   *     <motion.div drag="x" dragControls={dragControls} />
   *   </>
   * )
   * ```
   *
   * @public
   */
  function useDragControls() {
      return useConstant(createDragControls);
  }
  
  export { DragControls, useDragControls };
  //# sourceMappingURL=use-drag-controls.mjs.map