motion-start 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +39 -0
  3. package/package.json +64 -0
  4. package/src/animation/UseAnimatedState.svelte +86 -0
  5. package/src/animation/UseAnimation.svelte +61 -0
  6. package/src/animation/animate.ts +78 -0
  7. package/src/animation/animation-controls.ts +101 -0
  8. package/src/animation/types.ts +83 -0
  9. package/src/animation/use-animated-state.ts +1 -0
  10. package/src/animation/use-animation.ts +74 -0
  11. package/src/animation/utils/default-transitions.ts +70 -0
  12. package/src/animation/utils/easing.ts +55 -0
  13. package/src/animation/utils/is-animatable.ts +42 -0
  14. package/src/animation/utils/is-animation-controls.ts +17 -0
  15. package/src/animation/utils/is-keyframes-target.ts +17 -0
  16. package/src/animation/utils/transitions.ts +218 -0
  17. package/src/animation/utils/variant-resolvers.ts +15 -0
  18. package/src/components/AnimatePresence/AnimatePresence.svelte +180 -0
  19. package/src/components/AnimatePresence/PresenceChild/PresenceChild.svelte +78 -0
  20. package/src/components/AnimatePresence/PresenceChild/index.ts +7 -0
  21. package/src/components/AnimatePresence/PresenceChild/types.ts +10 -0
  22. package/src/components/AnimatePresence/index.ts +46 -0
  23. package/src/components/AnimatePresence/types.ts +79 -0
  24. package/src/components/AnimatePresence/use-presence.ts +90 -0
  25. package/src/components/AnimateSharedLayout/AnimateSharedLayout.svelte +239 -0
  26. package/src/components/AnimateSharedLayout/index.ts +11 -0
  27. package/src/components/AnimateSharedLayout/types.ts +111 -0
  28. package/src/components/AnimateSharedLayout/utils/batcher.ts +96 -0
  29. package/src/components/AnimateSharedLayout/utils/crossfader.ts +260 -0
  30. package/src/components/AnimateSharedLayout/utils/rotate.ts +48 -0
  31. package/src/components/AnimateSharedLayout/utils/stack.ts +160 -0
  32. package/src/components/LazyMotion/LazyMotion.svelte +82 -0
  33. package/src/components/LazyMotion/index.ts +42 -0
  34. package/src/components/LazyMotion/types.ts +58 -0
  35. package/src/components/MotionConfig/MotionConfig.svelte +56 -0
  36. package/src/components/MotionConfig/MotionConfigScaleCorrection.ts +47 -0
  37. package/src/components/MotionConfig/index.ts +20 -0
  38. package/src/components/MotionDiv.svelte +8 -0
  39. package/src/context/DOMcontext.ts +21 -0
  40. package/src/context/LayoutGroupContext.ts +13 -0
  41. package/src/context/LazyContext.ts +18 -0
  42. package/src/context/MotionConfigContext.ts +48 -0
  43. package/src/context/MotionContext/MotionContext.svelte +27 -0
  44. package/src/context/MotionContext/MotionContextProvider.svelte +22 -0
  45. package/src/context/MotionContext/UseCreateMotionContext.svelte +34 -0
  46. package/src/context/MotionContext/create.ts +1 -0
  47. package/src/context/MotionContext/index.ts +14 -0
  48. package/src/context/MotionContext/utils.ts +29 -0
  49. package/src/context/PresenceContext.ts +26 -0
  50. package/src/context/ScaleCorrectionProvider.svelte +27 -0
  51. package/src/context/SharedLayoutContext.ts +29 -0
  52. package/src/events/UseDomEvent.svelte +67 -0
  53. package/src/events/UsePointerEvent.svelte +76 -0
  54. package/src/events/event-info.ts +69 -0
  55. package/src/events/types.ts +15 -0
  56. package/src/events/use-dom-event.ts +48 -0
  57. package/src/events/use-pointer-event.ts +29 -0
  58. package/src/events/utils.ts +25 -0
  59. package/src/gestures/PanSession.ts +298 -0
  60. package/src/gestures/UseFocusGesture.svelte +31 -0
  61. package/src/gestures/UseGestures.svelte +17 -0
  62. package/src/gestures/UseHoverGesture.svelte +40 -0
  63. package/src/gestures/UsePanGesture.svelte +58 -0
  64. package/src/gestures/UseTapGesture.svelte +77 -0
  65. package/src/gestures/drag/UseDrag.svelte +55 -0
  66. package/src/gestures/drag/UseDragControls.svelte +145 -0
  67. package/src/gestures/drag/VisualElementDragControls.ts +632 -0
  68. package/src/gestures/drag/types.ts +307 -0
  69. package/src/gestures/drag/use-drag-controls.ts +148 -0
  70. package/src/gestures/drag/use-drag.ts +15 -0
  71. package/src/gestures/drag/utils/constraints.ts +157 -0
  72. package/src/gestures/drag/utils/lock.ts +69 -0
  73. package/src/gestures/types.ts +257 -0
  74. package/src/gestures/use-focus-gesture.ts +16 -0
  75. package/src/gestures/use-gestures.ts +2 -0
  76. package/src/gestures/use-hover-gesture.ts +10 -0
  77. package/src/gestures/use-pan-gesture.ts +22 -0
  78. package/src/gestures/use-tap-gesture.ts +14 -0
  79. package/src/gestures/utils/event-type.ts +24 -0
  80. package/src/gestures/utils/is-node-or-child.ts +31 -0
  81. package/src/index.ts +104 -0
  82. package/src/motion/Motion.svelte +246 -0
  83. package/src/motion/MotionSSR.svelte +244 -0
  84. package/src/motion/features/AnimationState.svelte +29 -0
  85. package/src/motion/features/Exit.svelte +32 -0
  86. package/src/motion/features/UseFeatures.svelte +39 -0
  87. package/src/motion/features/animations.ts +22 -0
  88. package/src/motion/features/definitions.ts +49 -0
  89. package/src/motion/features/drag.ts +24 -0
  90. package/src/motion/features/gestures.ts +24 -0
  91. package/src/motion/features/layout/Animate.svelte +314 -0
  92. package/src/motion/features/layout/Animate.ts +9 -0
  93. package/src/motion/features/layout/AnimateLayoutContextProvider.svelte +14 -0
  94. package/src/motion/features/layout/Measure.svelte +98 -0
  95. package/src/motion/features/layout/Measure.ts +9 -0
  96. package/src/motion/features/layout/MeasureContextProvider.svelte +32 -0
  97. package/src/motion/features/layout/index.ts +20 -0
  98. package/src/motion/features/layout/types.ts +71 -0
  99. package/src/motion/features/layout/utils.ts +40 -0
  100. package/src/motion/features/types.ts +53 -0
  101. package/src/motion/features/use-features.ts +16 -0
  102. package/src/motion/index.ts +64 -0
  103. package/src/motion/types.ts +278 -0
  104. package/src/motion/utils/UseLayoutId.svelte +18 -0
  105. package/src/motion/utils/UseVisualElement.svelte +104 -0
  106. package/src/motion/utils/UseVisualState.svelte +137 -0
  107. package/src/motion/utils/is-forced-motion-value.ts +23 -0
  108. package/src/motion/utils/make-renderless-component.ts +17 -0
  109. package/src/motion/utils/should-inhert-variant.ts +6 -0
  110. package/src/motion/utils/use-motion-ref.ts +41 -0
  111. package/src/motion/utils/use-visual-element.ts +13 -0
  112. package/src/motion/utils/use-visual-state.ts +24 -0
  113. package/src/motion/utils/valid-prop.ts +80 -0
  114. package/src/render/dom/M.svelte +16 -0
  115. package/src/render/dom/UseRender.svelte +37 -0
  116. package/src/render/dom/create-motion-class.ts +12 -0
  117. package/src/render/dom/create-visual-element.ts +22 -0
  118. package/src/render/dom/featureBundle.ts +22 -0
  119. package/src/render/dom/motion-minimal.ts +22 -0
  120. package/src/render/dom/motion-proxy.ts +107 -0
  121. package/src/render/dom/motion.ts +62 -0
  122. package/src/render/dom/projection/convert-to-relative.ts +40 -0
  123. package/src/render/dom/projection/default-scale-correctors.ts +138 -0
  124. package/src/render/dom/projection/measure.ts +28 -0
  125. package/src/render/dom/projection/relative-set.ts +27 -0
  126. package/src/render/dom/projection/scale-correction.ts +22 -0
  127. package/src/render/dom/projection/types.ts +13 -0
  128. package/src/render/dom/projection/utils.ts +69 -0
  129. package/src/render/dom/svg-visual-element.ts +114 -0
  130. package/src/render/dom/types.ts +32 -0
  131. package/src/render/dom/use-render.ts +11 -0
  132. package/src/render/dom/utils/UseInitialMotionProps.svelte +26 -0
  133. package/src/render/dom/utils/batch-layout.ts +77 -0
  134. package/src/render/dom/utils/camel-to-dash.ts +20 -0
  135. package/src/render/dom/utils/create-config.ts +33 -0
  136. package/src/render/dom/utils/css-variables-conversion.ts +121 -0
  137. package/src/render/dom/utils/filter-props.ts +55 -0
  138. package/src/render/dom/utils/is-css-variable.ts +18 -0
  139. package/src/render/dom/utils/is-svg-component.ts +41 -0
  140. package/src/render/dom/utils/parse-dom-variant.ts +26 -0
  141. package/src/render/dom/utils/unit-conversion.ts +258 -0
  142. package/src/render/dom/utils/use-html-props.ts +2 -0
  143. package/src/render/dom/utils/use-svg-props.ts +1 -0
  144. package/src/render/dom/value-types/animatable-none.ts +24 -0
  145. package/src/render/dom/value-types/defaults.ts +30 -0
  146. package/src/render/dom/value-types/dimensions.ts +27 -0
  147. package/src/render/dom/value-types/find.ts +31 -0
  148. package/src/render/dom/value-types/get-as-type.ts +21 -0
  149. package/src/render/dom/value-types/number.ts +83 -0
  150. package/src/render/dom/value-types/test.ts +17 -0
  151. package/src/render/dom/value-types/type-auto.ts +21 -0
  152. package/src/render/dom/value-types/type-int.ts +23 -0
  153. package/src/render/dom/value-types/types.ts +8 -0
  154. package/src/render/html/UseHTMLProps.svelte +33 -0
  155. package/src/render/html/UseInitialMotionValues.svelte +27 -0
  156. package/src/render/html/UseStyle.svelte +47 -0
  157. package/src/render/html/config-motion.ts +23 -0
  158. package/src/render/html/supported-elements.ts +10 -0
  159. package/src/render/html/types.ts +64 -0
  160. package/src/render/html/use-props.ts +14 -0
  161. package/src/render/html/utils/build-projection-transform.ts +53 -0
  162. package/src/render/html/utils/build-styles.ts +121 -0
  163. package/src/render/html/utils/build-transform.ts +79 -0
  164. package/src/render/html/utils/create-render-state.ts +18 -0
  165. package/src/render/html/utils/render.ts +22 -0
  166. package/src/render/html/utils/scrape-motion-values.ts +26 -0
  167. package/src/render/html/utils/transform.ts +51 -0
  168. package/src/render/html/visual-element.ts +129 -0
  169. package/src/render/index.ts +703 -0
  170. package/src/render/svg/UseSVGProps.svelte +34 -0
  171. package/src/render/svg/config-motion.ts +51 -0
  172. package/src/render/svg/lowercase-elements.ts +35 -0
  173. package/src/render/svg/supported-elements.ts +10 -0
  174. package/src/render/svg/types.ts +51 -0
  175. package/src/render/svg/use-props.ts +14 -0
  176. package/src/render/svg/utils/build-attrs.ts +58 -0
  177. package/src/render/svg/utils/camel-case-attrs.ts +27 -0
  178. package/src/render/svg/utils/create-render-state.ts +17 -0
  179. package/src/render/svg/utils/path.ts +49 -0
  180. package/src/render/svg/utils/render.ts +22 -0
  181. package/src/render/svg/utils/scrape-motion-values.ts +26 -0
  182. package/src/render/svg/utils/transform-origin.ts +30 -0
  183. package/src/render/svg/visual-element.ts +44 -0
  184. package/src/render/types.ts +148 -0
  185. package/src/render/utils/animation-state.ts +375 -0
  186. package/src/render/utils/animation.ts +167 -0
  187. package/src/render/utils/compare-by-depth.ts +18 -0
  188. package/src/render/utils/flat-tree.ts +35 -0
  189. package/src/render/utils/is-draggable.ts +17 -0
  190. package/src/render/utils/lifecycles.ts +172 -0
  191. package/src/render/utils/motion-values.ts +59 -0
  192. package/src/render/utils/projection.ts +38 -0
  193. package/src/render/utils/setters.ts +910 -0
  194. package/src/render/utils/state.ts +113 -0
  195. package/src/render/utils/types.ts +12 -0
  196. package/src/render/utils/variants.ts +76 -0
  197. package/src/types/geometry.ts +91 -0
  198. package/src/types.ts +1088 -0
  199. package/src/utils/UseUnmountEffect.svelte +11 -0
  200. package/src/utils/array.ts +18 -0
  201. package/src/utils/each-axis.ts +15 -0
  202. package/src/utils/errors.ts +22 -0
  203. package/src/utils/fix-process-env.ts +22 -0
  204. package/src/utils/geometry/delta-apply.ts +162 -0
  205. package/src/utils/geometry/delta-calc.ts +89 -0
  206. package/src/utils/geometry/index.ts +83 -0
  207. package/src/utils/is-browser.ts +12 -0
  208. package/src/utils/is-numerical-string.ts +15 -0
  209. package/src/utils/is-ref-object.ts +16 -0
  210. package/src/utils/noop.ts +15 -0
  211. package/src/utils/resolve-value.ts +23 -0
  212. package/src/utils/shallow-compare.ts +23 -0
  213. package/src/utils/subscription-manager.ts +49 -0
  214. package/src/utils/time-conversion.ts +18 -0
  215. package/src/utils/transform.ts +120 -0
  216. package/src/utils/use-constant.ts +23 -0
  217. package/src/utils/use-cycle.ts +78 -0
  218. package/src/utils/use-force-update.ts +7 -0
  219. package/src/utils/use-isomorphic-effect.ts +8 -0
  220. package/src/utils/use-reduced-motion.ts +70 -0
  221. package/src/utils/use-unmount-effect.ts +8 -0
  222. package/src/value/index.ts +409 -0
  223. package/src/value/scroll/use-element-scroll.ts +73 -0
  224. package/src/value/scroll/use-viewport-scroll.ts +81 -0
  225. package/src/value/scroll/utils.ts +76 -0
  226. package/src/value/use-combine-values.ts +53 -0
  227. package/src/value/use-motion-template.ts +57 -0
  228. package/src/value/use-motion-value.ts +27 -0
  229. package/src/value/use-spring.ts +84 -0
  230. package/src/value/use-transform.ts +216 -0
  231. package/src/value/use-velocity.ts +44 -0
  232. package/src/value/utils/is-motion-value.ts +15 -0
  233. package/src/value/utils/resolve-motion-value.ts +29 -0
@@ -0,0 +1,307 @@
1
+ /**
2
+ based on framer-motion@4.1.17,
3
+ Copyright (c) 2018 Framer B.V.
4
+ */
5
+ import type { RefObject } from "react";
6
+ import type { VariantLabels } from "../../motion/types";
7
+ import type { Inertia, TargetAndTransition } from "../../types";
8
+ import type { Axis, BoundingBox2D } from "../../types/geometry";
9
+ import { MotionValue } from "../../value";
10
+ import type { PanInfo } from "../PanSession";
11
+ import { DragControls } from "./use-drag-controls";
12
+ export type DragHandler = (event: MouseEvent | TouchEvent | PointerEvent, info: PanInfo) => void;
13
+ export type DragElastic = boolean | number | Partial<BoundingBox2D>;
14
+ export interface ResolvedConstraints {
15
+ x: Partial<Axis>;
16
+ y: Partial<Axis>;
17
+ }
18
+ export interface ResolvedElastic {
19
+ x: Axis;
20
+ y: Axis;
21
+ }
22
+ /**
23
+ * @public
24
+ */
25
+ export interface DragHandlers {
26
+ /**
27
+ * Callback function that fires when dragging starts.
28
+ *
29
+ * @motion
30
+ *
31
+ * ```jsx
32
+ * <MotionDiv
33
+ * drag
34
+ * onDragStart={
35
+ * (event, info) => console.log(info.point.x, info.point.y)
36
+ * }
37
+ * />
38
+ * ```
39
+ *
40
+ * @public
41
+ */
42
+ onDragStart?(event: MouseEvent | TouchEvent | PointerEvent, info: PanInfo): void;
43
+ /**
44
+ * Callback function that fires when dragging ends.
45
+ *
46
+ * @motion
47
+ *
48
+ * ```jsx
49
+ * <MotionDiv
50
+ * drag
51
+ * onDragEnd={
52
+ * (event, info) => console.log(info.point.x, info.point.y)
53
+ * }
54
+ * />
55
+ * ```
56
+ *
57
+ * @public
58
+ */
59
+ onDragEnd?(event: MouseEvent | TouchEvent | PointerEvent, info: PanInfo): void;
60
+ /**
61
+ * Callback function that fires when the component is dragged.
62
+ *
63
+ * @motion
64
+ *
65
+ * ```jsx
66
+ * <MotionDiv
67
+ * drag
68
+ * onDrag={
69
+ * (event, info) => console.log(info.point.x, info.point.y)
70
+ * }
71
+ * />
72
+ * ```
73
+ *
74
+ * @public
75
+ */
76
+ onDrag?(event: MouseEvent | TouchEvent | PointerEvent, info: PanInfo): void;
77
+ /**
78
+ * Callback function that fires a drag direction is determined.
79
+ *
80
+ * @motion
81
+ *
82
+ * ```jsx
83
+ * <MotionDiv
84
+ * drag
85
+ * dragDirectionLock
86
+ * onDirectionLock={axis => console.log(axis)}
87
+ * />
88
+ * ```
89
+ *
90
+ * @public
91
+ */
92
+ onDirectionLock?(axis: "x" | "y"): void;
93
+ /**
94
+ * Callback function that fires when drag momentum/bounce transition finishes.
95
+ *
96
+ * @motion
97
+ *
98
+ * ```jsx
99
+ * <MotionDiv
100
+ * drag
101
+ * onDragTransitionEnd={() => console.log('Drag transition complete')}
102
+ * />
103
+ * ```
104
+ *
105
+ * @public
106
+ */
107
+ onDragTransitionEnd?(): void;
108
+ }
109
+ /**
110
+ * @public
111
+ */
112
+ export type InertiaOptions = Partial<Omit<Inertia, "velocity" | "type">>;
113
+ /**
114
+ * @public
115
+ */
116
+ export interface DraggableProps extends DragHandlers {
117
+ /**
118
+ * Enable dragging for this element. Set to `false` by default.
119
+ * Set `true` to drag in both directions.
120
+ * Set `"x"` or `"y"` to only drag in a specific direction.
121
+ *
122
+ * @motion
123
+ *
124
+ * ```jsx
125
+ * <MotionDiv drag="x" />
126
+ * ```
127
+ */
128
+ drag?: boolean | "x" | "y";
129
+ /**
130
+ * Properties or variant label to animate to while the drag gesture is recognised.
131
+ *
132
+ * ```jsx
133
+ * <MotionDiv whileDrag={{ scale: 1.2 }} />
134
+ * ```
135
+ */
136
+ whileDrag?: VariantLabels | TargetAndTransition;
137
+ /**
138
+ * If `true`, this will lock dragging to the initially-detected direction. Defaults to `false`.
139
+ *
140
+ * @motion
141
+ *
142
+ * ```jsx
143
+ * <MotionDiv drag dragDirectionLock />
144
+ * ```
145
+ */
146
+ dragDirectionLock?: boolean;
147
+ /**
148
+ * Allows drag gesture propagation to child components. Set to `false` by
149
+ * default.
150
+ *
151
+ * @motion
152
+ *
153
+ * ```jsx
154
+ * <MotionDiv drag="x" dragPropagation />
155
+ * ```
156
+ */
157
+ dragPropagation?: boolean;
158
+ /**
159
+ * Applies constraints on the permitted draggable area.
160
+ *
161
+ * It can accept an object of optional `top`, `left`, `right`, and `bottom` values, measured in pixels.
162
+ * This will define a distance the named edge of the draggable component.
163
+ *
164
+ * Alternatively, it can accept a `ref` to another component created with React's `useRef` hook.
165
+ * This `ref` should be passed both to the draggable component's `dragConstraints` prop, and the `ref`
166
+ * of the component you want to use as constraints.
167
+ *
168
+ * @motion
169
+ *
170
+ * ```jsx
171
+ * // In pixels
172
+ * <MotionDiv
173
+ * drag="x"
174
+ * dragConstraints={{ left: 0, right: 300 }}
175
+ * />
176
+ *
177
+ * // As a ref to another component
178
+ * let constraintsRef;
179
+ *
180
+ * <MotionDiv bind:this={constraintsRef}>
181
+ * <MotionDiv drag dragConstraints={constraintsRef} />
182
+ * </MotionDiv>
183
+ * )
184
+ * }
185
+ * ```
186
+ */
187
+ dragConstraints?: false | Partial<BoundingBox2D> | RefObject<Element>;
188
+ /**
189
+ * The degree of movement allowed outside constraints. 0 = no movement, 1 =
190
+ * full movement.
191
+ *
192
+ * Set to `0.5` by default. Can also be set as `false` to disable movement.
193
+ *
194
+ * By passing an object of `top`/`right`/`bottom`/`left`, individual values can be set
195
+ * per constraint. Any missing values will be set to `0`.
196
+ *
197
+ * @motion
198
+ *
199
+ * ```jsx
200
+ * <MotionDiv
201
+ * drag
202
+ * dragConstraints={{ left: 0, right: 300 }}
203
+ * dragElastic={0.2}
204
+ * />
205
+ * ```
206
+ */
207
+ dragElastic?: DragElastic;
208
+ /**
209
+ * Apply momentum from the pan gesture to the component when dragging
210
+ * finishes. Set to `true` by default.
211
+ *
212
+ * @motion
213
+ *
214
+ * ```jsx
215
+ * <MotionDiv
216
+ * drag
217
+ * dragConstraints={{ left: 0, right: 300 }}
218
+ * dragMomentum={false}
219
+ * />
220
+ * ```
221
+ */
222
+ dragMomentum?: boolean;
223
+ /**
224
+ * Allows you to change dragging inertia parameters.
225
+ * When releasing a draggable Frame, an animation with type `inertia` starts. The animation is based on your dragging velocity. This property allows you to customize it.
226
+ * See {@link https://framer.com/api/animation/#inertia | Inertia} for all properties you can use.
227
+ *
228
+ * @motion
229
+ *
230
+ * ```jsx
231
+ * <MotionDiv
232
+ * drag
233
+ * dragTransition={{ bounceStiffness: 600, bounceDamping: 10 }}
234
+ * />
235
+ * ```
236
+ */
237
+ dragTransition?: InertiaOptions;
238
+ /**
239
+ * Usually, dragging is initiated by pressing down on a component and moving it. For some
240
+ * use-cases, for instance clicking at an arbitrary point on a video scrubber, we
241
+ * might want to initiate dragging from a different component than the draggable one.
242
+ *
243
+ * By creating a `dragControls` using the `useDragControls` hook, we can pass this into
244
+ * the draggable component's `dragControls` prop. It exposes a `start` method
245
+ * that can start dragging from pointer events on other components.
246
+ *
247
+ * @motion
248
+ *
249
+ * ```jsx
250
+ * const dragControls = useDragControls()
251
+ *
252
+ * function startDrag(event) {
253
+ * dragControls.start(event, { snapToCursor: true })
254
+ * }
255
+ *
256
+ * <div onPointerDown={startDrag} />
257
+ * <MotionDiv drag="x" dragControls={dragControls} />
258
+ * ```
259
+ */
260
+ dragControls?: DragControls;
261
+ /**
262
+ * By default, if `drag` is defined on a component then an event listener will be attached
263
+ * to automatically initiate dragging when a user presses down on it.
264
+ *
265
+ * By setting `dragListener` to `false`, this event listener will not be created.
266
+ *
267
+ * @motion
268
+ *
269
+ * ```jsx
270
+ * const dragControls = useDragControls()
271
+ *
272
+ * function startDrag(event) {
273
+ * dragControls.start(event, { snapToCursor: true })
274
+ * }
275
+ *
276
+ * <div onPointerDown={startDrag} />
277
+ * <MotionDiv
278
+ * drag="x"
279
+ * dragControls={dragControls}
280
+ * dragListener={false}
281
+ * />
282
+ * ```
283
+ */
284
+ dragListener?: boolean;
285
+ /**
286
+ * If `dragConstraints` is set to a React ref, this callback will call with the measured drag constraints.
287
+ *
288
+ * @public
289
+ */
290
+ onMeasureDragConstraints?: (constraints: BoundingBox2D) => BoundingBox2D | void;
291
+ /**
292
+ * Usually, dragging uses the layout project engine, and applies transforms to the underlying VisualElement.
293
+ * Passing MotionValues as _dragX and _dragY instead applies drag updates to these motion values.
294
+ * This allows you to manually control how updates from a drag gesture on an element is applied.
295
+ *
296
+ * @public
297
+ */
298
+ _dragX?: MotionValue<number>;
299
+ /**
300
+ * Usually, dragging uses the layout project engine, and applies transforms to the underlying VisualElement.
301
+ * Passing MotionValues as _dragX and _dragY instead applies drag updates to these motion values.
302
+ * This allows you to manually control how updates from a drag gesture on an element is applied.
303
+ *
304
+ * @public
305
+ */
306
+ _dragY?: MotionValue<number>;
307
+ }
@@ -0,0 +1,148 @@
1
+ /**
2
+ based on framer-motion@4.1.17,
3
+ Copyright (c) 2018 Framer B.V.
4
+ */
5
+ import { VisualElementDragControls, type DragControlOptions } from "./VisualElementDragControls";
6
+ /**
7
+ * Can manually trigger a drag gesture on one or more `drag`-enabled `motion` components.
8
+ *
9
+ * @motion
10
+ *
11
+ * ```jsx
12
+ * const dragControls = useDragControls()
13
+ *
14
+ * function startDrag(event) {
15
+ * dragControls.start(event, { snapToCursor: true })
16
+ * }
17
+ *
18
+ * <div onPointerDown={startDrag} />
19
+ * <MotionDiv drag="x" dragControls={dragControls} />
20
+ * ```
21
+ *
22
+ * @public
23
+ */
24
+ export class DragControls {
25
+ private componentControls = new Set<VisualElementDragControls>();
26
+ /**
27
+ * Subscribe a component's internal `VisualElementDragControls` to the user-facing API.
28
+ *
29
+ * @internal
30
+ */
31
+ subscribe(controls: VisualElementDragControls): () => void {
32
+ this.componentControls.add(controls);
33
+
34
+ return () => this.componentControls.delete(controls);
35
+ }
36
+ /**
37
+ * Start a drag gesture on every `motion` component that has this set of drag controls
38
+ * passed into it via the `dragControls` prop.
39
+ *
40
+ * ```jsx
41
+ * dragControls.start(e, {
42
+ * snapToCursor: true
43
+ * })
44
+ * ```
45
+ *
46
+ * @param event - PointerEvent
47
+ * @param options - Options
48
+ *
49
+ * @public
50
+ */
51
+ start(event: React.PointerEvent | PointerEvent, options?: DragControlOptions): void {
52
+ this.componentControls.forEach((controls) => {
53
+ controls.start((event as React.PointerEvent).nativeEvent || event),
54
+ options
55
+ })
56
+ }
57
+
58
+ updateConstraints(flush?: boolean): void {
59
+ this.componentControls.forEach(function (controls) {
60
+ controls.prepareBoundingBox();
61
+ controls.resolveDragConstraints();
62
+ });
63
+ }
64
+ }
65
+
66
+ const createDragControls = () => new DragControls();
67
+
68
+ /**
69
+ * Usually, dragging is initiated by pressing down on a `motion` component with a `drag` prop
70
+ * and moving it. For some use-cases, for instance clicking at an arbitrary point on a video scrubber, we
71
+ * might want to initiate that dragging from a different component than the draggable one.
72
+ *
73
+ * By creating a `dragControls` using the `useDragControls` hook, we can pass this into
74
+ * the draggable component's `dragControls` prop. It exposes a `start` method
75
+ * that can start dragging from pointer events on other components.
76
+ *
77
+ * @motion
78
+ *
79
+ * ```jsx
80
+ * const dragControls = useDragControls()
81
+ *
82
+ * function startDrag(event) {
83
+ * dragControls.start(event, { snapToCursor: true })
84
+ * }
85
+ *
86
+ * return (
87
+ * <>
88
+ * <div onPointerDown={startDrag} />
89
+ * <MotionDiv drag="x" dragControls={dragControls} />
90
+ * </>
91
+ * )
92
+ * ```
93
+ *
94
+ * @public
95
+ */
96
+
97
+ /**
98
+ based on framer-motion@4.0.3,
99
+ Copyright (c) 2018 Framer B.V.
100
+ */
101
+
102
+
103
+ /**
104
+ * Usually, dragging is initiated by pressing down on a `motion` component with a `drag` prop
105
+ * and moving it. For some use-cases, for instance clicking at an arbitrary point on a video scrubber, we
106
+ * might want to initiate that dragging from a different component than the draggable one.
107
+ *
108
+ * By creating a `dragControls` using the `useDragControls` hook, we can pass this into
109
+ * the draggable component's `dragControls` prop. It exposes a `start` method
110
+ * that can start dragging from pointer events on other components.
111
+ *
112
+ * @library
113
+ *
114
+ * ```jsx
115
+ * const dragControls = useDragControls()
116
+ *
117
+ * function startDrag(event) {
118
+ * dragControls.start(event, { snapToCursor: true })
119
+ * }
120
+ *
121
+ * return (
122
+ * <>
123
+ * <Frame onTapStart={startDrag} />
124
+ * <Frame drag="x" dragControls={dragControls} />
125
+ * </>
126
+ * )
127
+ * ```
128
+ *
129
+ * @motion
130
+ *
131
+ * ```jsx
132
+ * const dragControls = useDragControls()
133
+ *
134
+ * function startDrag(event) {
135
+ * dragControls.start(event, { snapToCursor: true })
136
+ * }
137
+ *
138
+ * return (
139
+ * <>
140
+ * <div onPointerDown={startDrag} />
141
+ * <MotionDiv drag="x" dragControls={dragControls} />
142
+ * </>
143
+ * )
144
+ * ```
145
+ *
146
+ * @public
147
+ */
148
+ export const useDragControls = createDragControls
@@ -0,0 +1,15 @@
1
+ /**
2
+ based on framer-motion@4.1.17,
3
+ Copyright (c) 2018 Framer B.V.
4
+ */
5
+ import type { FeatureProps } from "../../motion/features/types";
6
+ /**
7
+ * A hook that allows an element to be dragged.
8
+ *
9
+ * @internal
10
+ */
11
+ export type useDrag = (props: FeatureProps) => void;
12
+
13
+
14
+ export { default as UseDrag } from './UseDrag.svelte';
15
+
@@ -0,0 +1,157 @@
1
+ /**
2
+ based on framer-motion@4.1.17,
3
+ Copyright (c) 2018 Framer B.V.
4
+ */
5
+ import type { Axis, AxisBox2D, BoundingBox2D, Point2D } from "../../../types/geometry";
6
+ import type { DragElastic, ResolvedConstraints } from "../types";
7
+
8
+ /**
9
+ * Calculate the relative progress of one constraints box relative to another.
10
+ * Imagine a page scroll bar. At the top, this would return 0, at the bottom, 1.
11
+ * Anywhere in-between, a value between 0 and 1.
12
+ *
13
+ * This also handles flipped constraints, for instance a draggable container within
14
+ * a smaller viewport like a scrollable view.
15
+ */
16
+ export type calcProgressWithinConstraints = (layoutBox: AxisBox2D, constraintsBox: AxisBox2D) => Point2D;
17
+
18
+
19
+
20
+ /**
21
+ based on framer-motion@4.1.17,
22
+ Copyright (c) 2018 Framer B.V.
23
+ */
24
+ import { mix } from 'popmotion';
25
+ import { __read } from 'tslib';
26
+
27
+ /**
28
+ * Apply constraints to a point. These constraints are both physical along an
29
+ * axis, and an elastic factor that determines how much to constrain the point
30
+ * by if it does lie outside the defined parameters.
31
+ */
32
+ function applyConstraints(point: number, { min, max }: Partial<Axis>, elastic?: Axis) {
33
+ if (min !== undefined && point < min) {
34
+ // If we have a min point defined, and this is outside of that, constrain
35
+ point = elastic ? mix(min, point, elastic.min) : Math.max(point, min);
36
+ }
37
+ else if (max !== undefined && point > max) {
38
+ // If we have a max point defined, and this is outside of that, constrain
39
+ point = elastic ? mix(max, point, elastic.max) : Math.min(point, max);
40
+ }
41
+ return point;
42
+ }
43
+ /**
44
+ * Calculates a min projection point based on a pointer, pointer progress
45
+ * within the drag target, and constraints.
46
+ *
47
+ * For instance if an element was 100px width, we were dragging from 0.25
48
+ * along this axis, the pointer is at 200px, and there were no constraints,
49
+ * we would calculate a min projection point of 175px.
50
+ */
51
+ function calcConstrainedMinPoint(point: number, length: number, progress: number, constraints?: Partial<Axis>, elastic?: Axis) {
52
+ // Calculate a min point for this axis and apply it to the current pointer
53
+ var min = point - length * progress;
54
+ return constraints ? applyConstraints(min, constraints, elastic) : min;
55
+ }
56
+ /**
57
+ * Calculate constraints in terms of the viewport when defined relatively to the
58
+ * measured axis. This is measured from the nearest edge, so a max constraint of 200
59
+ * on an axis with a max value of 300 would return a constraint of 500 - axis length
60
+ */
61
+ function calcRelativeAxisConstraints(axis: Axis, min?: number, max?: number) {
62
+ return {
63
+ min: min !== undefined ? axis.min + min : undefined,
64
+ max: max !== undefined
65
+ ? axis.max + max - (axis.max - axis.min)
66
+ : undefined,
67
+ } as Partial<Axis>;
68
+ }
69
+ /**
70
+ * Calculate constraints in terms of the viewport when
71
+ * defined relatively to the measured bounding box.
72
+ */
73
+ function calcRelativeConstraints(layoutBox: AxisBox2D, { top, left, bottom, right }: Partial<BoundingBox2D>) {
74
+ return {
75
+ x: calcRelativeAxisConstraints(layoutBox.x, left, right),
76
+ y: calcRelativeAxisConstraints(layoutBox.y, top, bottom),
77
+ } as ResolvedConstraints;
78
+ }
79
+ /**
80
+ * Calculate viewport constraints when defined as another viewport-relative axis
81
+ */
82
+ function calcViewportAxisConstraints(layoutAxis: Axis, constraintsAxis: Axis) {
83
+ var _a;
84
+ var min = constraintsAxis.min - layoutAxis.min;
85
+ var max = constraintsAxis.max - layoutAxis.max;
86
+ // If the constraints axis is actually smaller than the layout axis then we can
87
+ // flip the constraints
88
+ if (constraintsAxis.max - constraintsAxis.min <
89
+ layoutAxis.max - layoutAxis.min) {
90
+ _a = __read([max, min], 2), min = _a[0], max = _a[1];
91
+ }
92
+ return {
93
+ min: layoutAxis.min + min,
94
+ max: layoutAxis.min + max,
95
+ };
96
+ }
97
+ /**
98
+ * Calculate viewport constraints when defined as another viewport-relative box
99
+ */
100
+ function calcViewportConstraints(layoutBox: AxisBox2D, constraintsBox: AxisBox2D) {
101
+ return {
102
+ x: calcViewportAxisConstraints(layoutBox.x, constraintsBox.x),
103
+ y: calcViewportAxisConstraints(layoutBox.y, constraintsBox.y),
104
+ };
105
+ }
106
+ /**
107
+ * Calculate the an axis position based on two axes and a progress value.
108
+ */
109
+ function calcPositionFromProgress(axis: Axis, constraints: Axis, progress: number) {
110
+ var axisLength = axis.max - axis.min;
111
+ var min = mix(constraints.min, constraints.max - axisLength, progress);
112
+ return { min: min, max: min + axisLength } as Axis;
113
+ }
114
+ /**
115
+ * Rebase the calculated viewport constraints relative to the layout.min point.
116
+ */
117
+ function rebaseAxisConstraints(layout: Axis, constraints: Partial<Axis>) {
118
+ var relativeConstraints: Partial<Axis> = {};
119
+ if (constraints.min !== undefined) {
120
+ relativeConstraints.min = constraints.min - layout.min;
121
+ }
122
+ if (constraints.max !== undefined) {
123
+ relativeConstraints.max = constraints.max - layout.min;
124
+ }
125
+ return relativeConstraints;
126
+ }
127
+ const defaultElastic = 0.35;
128
+ /**
129
+ * Accepts a dragElastic prop and returns resolved elastic values for each axis.
130
+ */
131
+ function resolveDragElastic(dragElastic: DragElastic) {
132
+ if (dragElastic === false) {
133
+ dragElastic = 0;
134
+ }
135
+ else if (dragElastic === true) {
136
+ dragElastic = defaultElastic;
137
+ }
138
+ return {
139
+ x: resolveAxisElastic(dragElastic, "left", "right"),
140
+ y: resolveAxisElastic(dragElastic, "top", "bottom"),
141
+ } as AxisBox2D;
142
+ }
143
+ function resolveAxisElastic(dragElastic: DragElastic, minLabel: string, maxLabel: string) {
144
+ return {
145
+ min: resolvePointElastic(dragElastic, minLabel),
146
+ max: resolvePointElastic(dragElastic, maxLabel),
147
+ } as Axis;
148
+ }
149
+ function resolvePointElastic(dragElastic: DragElastic, label: string): number {
150
+ var _a;
151
+ return typeof dragElastic === "number"
152
+ ? dragElastic
153
+ : (_a = (dragElastic as any)[label]) !== null && _a !== void 0 ? _a : 0;
154
+ }
155
+
156
+ export { applyConstraints, calcConstrainedMinPoint, calcPositionFromProgress, calcRelativeAxisConstraints, calcRelativeConstraints, calcViewportAxisConstraints, calcViewportConstraints, defaultElastic, rebaseAxisConstraints, resolveAxisElastic, resolveDragElastic, resolvePointElastic };
157
+
@@ -0,0 +1,69 @@
1
+ /**
2
+ based on framer-motion@4.1.17,
3
+ Copyright (c) 2018 Framer B.V.
4
+ */
5
+ export type Lock = (() => void) | false;
6
+
7
+
8
+ /**
9
+ based on framer-motion@4.1.17,
10
+ Copyright (c) 2018 Framer B.V.
11
+ */
12
+
13
+ function createLock(name: string) {
14
+ var lock: string | null = null;
15
+ return function (): Lock {
16
+ var openLock = function () {
17
+ lock = null;
18
+ };
19
+
20
+ if (lock === null) {
21
+ lock = name;
22
+ return openLock;
23
+ }
24
+ return false;
25
+ };
26
+ }
27
+ var globalHorizontalLock = createLock("dragHorizontal");
28
+ var globalVerticalLock = createLock("dragVertical");
29
+ function getGlobalLock(drag: boolean | "x" | "y" | "lockDirection") {
30
+ var lock: Lock = false;
31
+ if (drag === "y") {
32
+
33
+ lock = globalVerticalLock();
34
+ }
35
+ else if (drag === "x") {
36
+
37
+ lock = globalHorizontalLock();
38
+ }
39
+ else {
40
+ var openHorizontal_1 = globalHorizontalLock();
41
+ var openVertical_1 = globalVerticalLock();
42
+ if (openHorizontal_1 && openVertical_1) {
43
+ lock = function () {
44
+ openHorizontal_1();
45
+ openVertical_1();
46
+ };
47
+ }
48
+ else {
49
+ // Release the locks because we don't use them
50
+ if (openHorizontal_1)
51
+ openHorizontal_1();
52
+ if (openVertical_1)
53
+ openVertical_1();
54
+ }
55
+ }
56
+ return lock;
57
+ }
58
+ function isDragActive() {
59
+ // Check the gesture lock - if we get it, it means no drag gesture is active
60
+ // and we can safely fire the tap gesture.
61
+ var openGestureLock = getGlobalLock(true);
62
+ if (!openGestureLock)
63
+ return true;
64
+ openGestureLock();
65
+ return false;
66
+ }
67
+
68
+ export { createLock, getGlobalLock, isDragActive };
69
+