framer-motion 7.0.3 → 7.1.2
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 +2090 -2304
- package/dist/es/animation/animate.mjs +4 -5
- package/dist/es/animation/animation-controls.mjs +22 -24
- package/dist/es/animation/use-animated-state.mjs +23 -25
- package/dist/es/animation/use-animation.mjs +2 -2
- package/dist/es/animation/utils/default-transitions.mjs +13 -14
- package/dist/es/animation/utils/easing.mjs +20 -21
- package/dist/es/animation/utils/is-animatable.mjs +1 -1
- package/dist/es/animation/utils/is-keyframes-target.mjs +1 -1
- package/dist/es/animation/utils/transitions.mjs +30 -31
- package/dist/es/components/AnimatePresence/PresenceChild.mjs +18 -31
- package/dist/es/components/AnimatePresence/index.mjs +33 -35
- package/dist/es/components/AnimatePresence/use-presence.mjs +5 -5
- package/dist/es/components/AnimateSharedLayout.mjs +4 -5
- package/dist/es/components/LayoutGroup/index.mjs +12 -16
- package/dist/es/components/LazyMotion/index.mjs +9 -10
- package/dist/es/components/MotionConfig/index.mjs +5 -5
- package/dist/es/components/Reorder/Group.mjs +16 -16
- package/dist/es/components/Reorder/Item.mjs +14 -18
- package/dist/es/components/Reorder/index.mjs +3 -3
- package/dist/es/components/Reorder/utils/check-reorder.mjs +6 -6
- package/dist/es/context/DeprecatedLayoutGroupContext.mjs +1 -1
- package/dist/es/context/LayoutGroupContext.mjs +1 -1
- package/dist/es/context/LazyContext.mjs +1 -1
- package/dist/es/context/MotionConfigContext.mjs +2 -2
- package/dist/es/context/MotionContext/create.mjs +2 -2
- package/dist/es/context/MotionContext/index.mjs +1 -1
- package/dist/es/context/MotionContext/utils.mjs +1 -1
- package/dist/es/context/PresenceContext.mjs +1 -1
- package/dist/es/context/ReorderContext.mjs +1 -1
- package/dist/es/context/SwitchLayoutGroupContext.mjs +1 -1
- package/dist/es/events/event-info.mjs +11 -17
- package/dist/es/events/use-dom-event.mjs +4 -5
- package/dist/es/events/use-pointer-event.mjs +2 -2
- package/dist/es/events/utils.mjs +3 -9
- package/dist/es/gestures/PanSession.mjs +42 -47
- package/dist/es/gestures/drag/VisualElementDragControls.mjs +147 -157
- package/dist/es/gestures/drag/use-drag-controls.mjs +10 -12
- package/dist/es/gestures/drag/use-drag.mjs +4 -4
- package/dist/es/gestures/drag/utils/constraints.mjs +12 -17
- package/dist/es/gestures/drag/utils/lock.mjs +17 -17
- package/dist/es/gestures/use-focus-gesture.mjs +3 -4
- package/dist/es/gestures/use-hover-gesture.mjs +2 -3
- package/dist/es/gestures/use-pan-gesture.mjs +9 -10
- package/dist/es/gestures/use-tap-gesture.mjs +5 -6
- package/dist/es/gestures/utils/event-type.mjs +1 -1
- package/dist/es/gestures/utils/is-node-or-child.mjs +1 -1
- package/dist/es/index.mjs +1 -0
- package/dist/es/motion/features/animations.mjs +9 -11
- package/dist/es/motion/features/definitions.mjs +5 -5
- package/dist/es/motion/features/drag.mjs +1 -1
- package/dist/es/motion/features/gestures.mjs +1 -1
- package/dist/es/motion/features/layout/MeasureLayout.mjs +32 -40
- package/dist/es/motion/features/layout/index.mjs +1 -1
- package/dist/es/motion/features/use-features.mjs +8 -9
- package/dist/es/motion/features/use-projection.mjs +10 -11
- package/dist/es/motion/features/viewport/observers.mjs +12 -12
- package/dist/es/motion/features/viewport/use-viewport.mjs +17 -20
- package/dist/es/motion/index.mjs +13 -16
- package/dist/es/motion/utils/VisualElementHandler.mjs +10 -16
- package/dist/es/motion/utils/is-forced-motion-value.mjs +1 -2
- package/dist/es/motion/utils/make-renderless-component.mjs +2 -2
- package/dist/es/motion/utils/use-motion-ref.mjs +1 -1
- package/dist/es/motion/utils/use-visual-element.mjs +13 -13
- package/dist/es/motion/utils/use-visual-state.mjs +26 -31
- package/dist/es/motion/utils/valid-prop.mjs +1 -1
- package/dist/es/projection/animation/mix-values.mjs +12 -16
- package/dist/es/projection/geometry/conversion.mjs +4 -6
- package/dist/es/projection/geometry/delta-apply.mjs +14 -20
- package/dist/es/projection/geometry/delta-calc.mjs +2 -5
- package/dist/es/projection/geometry/delta-remove.mjs +6 -13
- package/dist/es/projection/geometry/models.mjs +7 -7
- package/dist/es/projection/node/DocumentProjectionNode.mjs +5 -5
- package/dist/es/projection/node/HTMLProjectionNode.mjs +8 -10
- package/dist/es/projection/node/create-projection-node.mjs +283 -307
- package/dist/es/projection/node/group.mjs +6 -8
- package/dist/es/projection/node/id.mjs +2 -2
- package/dist/es/projection/node/state.mjs +1 -1
- package/dist/es/projection/shared/stack.mjs +26 -27
- package/dist/es/projection/styles/scale-border-radius.mjs +5 -5
- package/dist/es/projection/styles/scale-box-shadow.mjs +18 -19
- package/dist/es/projection/styles/scale-correction.mjs +1 -1
- package/dist/es/projection/styles/transform.mjs +12 -12
- package/dist/es/projection/use-reset-projection.mjs +2 -2
- package/dist/es/projection/utils/has-transform.mjs +1 -2
- package/dist/es/projection/utils/measure.mjs +2 -2
- package/dist/es/render/dom/create-visual-element.mjs +1 -1
- package/dist/es/render/dom/features-animation.mjs +1 -2
- package/dist/es/render/dom/features-max.mjs +1 -2
- package/dist/es/render/dom/motion-minimal.mjs +1 -1
- package/dist/es/render/dom/motion-proxy.mjs +3 -4
- package/dist/es/render/dom/motion.mjs +2 -5
- package/dist/es/render/dom/use-render.mjs +6 -9
- package/dist/es/render/dom/utils/camel-to-dash.mjs +3 -5
- package/dist/es/render/dom/utils/create-config.mjs +5 -5
- package/dist/es/render/dom/utils/css-variables-conversion.mjs +19 -20
- package/dist/es/render/dom/utils/filter-props.mjs +4 -6
- package/dist/es/render/dom/utils/parse-dom-variant.mjs +2 -2
- package/dist/es/render/dom/utils/unit-conversion.mjs +68 -99
- package/dist/es/render/dom/value-types/animatable-none.mjs +1 -1
- package/dist/es/render/dom/value-types/defaults.mjs +4 -5
- package/dist/es/render/dom/value-types/dimensions.mjs +2 -4
- package/dist/es/render/dom/value-types/find.mjs +2 -3
- package/dist/es/render/dom/value-types/get-as-type.mjs +1 -1
- package/dist/es/render/dom/value-types/number.mjs +2 -2
- package/dist/es/render/dom/value-types/test.mjs +1 -1
- package/dist/es/render/dom/value-types/type-auto.mjs +3 -3
- package/dist/es/render/dom/value-types/type-int.mjs +1 -2
- package/dist/es/render/html/config-motion.mjs +1 -1
- package/dist/es/render/html/use-props.mjs +11 -13
- package/dist/es/render/html/utils/build-styles.mjs +8 -8
- package/dist/es/render/html/utils/build-transform.mjs +11 -16
- package/dist/es/render/html/utils/create-render-state.mjs +2 -2
- package/dist/es/render/html/utils/render.mjs +2 -3
- package/dist/es/render/html/utils/scrape-motion-values.mjs +3 -3
- package/dist/es/render/html/utils/transform.mjs +6 -10
- package/dist/es/render/html/visual-element.mjs +22 -23
- package/dist/es/render/index.mjs +359 -364
- package/dist/es/render/svg/config-motion.mjs +2 -3
- package/dist/es/render/svg/lowercase-elements.mjs +1 -1
- package/dist/es/render/svg/use-props.mjs +5 -6
- package/dist/es/render/svg/utils/build-attrs.mjs +2 -2
- package/dist/es/render/svg/utils/camel-case-attrs.mjs +1 -1
- package/dist/es/render/svg/utils/create-render-state.mjs +1 -2
- package/dist/es/render/svg/utils/path.mjs +7 -10
- package/dist/es/render/svg/utils/render.mjs +1 -1
- package/dist/es/render/svg/utils/scrape-motion-values.mjs +3 -3
- package/dist/es/render/svg/utils/transform-origin.mjs +3 -3
- package/dist/es/render/svg/visual-element.mjs +6 -4
- package/dist/es/render/utils/animation-state.mjs +71 -87
- package/dist/es/render/utils/animation.mjs +48 -60
- package/dist/es/render/utils/compare-by-depth.mjs +1 -3
- package/dist/es/render/utils/flat-tree.mjs +9 -10
- package/dist/es/render/utils/lifecycles.mjs +12 -19
- package/dist/es/render/utils/motion-values.mjs +16 -8
- package/dist/es/render/utils/setters.mjs +19 -19
- package/dist/es/render/utils/variants.mjs +6 -8
- package/dist/es/utils/array.mjs +9 -10
- package/dist/es/utils/is-browser.mjs +1 -1
- package/dist/es/utils/is-numerical-string.mjs +1 -1
- package/dist/es/utils/is-zero-value-string.mjs +1 -1
- package/dist/es/utils/process.mjs +2 -2
- package/dist/es/utils/resolve-value.mjs +2 -2
- package/dist/es/utils/shallow-compare.mjs +2 -2
- package/dist/es/utils/subscription-manager.mjs +15 -17
- package/dist/es/utils/time-conversion.mjs +1 -1
- package/dist/es/utils/transform.mjs +10 -15
- package/dist/es/utils/use-animation-frame.mjs +5 -6
- package/dist/es/utils/use-constant.mjs +1 -1
- package/dist/es/utils/use-cycle.mjs +9 -10
- package/dist/es/utils/use-force-update.mjs +4 -5
- package/dist/es/utils/use-in-view.mjs +7 -9
- package/dist/es/utils/use-instant-transition-state.mjs +1 -1
- package/dist/es/utils/use-instant-transition.mjs +6 -9
- package/dist/es/utils/use-is-mounted.mjs +3 -3
- package/dist/es/utils/use-isomorphic-effect.mjs +1 -1
- package/dist/es/utils/use-reduced-motion.mjs +8 -11
- package/dist/es/utils/use-unmount-effect.mjs +1 -1
- package/dist/es/utils/warn-once.mjs +1 -1
- package/dist/es/value/index.mjs +53 -59
- package/dist/es/value/use-combine-values.mjs +3 -3
- package/dist/es/value/use-inverted-scale.mjs +9 -11
- package/dist/es/value/use-motion-template.mjs +5 -9
- package/dist/es/value/use-motion-value.mjs +4 -5
- package/dist/es/value/use-on-change.mjs +4 -4
- package/dist/es/value/use-scroll.mjs +9 -11
- package/dist/es/value/use-spring.mjs +8 -10
- package/dist/es/value/use-time.mjs +2 -2
- package/dist/es/value/use-transform.mjs +6 -10
- package/dist/es/value/use-velocity.mjs +3 -3
- package/dist/es/value/use-will-change/index.mjs +50 -0
- package/dist/es/value/use-will-change/is.mjs +7 -0
- package/dist/es/value/utils/is-motion-value.mjs +1 -1
- package/dist/es/value/utils/resolve-motion-value.mjs +1 -1
- package/dist/framer-motion.dev.js +2134 -2414
- package/dist/framer-motion.js +1 -1
- package/dist/index.d.ts +9 -2
- package/dist/projection.dev.js +1154 -1300
- package/dist/size-rollup-dom-animation.js +1 -1
- package/dist/size-rollup-dom-max.js +1 -1
- package/dist/size-rollup-m.js +1 -1
- package/dist/size-webpack-dom-animation.js +1 -1
- package/dist/size-webpack-dom-max.js +1 -1
- package/dist/size-webpack-m.js +1 -1
- package/dist/three-entry.d.ts +0 -1
- package/package.json +12 -12
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { __assign } from 'tslib';
|
|
2
1
|
import { invariant } from 'hey-listen';
|
|
3
2
|
import { PanSession } from '../PanSession.mjs';
|
|
4
3
|
import { getGlobalLock } from './utils/lock.mjs';
|
|
@@ -17,13 +16,13 @@ import { mix } from 'popmotion';
|
|
|
17
16
|
import { percent } from 'style-value-types';
|
|
18
17
|
import { calcLength } from '../../projection/geometry/delta-calc.mjs';
|
|
19
18
|
|
|
20
|
-
|
|
19
|
+
const elementDragControls = new WeakMap();
|
|
21
20
|
/**
|
|
22
21
|
*
|
|
23
22
|
*/
|
|
24
23
|
// let latestPointerEvent: AnyPointerEvent
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
class VisualElementDragControls {
|
|
25
|
+
constructor(visualElement) {
|
|
27
26
|
// This is a reference to the global drag gesture lock, ensuring only one component
|
|
28
27
|
// can "capture" the drag of one or both axes.
|
|
29
28
|
// TODO: Look into moving this into pansession?
|
|
@@ -42,116 +41,112 @@ var VisualElementDragControls = /** @class */ (function () {
|
|
|
42
41
|
this.elastic = createBox();
|
|
43
42
|
this.visualElement = visualElement;
|
|
44
43
|
}
|
|
45
|
-
|
|
46
|
-
var _this = this;
|
|
47
|
-
var _b = _a === void 0 ? {} : _a, _c = _b.snapToCursor, snapToCursor = _c === void 0 ? false : _c;
|
|
44
|
+
start(originEvent, { snapToCursor = false } = {}) {
|
|
48
45
|
/**
|
|
49
46
|
* Don't start dragging if this component is exiting
|
|
50
47
|
*/
|
|
51
48
|
if (this.visualElement.isPresent === false)
|
|
52
49
|
return;
|
|
53
|
-
|
|
50
|
+
const onSessionStart = (event) => {
|
|
54
51
|
// Stop any animations on both axis values immediately. This allows the user to throw and catch
|
|
55
52
|
// the component.
|
|
56
|
-
|
|
53
|
+
this.stopAnimation();
|
|
57
54
|
if (snapToCursor) {
|
|
58
|
-
|
|
55
|
+
this.snapToCursor(extractEventInfo(event, "page").point);
|
|
59
56
|
}
|
|
60
57
|
};
|
|
61
|
-
|
|
58
|
+
const onStart = (event, info) => {
|
|
62
59
|
var _a;
|
|
63
60
|
// Attempt to grab the global drag gesture lock - maybe make this part of PanSession
|
|
64
|
-
|
|
61
|
+
const { drag, dragPropagation, onDragStart } = this.getProps();
|
|
65
62
|
if (drag && !dragPropagation) {
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
if (this.openGlobalLock)
|
|
64
|
+
this.openGlobalLock();
|
|
65
|
+
this.openGlobalLock = getGlobalLock(drag);
|
|
69
66
|
// If we don 't have the lock, don't start dragging
|
|
70
|
-
if (!
|
|
67
|
+
if (!this.openGlobalLock)
|
|
71
68
|
return;
|
|
72
69
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
-
|
|
70
|
+
this.isDragging = true;
|
|
71
|
+
this.currentDirection = null;
|
|
72
|
+
this.resolveConstraints();
|
|
73
|
+
if (this.visualElement.projection) {
|
|
74
|
+
this.visualElement.projection.isAnimationBlocked = true;
|
|
75
|
+
this.visualElement.projection.target = undefined;
|
|
79
76
|
}
|
|
80
77
|
/**
|
|
81
78
|
* Record gesture origin
|
|
82
79
|
*/
|
|
83
|
-
eachAxis(
|
|
80
|
+
eachAxis((axis) => {
|
|
84
81
|
var _a, _b;
|
|
85
|
-
|
|
82
|
+
let current = this.getAxisMotionValue(axis).get() || 0;
|
|
86
83
|
/**
|
|
87
84
|
* If the MotionValue is a percentage value convert to px
|
|
88
85
|
*/
|
|
89
86
|
if (percent.test(current)) {
|
|
90
|
-
|
|
87
|
+
const measuredAxis = (_b = (_a = this.visualElement.projection) === null || _a === void 0 ? void 0 : _a.layout) === null || _b === void 0 ? void 0 : _b.actual[axis];
|
|
91
88
|
if (measuredAxis) {
|
|
92
|
-
|
|
93
|
-
current =
|
|
89
|
+
const length = calcLength(measuredAxis);
|
|
90
|
+
current = length * (parseFloat(current) / 100);
|
|
94
91
|
}
|
|
95
92
|
}
|
|
96
|
-
|
|
93
|
+
this.originPoint[axis] = current;
|
|
97
94
|
});
|
|
98
95
|
// Fire onDragStart event
|
|
99
96
|
onDragStart === null || onDragStart === void 0 ? void 0 : onDragStart(event, info);
|
|
100
|
-
(_a =
|
|
97
|
+
(_a = this.visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(AnimationType.Drag, true);
|
|
101
98
|
};
|
|
102
|
-
|
|
99
|
+
const onMove = (event, info) => {
|
|
103
100
|
// latestPointerEvent = event
|
|
104
|
-
|
|
101
|
+
const { dragPropagation, dragDirectionLock, onDirectionLock, onDrag, } = this.getProps();
|
|
105
102
|
// If we didn't successfully receive the gesture lock, early return.
|
|
106
|
-
if (!dragPropagation && !
|
|
103
|
+
if (!dragPropagation && !this.openGlobalLock)
|
|
107
104
|
return;
|
|
108
|
-
|
|
105
|
+
const { offset } = info;
|
|
109
106
|
// Attempt to detect drag direction if directionLock is true
|
|
110
|
-
if (dragDirectionLock &&
|
|
111
|
-
|
|
107
|
+
if (dragDirectionLock && this.currentDirection === null) {
|
|
108
|
+
this.currentDirection = getCurrentDirection(offset);
|
|
112
109
|
// If we've successfully set a direction, notify listener
|
|
113
|
-
if (
|
|
114
|
-
onDirectionLock === null || onDirectionLock === void 0 ? void 0 : onDirectionLock(
|
|
110
|
+
if (this.currentDirection !== null) {
|
|
111
|
+
onDirectionLock === null || onDirectionLock === void 0 ? void 0 : onDirectionLock(this.currentDirection);
|
|
115
112
|
}
|
|
116
113
|
return;
|
|
117
114
|
}
|
|
118
115
|
// Update each point with the latest position
|
|
119
|
-
|
|
120
|
-
|
|
116
|
+
this.updateAxis("x", info.point, offset);
|
|
117
|
+
this.updateAxis("y", info.point, offset);
|
|
121
118
|
/**
|
|
122
119
|
* Ideally we would leave the renderer to fire naturally at the end of
|
|
123
120
|
* this frame but if the element is about to change layout as the result
|
|
124
121
|
* of a re-render we want to ensure the browser can read the latest
|
|
125
122
|
* bounding box to ensure the pointer and element don't fall out of sync.
|
|
126
123
|
*/
|
|
127
|
-
|
|
124
|
+
this.visualElement.syncRender();
|
|
128
125
|
/**
|
|
129
126
|
* This must fire after the syncRender call as it might trigger a state
|
|
130
127
|
* change which itself might trigger a layout update.
|
|
131
128
|
*/
|
|
132
129
|
onDrag === null || onDrag === void 0 ? void 0 : onDrag(event, info);
|
|
133
130
|
};
|
|
134
|
-
|
|
135
|
-
return _this.stop(event, info);
|
|
136
|
-
};
|
|
131
|
+
const onSessionEnd = (event, info) => this.stop(event, info);
|
|
137
132
|
this.panSession = new PanSession(originEvent, {
|
|
138
|
-
onSessionStart
|
|
139
|
-
onStart
|
|
140
|
-
onMove
|
|
141
|
-
onSessionEnd
|
|
133
|
+
onSessionStart,
|
|
134
|
+
onStart,
|
|
135
|
+
onMove,
|
|
136
|
+
onSessionEnd,
|
|
142
137
|
}, { transformPagePoint: this.visualElement.getTransformPagePoint() });
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
138
|
+
}
|
|
139
|
+
stop(event, info) {
|
|
140
|
+
const isDragging = this.isDragging;
|
|
146
141
|
this.cancel();
|
|
147
142
|
if (!isDragging)
|
|
148
143
|
return;
|
|
149
|
-
|
|
144
|
+
const { velocity } = info;
|
|
150
145
|
this.startAnimation(velocity);
|
|
151
|
-
|
|
146
|
+
const { onDragEnd } = this.getProps();
|
|
152
147
|
onDragEnd === null || onDragEnd === void 0 ? void 0 : onDragEnd(event, info);
|
|
153
|
-
}
|
|
154
|
-
|
|
148
|
+
}
|
|
149
|
+
cancel() {
|
|
155
150
|
var _a, _b;
|
|
156
151
|
this.isDragging = false;
|
|
157
152
|
if (this.visualElement.projection) {
|
|
@@ -159,31 +154,30 @@ var VisualElementDragControls = /** @class */ (function () {
|
|
|
159
154
|
}
|
|
160
155
|
(_a = this.panSession) === null || _a === void 0 ? void 0 : _a.end();
|
|
161
156
|
this.panSession = undefined;
|
|
162
|
-
|
|
157
|
+
const { dragPropagation } = this.getProps();
|
|
163
158
|
if (!dragPropagation && this.openGlobalLock) {
|
|
164
159
|
this.openGlobalLock();
|
|
165
160
|
this.openGlobalLock = null;
|
|
166
161
|
}
|
|
167
162
|
(_b = this.visualElement.animationState) === null || _b === void 0 ? void 0 : _b.setActive(AnimationType.Drag, false);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
163
|
+
}
|
|
164
|
+
updateAxis(axis, _point, offset) {
|
|
165
|
+
const { drag } = this.getProps();
|
|
171
166
|
// If we're not dragging this axis, do an early return.
|
|
172
167
|
if (!offset || !shouldDrag(axis, drag, this.currentDirection))
|
|
173
168
|
return;
|
|
174
|
-
|
|
175
|
-
|
|
169
|
+
const axisValue = this.getAxisMotionValue(axis);
|
|
170
|
+
let next = this.originPoint[axis] + offset[axis];
|
|
176
171
|
// Apply constraints
|
|
177
172
|
if (this.constraints && this.constraints[axis]) {
|
|
178
173
|
next = applyConstraints(next, this.constraints[axis], this.elastic[axis]);
|
|
179
174
|
}
|
|
180
175
|
axisValue.set(next);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
var prevConstraints = this.constraints;
|
|
176
|
+
}
|
|
177
|
+
resolveConstraints() {
|
|
178
|
+
const { dragConstraints, dragElastic } = this.getProps();
|
|
179
|
+
const { layout } = this.visualElement.projection || {};
|
|
180
|
+
const prevConstraints = this.constraints;
|
|
187
181
|
if (dragConstraints && isRefObject(dragConstraints)) {
|
|
188
182
|
if (!this.constraints) {
|
|
189
183
|
this.constraints = this.resolveRefConstraints();
|
|
@@ -206,48 +200,47 @@ var VisualElementDragControls = /** @class */ (function () {
|
|
|
206
200
|
layout &&
|
|
207
201
|
this.constraints &&
|
|
208
202
|
!this.hasMutatedConstraints) {
|
|
209
|
-
eachAxis(
|
|
210
|
-
if (
|
|
211
|
-
|
|
203
|
+
eachAxis((axis) => {
|
|
204
|
+
if (this.getAxisMotionValue(axis)) {
|
|
205
|
+
this.constraints[axis] = rebaseAxisConstraints(layout.actual[axis], this.constraints[axis]);
|
|
212
206
|
}
|
|
213
207
|
});
|
|
214
208
|
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
209
|
+
}
|
|
210
|
+
resolveRefConstraints() {
|
|
211
|
+
const { dragConstraints: constraints, onMeasureDragConstraints } = this.getProps();
|
|
218
212
|
if (!constraints || !isRefObject(constraints))
|
|
219
213
|
return false;
|
|
220
|
-
|
|
214
|
+
const constraintsElement = constraints.current;
|
|
221
215
|
invariant(constraintsElement !== null, "If `dragConstraints` is set as a React ref, that ref must be passed to another component's `ref` prop.");
|
|
222
|
-
|
|
216
|
+
const { projection } = this.visualElement;
|
|
223
217
|
// TODO
|
|
224
218
|
if (!projection || !projection.layout)
|
|
225
219
|
return false;
|
|
226
|
-
|
|
227
|
-
|
|
220
|
+
const constraintsBox = measurePageBox(constraintsElement, projection.root, this.visualElement.getTransformPagePoint());
|
|
221
|
+
let measuredConstraints = calcViewportConstraints(projection.layout.actual, constraintsBox);
|
|
228
222
|
/**
|
|
229
223
|
* If there's an onMeasureDragConstraints listener we call it and
|
|
230
224
|
* if different constraints are returned, set constraints to that
|
|
231
225
|
*/
|
|
232
226
|
if (onMeasureDragConstraints) {
|
|
233
|
-
|
|
227
|
+
const userConstraints = onMeasureDragConstraints(convertBoxToBoundingBox(measuredConstraints));
|
|
234
228
|
this.hasMutatedConstraints = !!userConstraints;
|
|
235
229
|
if (userConstraints) {
|
|
236
230
|
measuredConstraints = convertBoundingBoxToBox(userConstraints);
|
|
237
231
|
}
|
|
238
232
|
}
|
|
239
233
|
return measuredConstraints;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
var momentumAnimations = eachAxis(function (axis) {
|
|
234
|
+
}
|
|
235
|
+
startAnimation(velocity) {
|
|
236
|
+
const { drag, dragMomentum, dragElastic, dragTransition, dragSnapToOrigin, onDragTransitionEnd, } = this.getProps();
|
|
237
|
+
const constraints = this.constraints || {};
|
|
238
|
+
const momentumAnimations = eachAxis((axis) => {
|
|
246
239
|
var _a;
|
|
247
|
-
if (!shouldDrag(axis, drag,
|
|
240
|
+
if (!shouldDrag(axis, drag, this.currentDirection)) {
|
|
248
241
|
return;
|
|
249
242
|
}
|
|
250
|
-
|
|
243
|
+
let transition = (_a = constraints === null || constraints === void 0 ? void 0 : constraints[axis]) !== null && _a !== void 0 ? _a : {};
|
|
251
244
|
if (dragSnapToOrigin)
|
|
252
245
|
transition = { min: 0, max: 0 };
|
|
253
246
|
/**
|
|
@@ -256,64 +249,62 @@ var VisualElementDragControls = /** @class */ (function () {
|
|
|
256
249
|
* We could do something here where we affect the `bounceStiffness` and `bounceDamping`
|
|
257
250
|
* using the value of `dragElastic`.
|
|
258
251
|
*/
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
252
|
+
const bounceStiffness = dragElastic ? 200 : 1000000;
|
|
253
|
+
const bounceDamping = dragElastic ? 40 : 10000000;
|
|
254
|
+
const inertia = Object.assign(Object.assign({ type: "inertia", velocity: dragMomentum ? velocity[axis] : 0, bounceStiffness,
|
|
255
|
+
bounceDamping, timeConstant: 750, restDelta: 1, restSpeed: 10 }, dragTransition), transition);
|
|
262
256
|
// If we're not animating on an externally-provided `MotionValue` we can use the
|
|
263
257
|
// component's animation controls which will handle interactions with whileHover (etc),
|
|
264
258
|
// otherwise we just have to animate the `MotionValue` itself.
|
|
265
|
-
return
|
|
259
|
+
return this.startAxisValueAnimation(axis, inertia);
|
|
266
260
|
});
|
|
267
261
|
// Run all animations and then resolve the new drag constraints.
|
|
268
262
|
return Promise.all(momentumAnimations).then(onDragTransitionEnd);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
|
|
263
|
+
}
|
|
264
|
+
startAxisValueAnimation(axis, transition) {
|
|
265
|
+
const axisValue = this.getAxisMotionValue(axis);
|
|
272
266
|
return startAnimation(axis, axisValue, 0, transition);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
};
|
|
267
|
+
}
|
|
268
|
+
stopAnimation() {
|
|
269
|
+
eachAxis((axis) => this.getAxisMotionValue(axis).stop());
|
|
270
|
+
}
|
|
278
271
|
/**
|
|
279
272
|
* Drag works differently depending on which props are provided.
|
|
280
273
|
*
|
|
281
274
|
* - If _dragX and _dragY are provided, we output the gesture delta directly to those motion values.
|
|
282
275
|
* - Otherwise, we apply the delta to the x/y motion values.
|
|
283
276
|
*/
|
|
284
|
-
|
|
277
|
+
getAxisMotionValue(axis) {
|
|
285
278
|
var _a, _b;
|
|
286
|
-
|
|
287
|
-
|
|
279
|
+
const dragKey = "_drag" + axis.toUpperCase();
|
|
280
|
+
const externalMotionValue = this.visualElement.getProps()[dragKey];
|
|
288
281
|
return externalMotionValue
|
|
289
282
|
? externalMotionValue
|
|
290
283
|
: this.visualElement.getValue(axis, (_b = (_a = this.visualElement.getProps().initial) === null || _a === void 0 ? void 0 : _a[axis]) !== null && _b !== void 0 ? _b : 0);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
var drag = _this.getProps().drag;
|
|
284
|
+
}
|
|
285
|
+
snapToCursor(point) {
|
|
286
|
+
eachAxis((axis) => {
|
|
287
|
+
const { drag } = this.getProps();
|
|
296
288
|
// If we're not dragging this axis, do an early return.
|
|
297
|
-
if (!shouldDrag(axis, drag,
|
|
289
|
+
if (!shouldDrag(axis, drag, this.currentDirection))
|
|
298
290
|
return;
|
|
299
|
-
|
|
300
|
-
|
|
291
|
+
const { projection } = this.visualElement;
|
|
292
|
+
const axisValue = this.getAxisMotionValue(axis);
|
|
301
293
|
if (projection && projection.layout) {
|
|
302
|
-
|
|
294
|
+
const { min, max } = projection.layout.actual[axis];
|
|
303
295
|
axisValue.set(point[axis] - mix(min, max, 0.5));
|
|
304
296
|
}
|
|
305
297
|
});
|
|
306
|
-
}
|
|
298
|
+
}
|
|
307
299
|
/**
|
|
308
300
|
* When the viewport resizes we want to check if the measured constraints
|
|
309
301
|
* have changed and, if so, reposition the element within those new constraints
|
|
310
302
|
* relative to where it was before the resize.
|
|
311
303
|
*/
|
|
312
|
-
|
|
313
|
-
var _this = this;
|
|
304
|
+
scalePositionWithinConstraints() {
|
|
314
305
|
var _a;
|
|
315
|
-
|
|
316
|
-
|
|
306
|
+
const { drag, dragConstraints } = this.getProps();
|
|
307
|
+
const { projection } = this.visualElement;
|
|
317
308
|
if (!isRefObject(dragConstraints) || !projection || !this.constraints)
|
|
318
309
|
return;
|
|
319
310
|
/**
|
|
@@ -325,18 +316,18 @@ var VisualElementDragControls = /** @class */ (function () {
|
|
|
325
316
|
* Record the relative position of the dragged element relative to the
|
|
326
317
|
* constraints box and save as a progress value.
|
|
327
318
|
*/
|
|
328
|
-
|
|
329
|
-
eachAxis(
|
|
330
|
-
|
|
319
|
+
const boxProgress = { x: 0, y: 0 };
|
|
320
|
+
eachAxis((axis) => {
|
|
321
|
+
const axisValue = this.getAxisMotionValue(axis);
|
|
331
322
|
if (axisValue) {
|
|
332
|
-
|
|
333
|
-
boxProgress[axis] = calcOrigin({ min: latest, max: latest },
|
|
323
|
+
const latest = axisValue.get();
|
|
324
|
+
boxProgress[axis] = calcOrigin({ min: latest, max: latest }, this.constraints[axis]);
|
|
334
325
|
}
|
|
335
326
|
});
|
|
336
327
|
/**
|
|
337
328
|
* Update the layout of this element and resolve the latest drag constraints
|
|
338
329
|
*/
|
|
339
|
-
|
|
330
|
+
const { transformTemplate } = this.visualElement.getProps();
|
|
340
331
|
this.visualElement.getInstance().style.transform = transformTemplate
|
|
341
332
|
? transformTemplate({}, "")
|
|
342
333
|
: "none";
|
|
@@ -347,37 +338,36 @@ var VisualElementDragControls = /** @class */ (function () {
|
|
|
347
338
|
* For each axis, calculate the current progress of the layout axis
|
|
348
339
|
* within the new constraints.
|
|
349
340
|
*/
|
|
350
|
-
eachAxis(
|
|
341
|
+
eachAxis((axis) => {
|
|
351
342
|
if (!shouldDrag(axis, drag, null))
|
|
352
343
|
return;
|
|
353
344
|
/**
|
|
354
345
|
* Calculate a new transform based on the previous box progress
|
|
355
346
|
*/
|
|
356
|
-
|
|
357
|
-
|
|
347
|
+
const axisValue = this.getAxisMotionValue(axis);
|
|
348
|
+
const { min, max } = this.constraints[axis];
|
|
358
349
|
axisValue.set(mix(min, max, boxProgress[axis]));
|
|
359
350
|
});
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
var _this = this;
|
|
351
|
+
}
|
|
352
|
+
addListeners() {
|
|
363
353
|
var _a;
|
|
364
354
|
elementDragControls.set(this.visualElement, this);
|
|
365
|
-
|
|
355
|
+
const element = this.visualElement.getInstance();
|
|
366
356
|
/**
|
|
367
357
|
* Attach a pointerdown event listener on this DOM element to initiate drag tracking.
|
|
368
358
|
*/
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
drag && dragListener &&
|
|
359
|
+
const stopPointerListener = addPointerEvent(element, "pointerdown", (event) => {
|
|
360
|
+
const { drag, dragListener = true } = this.getProps();
|
|
361
|
+
drag && dragListener && this.start(event);
|
|
372
362
|
});
|
|
373
|
-
|
|
374
|
-
|
|
363
|
+
const measureDragConstraints = () => {
|
|
364
|
+
const { dragConstraints } = this.getProps();
|
|
375
365
|
if (isRefObject(dragConstraints)) {
|
|
376
|
-
|
|
366
|
+
this.constraints = this.resolveRefConstraints();
|
|
377
367
|
}
|
|
378
368
|
};
|
|
379
|
-
|
|
380
|
-
|
|
369
|
+
const { projection } = this.visualElement;
|
|
370
|
+
const stopMeasureLayoutListener = projection.addEventListener("measure", measureDragConstraints);
|
|
381
371
|
if (projection && !projection.layout) {
|
|
382
372
|
(_a = projection.root) === null || _a === void 0 ? void 0 : _a.updateScroll();
|
|
383
373
|
projection.updateLayout();
|
|
@@ -387,40 +377,41 @@ var VisualElementDragControls = /** @class */ (function () {
|
|
|
387
377
|
* Attach a window resize listener to scale the draggable target within its defined
|
|
388
378
|
* constraints as the window resizes.
|
|
389
379
|
*/
|
|
390
|
-
|
|
391
|
-
return _this.scalePositionWithinConstraints();
|
|
392
|
-
});
|
|
380
|
+
const stopResizeListener = addDomEvent(window, "resize", () => this.scalePositionWithinConstraints());
|
|
393
381
|
/**
|
|
394
382
|
* If the element's layout changes, calculate the delta and apply that to
|
|
395
383
|
* the drag gesture's origin point.
|
|
396
384
|
*/
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
var motionValue = _this.getAxisMotionValue(axis);
|
|
385
|
+
const stopLayoutUpdateListener = projection.addEventListener("didUpdate", (({ delta, hasLayoutChanged }) => {
|
|
386
|
+
if (this.isDragging && hasLayoutChanged) {
|
|
387
|
+
eachAxis((axis) => {
|
|
388
|
+
const motionValue = this.getAxisMotionValue(axis);
|
|
402
389
|
if (!motionValue)
|
|
403
390
|
return;
|
|
404
|
-
|
|
391
|
+
this.originPoint[axis] += delta[axis].translate;
|
|
405
392
|
motionValue.set(motionValue.get() + delta[axis].translate);
|
|
406
393
|
});
|
|
407
|
-
|
|
394
|
+
this.visualElement.syncRender();
|
|
408
395
|
}
|
|
409
396
|
}));
|
|
410
|
-
return
|
|
397
|
+
return () => {
|
|
411
398
|
stopResizeListener();
|
|
412
399
|
stopPointerListener();
|
|
413
400
|
stopMeasureLayoutListener();
|
|
414
401
|
stopLayoutUpdateListener === null || stopLayoutUpdateListener === void 0 ? void 0 : stopLayoutUpdateListener();
|
|
415
402
|
};
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
return
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
403
|
+
}
|
|
404
|
+
getProps() {
|
|
405
|
+
const props = this.visualElement.getProps();
|
|
406
|
+
const { drag = false, dragDirectionLock = false, dragPropagation = false, dragConstraints = false, dragElastic = defaultElastic, dragMomentum = true, } = props;
|
|
407
|
+
return Object.assign(Object.assign({}, props), { drag,
|
|
408
|
+
dragDirectionLock,
|
|
409
|
+
dragPropagation,
|
|
410
|
+
dragConstraints,
|
|
411
|
+
dragElastic,
|
|
412
|
+
dragMomentum });
|
|
413
|
+
}
|
|
414
|
+
}
|
|
424
415
|
function shouldDrag(direction, drag, currentDirection) {
|
|
425
416
|
return ((drag === true || drag === direction) &&
|
|
426
417
|
(currentDirection === null || currentDirection === direction));
|
|
@@ -432,9 +423,8 @@ function shouldDrag(direction, drag, currentDirection) {
|
|
|
432
423
|
* @param offset - The x/y offset from origin.
|
|
433
424
|
* @param lockThreshold - (Optional) - the minimum absolute offset before we can determine a drag direction.
|
|
434
425
|
*/
|
|
435
|
-
function getCurrentDirection(offset, lockThreshold) {
|
|
436
|
-
|
|
437
|
-
var direction = null;
|
|
426
|
+
function getCurrentDirection(offset, lockThreshold = 10) {
|
|
427
|
+
let direction = null;
|
|
438
428
|
if (Math.abs(offset.y) > lockThreshold) {
|
|
439
429
|
direction = "y";
|
|
440
430
|
}
|
|
@@ -20,8 +20,8 @@ import { useConstant } from '../../utils/use-constant.mjs';
|
|
|
20
20
|
*
|
|
21
21
|
* @public
|
|
22
22
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
class DragControls {
|
|
24
|
+
constructor() {
|
|
25
25
|
this.componentControls = new Set();
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
@@ -29,11 +29,10 @@ var DragControls = /** @class */ (function () {
|
|
|
29
29
|
*
|
|
30
30
|
* @internal
|
|
31
31
|
*/
|
|
32
|
-
|
|
33
|
-
var _this = this;
|
|
32
|
+
subscribe(controls) {
|
|
34
33
|
this.componentControls.add(controls);
|
|
35
|
-
return
|
|
36
|
-
}
|
|
34
|
+
return () => this.componentControls.delete(controls);
|
|
35
|
+
}
|
|
37
36
|
/**
|
|
38
37
|
* Start a drag gesture on every `motion` component that has this set of drag controls
|
|
39
38
|
* passed into it via the `dragControls` prop.
|
|
@@ -49,14 +48,13 @@ var DragControls = /** @class */ (function () {
|
|
|
49
48
|
*
|
|
50
49
|
* @public
|
|
51
50
|
*/
|
|
52
|
-
|
|
53
|
-
this.componentControls.forEach(
|
|
51
|
+
start(event, options) {
|
|
52
|
+
this.componentControls.forEach((controls) => {
|
|
54
53
|
controls.start(event.nativeEvent || event, options);
|
|
55
54
|
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
var createDragControls = function () { return new DragControls(); };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const createDragControls = () => new DragControls();
|
|
60
58
|
/**
|
|
61
59
|
* Usually, dragging is initiated by pressing down on a `motion` component with a `drag` prop
|
|
62
60
|
* and moving it. For some use-cases, for instance clicking at an arbitrary point on a video scrubber, we
|
|
@@ -8,13 +8,13 @@ import { useConstant } from '../../utils/use-constant.mjs';
|
|
|
8
8
|
* @internal
|
|
9
9
|
*/
|
|
10
10
|
function useDrag(props) {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const { dragControls: groupDragControls, visualElement } = props;
|
|
12
|
+
const dragControls = useConstant(() => new VisualElementDragControls(visualElement));
|
|
13
13
|
// If we've been provided a DragControls for manual control over the drag gesture,
|
|
14
14
|
// subscribe this component to it on mount.
|
|
15
|
-
useEffect(
|
|
15
|
+
useEffect(() => groupDragControls && groupDragControls.subscribe(dragControls), [dragControls, groupDragControls]);
|
|
16
16
|
// Apply the event listeners to the element
|
|
17
|
-
useEffect(
|
|
17
|
+
useEffect(() => dragControls.addListeners(), [dragControls]);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export { useDrag };
|