motion-dom 14.3.1 → 14.4.0
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.
- package/dist/cjs/index.js +6669 -5559
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/animation/animate/element.mjs +16 -1
- package/dist/es/animation/animate/element.mjs.map +1 -1
- package/dist/es/animation/waapi/transforms/TransformAnimation.mjs +5 -1
- package/dist/es/animation/waapi/transforms/TransformAnimation.mjs.map +1 -1
- package/dist/es/events/add-pointer-event.mjs +9 -0
- package/dist/es/events/add-pointer-event.mjs.map +1 -0
- package/dist/es/events/event-info.mjs +14 -0
- package/dist/es/events/event-info.mjs.map +1 -0
- package/dist/es/gestures/drag/VisualElementDragControls.mjs +606 -0
- package/dist/es/gestures/drag/VisualElementDragControls.mjs.map +1 -0
- package/dist/es/gestures/drag/index.mjs +82 -0
- package/dist/es/gestures/drag/index.mjs.map +1 -0
- package/dist/es/gestures/drag/utils/constraints.mjs +129 -0
- package/dist/es/gestures/drag/utils/constraints.mjs.map +1 -0
- package/dist/es/gestures/pan/PanSession.mjs +269 -0
- package/dist/es/gestures/pan/PanSession.mjs.map +1 -0
- package/dist/es/index.mjs +6 -0
- package/dist/es/index.mjs.map +1 -1
- package/dist/es/layout/LayoutAnimationBuilder.mjs +21 -1
- package/dist/es/layout/LayoutAnimationBuilder.mjs.map +1 -1
- package/dist/es/utils/distance.mjs +10 -0
- package/dist/es/utils/distance.mjs.map +1 -0
- package/dist/index.d.ts +238 -3
- package/dist/motion-dom.dev.js +6687 -5577
- package/dist/motion-dom.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import { invariant } from 'motion-utils';
|
|
2
|
+
import { animateMotionValue } from '../../animation/interfaces/motion-value.mjs';
|
|
3
|
+
import { getVariableValue } from '../../animation/utils/css-variables-conversion.mjs';
|
|
4
|
+
import { isCSSVariableToken } from '../../animation/utils/is-css-variable.mjs';
|
|
5
|
+
import { addDomEvent } from '../../events/add-dom-event.mjs';
|
|
6
|
+
import { addPointerEvent } from '../../events/add-pointer-event.mjs';
|
|
7
|
+
import { frame } from '../../frameloop/frame.mjs';
|
|
8
|
+
import { convertBoxToBoundingBox, convertBoundingBoxToBox } from '../../projection/geometry/conversion.mjs';
|
|
9
|
+
import { calcLength } from '../../projection/geometry/delta-calc.mjs';
|
|
10
|
+
import { createBox } from '../../projection/geometry/models.mjs';
|
|
11
|
+
import { eachAxis } from '../../projection/utils/each-axis.mjs';
|
|
12
|
+
import { measurePageBox } from '../../projection/utils/measure.mjs';
|
|
13
|
+
import { resize } from '../../resize/index.mjs';
|
|
14
|
+
import { mixNumber } from '../../utils/mix/number.mjs';
|
|
15
|
+
import { px, percent } from '../../value/types/numbers/units.mjs';
|
|
16
|
+
import { addValueToWillChange } from '../../value/will-change/add-will-change.mjs';
|
|
17
|
+
import { PanSession } from '../pan/PanSession.mjs';
|
|
18
|
+
import { isElementTextInput } from '../press/utils/is-keyboard-accessible.mjs';
|
|
19
|
+
import { setDragLock } from './state/set-active.mjs';
|
|
20
|
+
import { applyConstraints, calcRelativeConstraints, resolveDragElastic, calcViewportConstraints, defaultElastic, rebaseAxisConstraints, calcOrigin } from './utils/constraints.mjs';
|
|
21
|
+
|
|
22
|
+
const elementDragControls = new WeakMap();
|
|
23
|
+
class VisualElementDragControls {
|
|
24
|
+
constructor(visualElement, readProps = () => visualElement.getProps()) {
|
|
25
|
+
this.openDragLock = null;
|
|
26
|
+
this.isDragging = false;
|
|
27
|
+
this.currentDirection = null;
|
|
28
|
+
this.originPoint = { x: 0, y: 0 };
|
|
29
|
+
/**
|
|
30
|
+
* The permitted boundaries of travel, in pixels.
|
|
31
|
+
*/
|
|
32
|
+
this.constraints = false;
|
|
33
|
+
this.hasMutatedConstraints = false;
|
|
34
|
+
/**
|
|
35
|
+
* The per-axis resolved elastic values.
|
|
36
|
+
*/
|
|
37
|
+
this.elastic = createBox();
|
|
38
|
+
/**
|
|
39
|
+
* The latest pointer event. Used as fallback when the `cancel` and `stop` functions are called without arguments.
|
|
40
|
+
*/
|
|
41
|
+
this.latestPointerEvent = null;
|
|
42
|
+
/**
|
|
43
|
+
* The latest pan info. Used as fallback when the `cancel` and `stop` functions are called without arguments.
|
|
44
|
+
*/
|
|
45
|
+
this.latestPanInfo = null;
|
|
46
|
+
this.visualElement = visualElement;
|
|
47
|
+
this.readProps = readProps;
|
|
48
|
+
}
|
|
49
|
+
start(originEvent, { snapToCursor = false, distanceThreshold } = {}) {
|
|
50
|
+
/**
|
|
51
|
+
* Don't start dragging if this component is exiting
|
|
52
|
+
*/
|
|
53
|
+
const { presenceContext } = this.visualElement;
|
|
54
|
+
if (presenceContext && presenceContext.isPresent === false)
|
|
55
|
+
return;
|
|
56
|
+
const onSessionStart = (event) => {
|
|
57
|
+
if (snapToCursor)
|
|
58
|
+
this.snapToCursor(event);
|
|
59
|
+
this.stopAnimation();
|
|
60
|
+
};
|
|
61
|
+
const onStart = (event, info) => {
|
|
62
|
+
// Attempt to grab the global drag gesture lock - maybe make this part of PanSession
|
|
63
|
+
const { drag, dragPropagation, onDragStart } = this.getProps();
|
|
64
|
+
if (drag && !dragPropagation) {
|
|
65
|
+
if (this.openDragLock)
|
|
66
|
+
this.openDragLock();
|
|
67
|
+
this.openDragLock = setDragLock(drag);
|
|
68
|
+
// If we don 't have the lock, don't start dragging
|
|
69
|
+
if (!this.openDragLock)
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
this.latestPointerEvent = event;
|
|
73
|
+
this.latestPanInfo = info;
|
|
74
|
+
this.isDragging = true;
|
|
75
|
+
this.currentDirection = null;
|
|
76
|
+
this.resolveConstraints();
|
|
77
|
+
if (this.visualElement.projection) {
|
|
78
|
+
this.visualElement.projection.isAnimationBlocked = true;
|
|
79
|
+
this.visualElement.projection.target = undefined;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Record gesture origin and pointer offset
|
|
83
|
+
*/
|
|
84
|
+
eachAxis((axis) => {
|
|
85
|
+
let current = this.getAxisMotionValue(axis).get() || 0;
|
|
86
|
+
/**
|
|
87
|
+
* If the MotionValue is a CSS variable, like "var(--y)",
|
|
88
|
+
* resolve it so it can be converted to px below.
|
|
89
|
+
*/
|
|
90
|
+
if (isCSSVariableToken(current)) {
|
|
91
|
+
current =
|
|
92
|
+
getVariableValue(current, this.visualElement.current) || 0;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* If the MotionValue is a px or percentage value convert to px
|
|
96
|
+
*/
|
|
97
|
+
if (px.test(current)) {
|
|
98
|
+
current = parseFloat(current);
|
|
99
|
+
}
|
|
100
|
+
else if (percent.test(current)) {
|
|
101
|
+
const { projection } = this.visualElement;
|
|
102
|
+
if (projection && projection.layout) {
|
|
103
|
+
const measuredAxis = projection.layout.layoutBox[axis];
|
|
104
|
+
if (measuredAxis) {
|
|
105
|
+
const length = calcLength(measuredAxis);
|
|
106
|
+
current = length * (parseFloat(current) / 100);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
this.originPoint[axis] = current;
|
|
111
|
+
});
|
|
112
|
+
// Fire onDragStart event
|
|
113
|
+
if (onDragStart) {
|
|
114
|
+
frame.update(() => onDragStart(event, info), false, true);
|
|
115
|
+
}
|
|
116
|
+
addValueToWillChange(this.visualElement, "transform");
|
|
117
|
+
const { animationState } = this.visualElement;
|
|
118
|
+
animationState && animationState.setActive("whileDrag", true);
|
|
119
|
+
};
|
|
120
|
+
const onMove = (event, info) => {
|
|
121
|
+
this.latestPointerEvent = event;
|
|
122
|
+
this.latestPanInfo = info;
|
|
123
|
+
const { dragPropagation, dragDirectionLock, onDirectionLock, onDrag, } = this.getProps();
|
|
124
|
+
// If we didn't successfully receive the gesture lock, early return.
|
|
125
|
+
if (!dragPropagation && !this.openDragLock)
|
|
126
|
+
return;
|
|
127
|
+
const { offset } = info;
|
|
128
|
+
// Attempt to detect drag direction if directionLock is true
|
|
129
|
+
if (dragDirectionLock && this.currentDirection === null) {
|
|
130
|
+
this.currentDirection = getCurrentDirection(offset);
|
|
131
|
+
/**
|
|
132
|
+
* Wait for more movement until a direction is set. Once
|
|
133
|
+
* it is, apply this move too: it can be the last before
|
|
134
|
+
* release when the browser coalesces a fast gesture.
|
|
135
|
+
*/
|
|
136
|
+
if (this.currentDirection === null)
|
|
137
|
+
return;
|
|
138
|
+
onDirectionLock && onDirectionLock(this.currentDirection);
|
|
139
|
+
}
|
|
140
|
+
// Update each point with the latest position
|
|
141
|
+
this.updateAxis("x", info.point, offset);
|
|
142
|
+
this.updateAxis("y", info.point, offset);
|
|
143
|
+
/**
|
|
144
|
+
* Ideally we would leave the renderer to fire naturally at the end of
|
|
145
|
+
* this frame but if the element is about to change layout as the result
|
|
146
|
+
* of a re-render we want to ensure the browser can read the latest
|
|
147
|
+
* bounding box to ensure the pointer and element don't fall out of sync.
|
|
148
|
+
*/
|
|
149
|
+
this.visualElement.render();
|
|
150
|
+
/**
|
|
151
|
+
* This must fire after the render call as it might trigger a state
|
|
152
|
+
* change which itself might trigger a layout update.
|
|
153
|
+
*/
|
|
154
|
+
if (onDrag) {
|
|
155
|
+
frame.update(() => onDrag(event, info), false, true);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const onSessionEnd = (event, info) => {
|
|
159
|
+
this.latestPointerEvent = event;
|
|
160
|
+
this.latestPanInfo = info;
|
|
161
|
+
this.stop(event, info);
|
|
162
|
+
this.latestPointerEvent = null;
|
|
163
|
+
this.latestPanInfo = null;
|
|
164
|
+
};
|
|
165
|
+
const resumeAnimation = () => {
|
|
166
|
+
const { dragSnapToOrigin: snap } = this.getProps();
|
|
167
|
+
if (snap || this.constraints) {
|
|
168
|
+
this.startAnimation({ x: 0, y: 0 });
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
const { dragSnapToOrigin } = this.getProps();
|
|
172
|
+
this.panSession = new PanSession(originEvent, {
|
|
173
|
+
onSessionStart,
|
|
174
|
+
onStart,
|
|
175
|
+
onMove,
|
|
176
|
+
onSessionEnd,
|
|
177
|
+
resumeAnimation,
|
|
178
|
+
}, {
|
|
179
|
+
transformPagePoint: this.visualElement.getTransformPagePoint(),
|
|
180
|
+
dragSnapToOrigin,
|
|
181
|
+
distanceThreshold,
|
|
182
|
+
contextWindow: this.visualElement.current?.ownerDocument.defaultView,
|
|
183
|
+
element: this.visualElement.current,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
stop(event, panInfo) {
|
|
187
|
+
const finalEvent = event || this.latestPointerEvent;
|
|
188
|
+
const finalPanInfo = panInfo || this.latestPanInfo;
|
|
189
|
+
const isDragging = this.isDragging;
|
|
190
|
+
this.cancel();
|
|
191
|
+
if (!isDragging || !finalPanInfo || !finalEvent)
|
|
192
|
+
return;
|
|
193
|
+
const { velocity } = finalPanInfo;
|
|
194
|
+
this.startAnimation(velocity);
|
|
195
|
+
const { onDragEnd } = this.getProps();
|
|
196
|
+
if (onDragEnd) {
|
|
197
|
+
frame.postRender(() => onDragEnd(finalEvent, finalPanInfo));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
cancel() {
|
|
201
|
+
this.isDragging = false;
|
|
202
|
+
const { projection, animationState } = this.visualElement;
|
|
203
|
+
if (projection) {
|
|
204
|
+
projection.isAnimationBlocked = false;
|
|
205
|
+
}
|
|
206
|
+
this.endPanSession();
|
|
207
|
+
const { dragPropagation } = this.getProps();
|
|
208
|
+
if (!dragPropagation && this.openDragLock) {
|
|
209
|
+
this.openDragLock();
|
|
210
|
+
this.openDragLock = null;
|
|
211
|
+
}
|
|
212
|
+
animationState && animationState.setActive("whileDrag", false);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Clean up the pan session without modifying other drag state.
|
|
216
|
+
* This is used during unmount to ensure event listeners are removed
|
|
217
|
+
* without affecting projection animations or drag locks.
|
|
218
|
+
*/
|
|
219
|
+
endPanSession() {
|
|
220
|
+
this.panSession && this.panSession.end();
|
|
221
|
+
this.panSession = undefined;
|
|
222
|
+
}
|
|
223
|
+
updateAxis(axis, _point, offset) {
|
|
224
|
+
const { drag } = this.getProps();
|
|
225
|
+
// If we're not dragging this axis, do an early return.
|
|
226
|
+
if (!offset || !shouldDrag(axis, drag, this.currentDirection))
|
|
227
|
+
return;
|
|
228
|
+
const axisValue = this.getAxisMotionValue(axis);
|
|
229
|
+
let next = this.originPoint[axis] + offset[axis];
|
|
230
|
+
// Apply constraints
|
|
231
|
+
if (this.constraints && this.constraints[axis]) {
|
|
232
|
+
next = applyConstraints(next, this.constraints[axis], this.elastic[axis]);
|
|
233
|
+
}
|
|
234
|
+
axisValue.set(next);
|
|
235
|
+
}
|
|
236
|
+
resolveConstraints() {
|
|
237
|
+
const { dragConstraints, dragElastic } = this.getProps();
|
|
238
|
+
const layout = this.visualElement.projection &&
|
|
239
|
+
!this.visualElement.projection.layout
|
|
240
|
+
? this.visualElement.projection.measure(false)
|
|
241
|
+
: this.visualElement.projection?.layout;
|
|
242
|
+
const prevConstraints = this.constraints;
|
|
243
|
+
if (isElementConstraints(dragConstraints)) {
|
|
244
|
+
if (!this.constraints) {
|
|
245
|
+
this.constraints = this.resolveRefConstraints();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
if (dragConstraints && layout) {
|
|
250
|
+
this.constraints = calcRelativeConstraints(layout.layoutBox, dragConstraints);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
this.constraints = false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
this.elastic = resolveDragElastic(dragElastic);
|
|
257
|
+
/**
|
|
258
|
+
* If we're outputting to external MotionValues, we want to rebase the measured constraints
|
|
259
|
+
* from viewport-relative to component-relative. This only applies to relative (non-ref)
|
|
260
|
+
* constraints, as ref-based constraints from calcViewportConstraints are already in the
|
|
261
|
+
* correct coordinate space for the motion value transform offset.
|
|
262
|
+
*/
|
|
263
|
+
if (prevConstraints !== this.constraints &&
|
|
264
|
+
!isElementConstraints(dragConstraints) &&
|
|
265
|
+
layout &&
|
|
266
|
+
this.constraints &&
|
|
267
|
+
!this.hasMutatedConstraints) {
|
|
268
|
+
eachAxis((axis) => {
|
|
269
|
+
if (this.constraints !== false &&
|
|
270
|
+
this.getAxisMotionValue(axis)) {
|
|
271
|
+
this.constraints[axis] = rebaseAxisConstraints(layout.layoutBox[axis], this.constraints[axis]);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
resolveRefConstraints() {
|
|
277
|
+
const { dragConstraints: constraints, onMeasureDragConstraints } = this.getProps();
|
|
278
|
+
if (!isElementConstraints(constraints))
|
|
279
|
+
return false;
|
|
280
|
+
const constraintsElement = resolveConstraintsElement(constraints);
|
|
281
|
+
invariant(!!constraintsElement, "If `dragConstraints` is set as a React ref, that ref must be passed to another component's `ref` prop.", "drag-constraints-ref");
|
|
282
|
+
const { projection } = this.visualElement;
|
|
283
|
+
// TODO
|
|
284
|
+
if (!projection || !projection.layout)
|
|
285
|
+
return false;
|
|
286
|
+
/**
|
|
287
|
+
* Refresh the root scroll offset so the constraint's viewport box
|
|
288
|
+
* translates to correct page coordinates. The scroll captured at
|
|
289
|
+
* drag mount can be stale if the document was scrolled afterwards —
|
|
290
|
+
* e.g. via the browser restoring scroll on refresh, or an ancestor
|
|
291
|
+
* layout effect running after this element's mount (#2829).
|
|
292
|
+
*
|
|
293
|
+
* Clear the cached scroll first so `updateScroll` bypasses its
|
|
294
|
+
* per-animationId cache and re-reads the live value.
|
|
295
|
+
*/
|
|
296
|
+
if (projection.root) {
|
|
297
|
+
projection.root.scroll = undefined;
|
|
298
|
+
projection.root.updateScroll();
|
|
299
|
+
}
|
|
300
|
+
const constraintsBox = measurePageBox(constraintsElement, projection.root, this.visualElement.getTransformPagePoint());
|
|
301
|
+
let measuredConstraints = calcViewportConstraints(projection.layout.layoutBox, constraintsBox);
|
|
302
|
+
/**
|
|
303
|
+
* If there's an onMeasureDragConstraints listener we call it and
|
|
304
|
+
* if different constraints are returned, set constraints to that
|
|
305
|
+
*/
|
|
306
|
+
if (onMeasureDragConstraints) {
|
|
307
|
+
const userConstraints = onMeasureDragConstraints(convertBoxToBoundingBox(measuredConstraints));
|
|
308
|
+
this.hasMutatedConstraints = !!userConstraints;
|
|
309
|
+
if (userConstraints) {
|
|
310
|
+
measuredConstraints = convertBoundingBoxToBox(userConstraints);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return measuredConstraints;
|
|
314
|
+
}
|
|
315
|
+
startAnimation(velocity) {
|
|
316
|
+
const { drag, dragMomentum, dragElastic, dragTransition, dragSnapToOrigin, onDragTransitionEnd, } = this.getProps();
|
|
317
|
+
const constraints = this.constraints || {};
|
|
318
|
+
const momentumAnimations = eachAxis((axis) => {
|
|
319
|
+
if (!shouldDrag(axis, drag, this.currentDirection)) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
let transition = (constraints && constraints[axis]) || {};
|
|
323
|
+
if (dragSnapToOrigin === true ||
|
|
324
|
+
dragSnapToOrigin === axis)
|
|
325
|
+
transition = { min: 0, max: 0 };
|
|
326
|
+
/**
|
|
327
|
+
* Overdamp the boundary spring if `dragElastic` is disabled. There's still a frame
|
|
328
|
+
* of spring animations so we should look into adding a disable spring option to `inertia`.
|
|
329
|
+
* We could do something here where we affect the `bounceStiffness` and `bounceDamping`
|
|
330
|
+
* using the value of `dragElastic`.
|
|
331
|
+
*/
|
|
332
|
+
const bounceStiffness = dragElastic ? 200 : 1000000;
|
|
333
|
+
const bounceDamping = dragElastic ? 40 : 10000000;
|
|
334
|
+
const inertia = {
|
|
335
|
+
type: "inertia",
|
|
336
|
+
velocity: dragMomentum ? velocity[axis] : 0,
|
|
337
|
+
bounceStiffness,
|
|
338
|
+
bounceDamping,
|
|
339
|
+
timeConstant: 750,
|
|
340
|
+
restDelta: 1,
|
|
341
|
+
restSpeed: 10,
|
|
342
|
+
...dragTransition,
|
|
343
|
+
...transition,
|
|
344
|
+
};
|
|
345
|
+
// If we're not animating on an externally-provided `MotionValue` we can use the
|
|
346
|
+
// component's animation controls which will handle interactions with whileHover (etc),
|
|
347
|
+
// otherwise we just have to animate the `MotionValue` itself.
|
|
348
|
+
return this.startAxisValueAnimation(axis, inertia);
|
|
349
|
+
});
|
|
350
|
+
// Run all animations and then resolve the new drag constraints.
|
|
351
|
+
return Promise.all(momentumAnimations).then(onDragTransitionEnd);
|
|
352
|
+
}
|
|
353
|
+
startAxisValueAnimation(axis, transition) {
|
|
354
|
+
const axisValue = this.getAxisMotionValue(axis);
|
|
355
|
+
addValueToWillChange(this.visualElement, axis);
|
|
356
|
+
return axisValue.start(animateMotionValue(axis, axisValue, 0, transition, this.visualElement, false));
|
|
357
|
+
}
|
|
358
|
+
stopAnimation() {
|
|
359
|
+
eachAxis((axis) => this.getAxisMotionValue(axis).stop());
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Drag works differently depending on which props are provided.
|
|
363
|
+
*
|
|
364
|
+
* - If _dragX and _dragY are provided, we output the gesture delta directly to those motion values.
|
|
365
|
+
* - Otherwise, we apply the delta to the x/y motion values.
|
|
366
|
+
*/
|
|
367
|
+
getAxisMotionValue(axis) {
|
|
368
|
+
const dragKey = `_drag${axis.toUpperCase()}`;
|
|
369
|
+
const props = this.readProps();
|
|
370
|
+
const externalMotionValue = props[dragKey];
|
|
371
|
+
return externalMotionValue
|
|
372
|
+
? externalMotionValue
|
|
373
|
+
: this.visualElement.getValue(axis, this.visualElement.latestValues[axis] ?? 0);
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Measure the live box - projection.layout may or may not include the
|
|
377
|
+
* drag transform, depending on when it was measured.
|
|
378
|
+
*/
|
|
379
|
+
snapToCursor({ clientX, clientY }) {
|
|
380
|
+
const { drag } = this.getProps();
|
|
381
|
+
const point = { x: clientX, y: clientY };
|
|
382
|
+
const cursor = this.visualElement.getTransformPagePoint()?.(point) || point;
|
|
383
|
+
const box = this.visualElement.measureViewportBox();
|
|
384
|
+
eachAxis((axis) => {
|
|
385
|
+
if (!shouldDrag(axis, drag, this.currentDirection))
|
|
386
|
+
return;
|
|
387
|
+
const axisValue = this.getAxisMotionValue(axis);
|
|
388
|
+
const { min, max } = box[axis];
|
|
389
|
+
axisValue.set((axisValue.get() || 0) + cursor[axis] - mixNumber(min, max, 0.5));
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* When the viewport resizes we want to check if the measured constraints
|
|
394
|
+
* have changed and, if so, reposition the element within those new constraints
|
|
395
|
+
* relative to where it was before the resize.
|
|
396
|
+
*/
|
|
397
|
+
scalePositionWithinConstraints() {
|
|
398
|
+
if (!this.visualElement.current)
|
|
399
|
+
return;
|
|
400
|
+
const { drag, dragConstraints } = this.getProps();
|
|
401
|
+
const { projection } = this.visualElement;
|
|
402
|
+
if (!isElementConstraints(dragConstraints) ||
|
|
403
|
+
!projection ||
|
|
404
|
+
!this.constraints)
|
|
405
|
+
return;
|
|
406
|
+
/**
|
|
407
|
+
* Stop current animations as there can be visual glitching if we try to do
|
|
408
|
+
* this mid-animation
|
|
409
|
+
*/
|
|
410
|
+
this.stopAnimation();
|
|
411
|
+
/**
|
|
412
|
+
* Record the relative position of the dragged element relative to the
|
|
413
|
+
* constraints box and save as a progress value.
|
|
414
|
+
*/
|
|
415
|
+
const constraints = this.constraints;
|
|
416
|
+
const boxProgress = { x: 0, y: 0 };
|
|
417
|
+
eachAxis((axis) => {
|
|
418
|
+
const latest = this.getAxisMotionValue(axis).get();
|
|
419
|
+
boxProgress[axis] = calcOrigin({ min: latest, max: latest }, constraints[axis]);
|
|
420
|
+
});
|
|
421
|
+
/**
|
|
422
|
+
* Update the layout of this element and resolve the latest drag constraints
|
|
423
|
+
*/
|
|
424
|
+
const { transformTemplate } = this.visualElement.getProps();
|
|
425
|
+
this.visualElement.current.style.transform = transformTemplate
|
|
426
|
+
? transformTemplate({}, "")
|
|
427
|
+
: "none";
|
|
428
|
+
projection.root && projection.root.updateScroll();
|
|
429
|
+
projection.updateLayout();
|
|
430
|
+
/**
|
|
431
|
+
* Reset constraints so resolveConstraints() will recalculate them
|
|
432
|
+
* with the freshly measured layout rather than returning the cached value.
|
|
433
|
+
*/
|
|
434
|
+
this.constraints = false;
|
|
435
|
+
this.resolveConstraints();
|
|
436
|
+
/**
|
|
437
|
+
* For each axis, calculate the current progress of the layout axis
|
|
438
|
+
* within the new constraints.
|
|
439
|
+
*/
|
|
440
|
+
eachAxis((axis) => {
|
|
441
|
+
const axisValue = this.getAxisMotionValue(axis);
|
|
442
|
+
/**
|
|
443
|
+
* An element resting at its origin has no offset to preserve,
|
|
444
|
+
* and re-projecting 0 into the new constraints would move it.
|
|
445
|
+
*/
|
|
446
|
+
if (!shouldDrag(axis, drag, null) || !axisValue.get())
|
|
447
|
+
return;
|
|
448
|
+
/**
|
|
449
|
+
* Calculate a new transform based on the previous box progress
|
|
450
|
+
*/
|
|
451
|
+
const { min, max } = this.constraints[axis];
|
|
452
|
+
axisValue.set(mixNumber(min, max, boxProgress[axis]));
|
|
453
|
+
});
|
|
454
|
+
/**
|
|
455
|
+
* Flush the updated transform to the DOM synchronously to prevent
|
|
456
|
+
* a visual flash at the element's CSS layout position (0,0) when
|
|
457
|
+
* the transform was stripped for measurement.
|
|
458
|
+
*/
|
|
459
|
+
this.visualElement.render();
|
|
460
|
+
}
|
|
461
|
+
addListeners() {
|
|
462
|
+
if (!this.visualElement.current)
|
|
463
|
+
return;
|
|
464
|
+
elementDragControls.set(this.visualElement, this);
|
|
465
|
+
const element = this.visualElement.current;
|
|
466
|
+
/**
|
|
467
|
+
* Attach a pointerdown event listener on this DOM element to initiate drag tracking.
|
|
468
|
+
*/
|
|
469
|
+
const stopPointerListener = addPointerEvent(element, "pointerdown", (event) => {
|
|
470
|
+
const { drag, dragListener = true } = this.getProps();
|
|
471
|
+
const target = event.target;
|
|
472
|
+
/**
|
|
473
|
+
* Only block drag if clicking on a text input child element
|
|
474
|
+
* (input, textarea, select, contenteditable) where users might
|
|
475
|
+
* want to select text or interact with the control.
|
|
476
|
+
*
|
|
477
|
+
* Buttons and links don't block drag since they don't have
|
|
478
|
+
* click-and-move actions of their own.
|
|
479
|
+
*/
|
|
480
|
+
const isClickingTextInputChild = target !== element && isElementTextInput(target);
|
|
481
|
+
if (drag && dragListener && !isClickingTextInputChild) {
|
|
482
|
+
this.start(event);
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
/**
|
|
486
|
+
* If using ref-based constraints, observe both the draggable element
|
|
487
|
+
* and the constraint container for size changes via ResizeObserver.
|
|
488
|
+
* Setup is deferred because dragConstraints.current is null when
|
|
489
|
+
* addListeners first runs (React hasn't committed the ref yet).
|
|
490
|
+
*/
|
|
491
|
+
let stopResizeObservers;
|
|
492
|
+
const measureDragConstraints = () => {
|
|
493
|
+
const { dragConstraints } = this.getProps();
|
|
494
|
+
const constraintsElement = isElementConstraints(dragConstraints) &&
|
|
495
|
+
resolveConstraintsElement(dragConstraints);
|
|
496
|
+
if (constraintsElement) {
|
|
497
|
+
this.constraints = this.resolveRefConstraints();
|
|
498
|
+
if (!stopResizeObservers) {
|
|
499
|
+
stopResizeObservers = startResizeObservers(element, constraintsElement, () => this.scalePositionWithinConstraints());
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
const { projection } = this.visualElement;
|
|
504
|
+
const stopMeasureLayoutListener = projection.addEventListener("measure", measureDragConstraints);
|
|
505
|
+
if (projection && !projection.layout) {
|
|
506
|
+
projection.root && projection.root.updateScroll();
|
|
507
|
+
projection.updateLayout();
|
|
508
|
+
}
|
|
509
|
+
frame.read(measureDragConstraints);
|
|
510
|
+
/**
|
|
511
|
+
* Attach a window resize listener to scale the draggable target within its defined
|
|
512
|
+
* constraints as the window resizes.
|
|
513
|
+
*/
|
|
514
|
+
const stopResizeListener = addDomEvent(window, "resize", () => this.scalePositionWithinConstraints());
|
|
515
|
+
/**
|
|
516
|
+
* If the element's layout changes, calculate the delta and apply that to
|
|
517
|
+
* the drag gesture's origin point.
|
|
518
|
+
*/
|
|
519
|
+
const stopLayoutUpdateListener = projection.addEventListener("didUpdate", (({ delta, hasLayoutChanged }) => {
|
|
520
|
+
if (this.isDragging && hasLayoutChanged) {
|
|
521
|
+
eachAxis((axis) => {
|
|
522
|
+
const motionValue = this.getAxisMotionValue(axis);
|
|
523
|
+
if (!motionValue)
|
|
524
|
+
return;
|
|
525
|
+
this.originPoint[axis] += delta[axis].translate;
|
|
526
|
+
motionValue.set(motionValue.get() + delta[axis].translate);
|
|
527
|
+
});
|
|
528
|
+
this.visualElement.render();
|
|
529
|
+
}
|
|
530
|
+
}));
|
|
531
|
+
return () => {
|
|
532
|
+
stopResizeListener();
|
|
533
|
+
stopPointerListener();
|
|
534
|
+
stopMeasureLayoutListener();
|
|
535
|
+
stopLayoutUpdateListener && stopLayoutUpdateListener();
|
|
536
|
+
stopResizeObservers && stopResizeObservers();
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
getProps() {
|
|
540
|
+
const props = this.readProps();
|
|
541
|
+
const { drag = false, dragDirectionLock = false, dragPropagation = false, dragConstraints = false, dragElastic = defaultElastic, dragMomentum = true, } = props;
|
|
542
|
+
return {
|
|
543
|
+
...props,
|
|
544
|
+
drag,
|
|
545
|
+
dragDirectionLock,
|
|
546
|
+
dragPropagation,
|
|
547
|
+
dragConstraints,
|
|
548
|
+
dragElastic,
|
|
549
|
+
dragMomentum,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* `dragConstraints` can be an element, or a ref (`{ current }`) to one.
|
|
555
|
+
*/
|
|
556
|
+
function isElementConstraints(constraints) {
|
|
557
|
+
return (!!constraints &&
|
|
558
|
+
typeof constraints === "object" &&
|
|
559
|
+
("current" in constraints || "nodeType" in constraints));
|
|
560
|
+
}
|
|
561
|
+
const resolveConstraintsElement = (constraints) => "current" in constraints ? constraints.current : constraints;
|
|
562
|
+
function skipFirstCall(callback) {
|
|
563
|
+
let isFirst = true;
|
|
564
|
+
return () => {
|
|
565
|
+
if (isFirst) {
|
|
566
|
+
isFirst = false;
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
callback();
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
function startResizeObservers(element, constraintsElement, onResize) {
|
|
573
|
+
const stopElement = resize(element, skipFirstCall(onResize));
|
|
574
|
+
const stopContainer = resize(constraintsElement, skipFirstCall(onResize));
|
|
575
|
+
return () => {
|
|
576
|
+
stopElement();
|
|
577
|
+
stopContainer();
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
function shouldDrag(direction, drag, currentDirection) {
|
|
581
|
+
return ((drag === true || drag === direction) &&
|
|
582
|
+
(currentDirection === null || currentDirection === direction));
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Based on an x/y offset determine the current drag direction. If both axis' offsets are lower
|
|
586
|
+
* than the provided threshold, return `null`.
|
|
587
|
+
*
|
|
588
|
+
* @param offset - The x/y offset from origin.
|
|
589
|
+
* @param lockThreshold - (Optional) - the minimum absolute offset before we can determine a drag direction.
|
|
590
|
+
*/
|
|
591
|
+
function getCurrentDirection(offset, lockThreshold = 10) {
|
|
592
|
+
let direction = null;
|
|
593
|
+
if (Math.abs(offset.y) > lockThreshold) {
|
|
594
|
+
direction = "y";
|
|
595
|
+
}
|
|
596
|
+
else if (Math.abs(offset.x) > lockThreshold) {
|
|
597
|
+
direction = "x";
|
|
598
|
+
}
|
|
599
|
+
return direction;
|
|
600
|
+
}
|
|
601
|
+
function expectsResolvedDragConstraints({ dragConstraints, onMeasureDragConstraints, }) {
|
|
602
|
+
return isElementConstraints(dragConstraints) && !!onMeasureDragConstraints;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
export { VisualElementDragControls, elementDragControls, expectsResolvedDragConstraints };
|
|
606
|
+
//# sourceMappingURL=VisualElementDragControls.mjs.map
|