framer-motion 7.6.4 → 7.6.6
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 +789 -850
- package/dist/es/animation/use-animated-state.mjs +29 -17
- package/dist/es/gestures/drag/VisualElementDragControls.mjs +14 -10
- package/dist/es/gestures/use-focus-gesture.mjs +1 -1
- package/dist/es/gestures/use-tap-gesture.mjs +1 -1
- package/dist/es/index.mjs +1 -1
- package/dist/es/motion/features/viewport/use-viewport.mjs +2 -2
- package/dist/es/motion/utils/use-visual-element.mjs +3 -3
- package/dist/es/projection/node/create-projection-node.mjs +74 -60
- package/dist/es/render/VisualElement.mjs +480 -0
- package/dist/es/render/dom/DOMVisualElement.mjs +49 -0
- package/dist/es/render/dom/create-visual-element.mjs +4 -4
- package/dist/es/render/dom/utils/css-variables-conversion.mjs +2 -2
- package/dist/es/render/dom/utils/unit-conversion.mjs +4 -4
- package/dist/es/render/html/HTMLVisualElement.mjs +41 -0
- package/dist/es/render/svg/{visual-element.mjs → SVGVisualElement.mjs} +21 -15
- package/dist/es/render/utils/animation.mjs +4 -4
- package/dist/es/render/utils/motion-values.mjs +1 -1
- package/dist/es/render/utils/resolve-dynamic-variants.mjs +2 -2
- package/dist/es/render/utils/setters.mjs +2 -1
- package/dist/es/value/index.mjs +1 -1
- package/dist/framer-motion.dev.js +789 -850
- package/dist/framer-motion.js +1 -1
- package/dist/index.d.ts +2101 -1931
- package/dist/projection.dev.js +1053 -1136
- package/dist/size-rollup-dom-animation-m.js +1 -1
- package/dist/size-rollup-dom-animation.js +1 -1
- package/dist/size-rollup-dom-max-assets.js +1 -1
- package/dist/size-rollup-dom-max.js +1 -1
- package/dist/size-rollup-m.js +1 -1
- package/dist/size-rollup-motion.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 +1956 -1745
- package/package.json +8 -8
- package/dist/es/render/html/visual-element.mjs +0 -109
- package/dist/es/render/index.mjs +0 -515
- package/dist/es/render/utils/lifecycles.mjs +0 -43
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
import sync, { cancelSync } from 'framesync';
|
|
2
|
+
import { invariant } from 'hey-listen';
|
|
3
|
+
import { createElement } from 'react';
|
|
4
|
+
import { featureDefinitions } from '../motion/features/definitions.mjs';
|
|
5
|
+
import { createBox } from '../projection/geometry/models.mjs';
|
|
6
|
+
import { isRefObject } from '../utils/is-ref-object.mjs';
|
|
7
|
+
import { env } from '../utils/process.mjs';
|
|
8
|
+
import { initPrefersReducedMotion } from '../utils/reduced-motion/index.mjs';
|
|
9
|
+
import { hasReducedMotionListener, prefersReducedMotion } from '../utils/reduced-motion/state.mjs';
|
|
10
|
+
import { SubscriptionManager } from '../utils/subscription-manager.mjs';
|
|
11
|
+
import { motionValue } from '../value/index.mjs';
|
|
12
|
+
import { isWillChangeMotionValue } from '../value/use-will-change/is.mjs';
|
|
13
|
+
import { isMotionValue } from '../value/utils/is-motion-value.mjs';
|
|
14
|
+
import { variantPriorityOrder } from './utils/animation-state.mjs';
|
|
15
|
+
import { isControllingVariants, isVariantNode } from './utils/is-controlling-variants.mjs';
|
|
16
|
+
import { isVariantLabel } from './utils/is-variant-label.mjs';
|
|
17
|
+
import { updateMotionValuesFromProps } from './utils/motion-values.mjs';
|
|
18
|
+
import { resolveVariantFromProps } from './utils/resolve-variants.mjs';
|
|
19
|
+
|
|
20
|
+
const featureNames = Object.keys(featureDefinitions);
|
|
21
|
+
const numFeatures = featureNames.length;
|
|
22
|
+
const propEventHandlers = [
|
|
23
|
+
"AnimationStart",
|
|
24
|
+
"AnimationComplete",
|
|
25
|
+
"Update",
|
|
26
|
+
"Unmount",
|
|
27
|
+
"BeforeLayoutMeasure",
|
|
28
|
+
"LayoutMeasure",
|
|
29
|
+
"LayoutAnimationStart",
|
|
30
|
+
"LayoutAnimationComplete",
|
|
31
|
+
];
|
|
32
|
+
/**
|
|
33
|
+
* A VisualElement is an imperative abstraction around UI elements such as
|
|
34
|
+
* HTMLElement, SVGElement, Three.Object3D etc.
|
|
35
|
+
*/
|
|
36
|
+
class VisualElement {
|
|
37
|
+
constructor({ parent, props, reducedMotionConfig, visualState, }, options = {}) {
|
|
38
|
+
/**
|
|
39
|
+
* A reference to the current underlying Instance, e.g. a HTMLElement
|
|
40
|
+
* or Three.Mesh etc.
|
|
41
|
+
*/
|
|
42
|
+
this.current = null;
|
|
43
|
+
/**
|
|
44
|
+
* A set containing references to this VisualElement's children.
|
|
45
|
+
*/
|
|
46
|
+
this.children = new Set();
|
|
47
|
+
/**
|
|
48
|
+
* Determine what role this visual element should take in the variant tree.
|
|
49
|
+
*/
|
|
50
|
+
this.isVariantNode = false;
|
|
51
|
+
this.isControllingVariants = false;
|
|
52
|
+
/**
|
|
53
|
+
* Decides whether this VisualElement should animate in reduced motion
|
|
54
|
+
* mode.
|
|
55
|
+
*
|
|
56
|
+
* TODO: This is currently set on every individual VisualElement but feels
|
|
57
|
+
* like it could be set globally.
|
|
58
|
+
*/
|
|
59
|
+
this.shouldReduceMotion = null;
|
|
60
|
+
/**
|
|
61
|
+
* A map of all motion values attached to this visual element. Motion
|
|
62
|
+
* values are source of truth for any given animated value. A motion
|
|
63
|
+
* value might be provided externally by the component via props.
|
|
64
|
+
*/
|
|
65
|
+
this.values = new Map();
|
|
66
|
+
/**
|
|
67
|
+
* Tracks whether this VisualElement's React component is currently present
|
|
68
|
+
* within the defined React tree.
|
|
69
|
+
*/
|
|
70
|
+
this.isPresent = true;
|
|
71
|
+
/**
|
|
72
|
+
* A map of every subscription that binds the provided or generated
|
|
73
|
+
* motion values onChange listeners to this visual element.
|
|
74
|
+
*/
|
|
75
|
+
this.valueSubscriptions = new Map();
|
|
76
|
+
/**
|
|
77
|
+
* A reference to the previously-provided motion values as returned
|
|
78
|
+
* from scrapeMotionValuesFromProps. We use the keys in here to determine
|
|
79
|
+
* if any motion values need to be removed after props are updated.
|
|
80
|
+
*/
|
|
81
|
+
this.prevMotionValues = {};
|
|
82
|
+
/**
|
|
83
|
+
* An object containing a SubscriptionManager for each active event.
|
|
84
|
+
*/
|
|
85
|
+
this.events = {};
|
|
86
|
+
/**
|
|
87
|
+
* An object containing an unsubscribe function for each prop event subscription.
|
|
88
|
+
* For example, every "Update" event can have multiple subscribers via
|
|
89
|
+
* VisualElement.on(), but only one of those can be defined via the onUpdate prop.
|
|
90
|
+
*/
|
|
91
|
+
this.propEventSubscriptions = {};
|
|
92
|
+
this.notifyUpdate = () => this.notify("Update", this.latestValues);
|
|
93
|
+
this.render = () => {
|
|
94
|
+
if (!this.current)
|
|
95
|
+
return;
|
|
96
|
+
this.triggerBuild();
|
|
97
|
+
this.renderInstance(this.current, this.renderState, this.props.style, this.projection);
|
|
98
|
+
};
|
|
99
|
+
this.scheduleRender = () => sync.render(this.render, false, true);
|
|
100
|
+
const { latestValues, renderState } = visualState;
|
|
101
|
+
this.latestValues = latestValues;
|
|
102
|
+
this.baseTarget = { ...latestValues };
|
|
103
|
+
this.initialValues = props.initial ? { ...latestValues } : {};
|
|
104
|
+
this.renderState = renderState;
|
|
105
|
+
this.parent = parent;
|
|
106
|
+
this.props = props;
|
|
107
|
+
this.depth = parent ? parent.depth + 1 : 0;
|
|
108
|
+
this.reducedMotionConfig = reducedMotionConfig;
|
|
109
|
+
this.options = options;
|
|
110
|
+
this.isControllingVariants = isControllingVariants(props);
|
|
111
|
+
this.isVariantNode = isVariantNode(props);
|
|
112
|
+
if (this.isVariantNode) {
|
|
113
|
+
this.variantChildren = new Set();
|
|
114
|
+
}
|
|
115
|
+
this.manuallyAnimateOnMount = Boolean(parent && parent.current);
|
|
116
|
+
/**
|
|
117
|
+
* Any motion values that are provided to the element when created
|
|
118
|
+
* aren't yet bound to the element, as this would technically be impure.
|
|
119
|
+
* However, we iterate through the motion values and set them to the
|
|
120
|
+
* initial values for this component.
|
|
121
|
+
*
|
|
122
|
+
* TODO: This is impure and we should look at changing this to run on mount.
|
|
123
|
+
* Doing so will break some tests but this isn't neccessarily a breaking change,
|
|
124
|
+
* more a reflection of the test.
|
|
125
|
+
*/
|
|
126
|
+
const { willChange, ...initialMotionValues } = this.scrapeMotionValuesFromProps(props);
|
|
127
|
+
for (const key in initialMotionValues) {
|
|
128
|
+
const value = initialMotionValues[key];
|
|
129
|
+
if (latestValues[key] !== undefined && isMotionValue(value)) {
|
|
130
|
+
value.set(latestValues[key], false);
|
|
131
|
+
if (isWillChangeMotionValue(willChange)) {
|
|
132
|
+
willChange.add(key);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Update external values with initial values
|
|
138
|
+
*/
|
|
139
|
+
if (props.values) {
|
|
140
|
+
for (const key in props.values) {
|
|
141
|
+
const value = props.values[key];
|
|
142
|
+
if (latestValues[key] !== undefined && isMotionValue(value)) {
|
|
143
|
+
value.set(latestValues[key]);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* This method takes React props and returns found MotionValues. For example, HTML
|
|
150
|
+
* MotionValues will be found within the style prop, whereas for Three.js within attribute arrays.
|
|
151
|
+
*
|
|
152
|
+
* This isn't an abstract method as it needs calling in the constructor, but it is
|
|
153
|
+
* intended to be one.
|
|
154
|
+
*/
|
|
155
|
+
scrapeMotionValuesFromProps(_props) {
|
|
156
|
+
return {};
|
|
157
|
+
}
|
|
158
|
+
mount(instance) {
|
|
159
|
+
var _a;
|
|
160
|
+
this.current = instance;
|
|
161
|
+
if (this.projection) {
|
|
162
|
+
this.projection.mount(instance);
|
|
163
|
+
}
|
|
164
|
+
if (this.parent && this.isVariantNode && !this.isControllingVariants) {
|
|
165
|
+
this.removeFromVariantTree = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.addVariantChild(this);
|
|
166
|
+
}
|
|
167
|
+
this.values.forEach((value, key) => this.bindToMotionValue(key, value));
|
|
168
|
+
if (!hasReducedMotionListener.current) {
|
|
169
|
+
initPrefersReducedMotion();
|
|
170
|
+
}
|
|
171
|
+
this.shouldReduceMotion =
|
|
172
|
+
this.reducedMotionConfig === "never"
|
|
173
|
+
? false
|
|
174
|
+
: this.reducedMotionConfig === "always"
|
|
175
|
+
? true
|
|
176
|
+
: prefersReducedMotion.current;
|
|
177
|
+
if (this.parent)
|
|
178
|
+
this.parent.children.add(this);
|
|
179
|
+
this.setProps(this.props);
|
|
180
|
+
}
|
|
181
|
+
unmount() {
|
|
182
|
+
var _a, _b, _c;
|
|
183
|
+
(_a = this.projection) === null || _a === void 0 ? void 0 : _a.unmount();
|
|
184
|
+
cancelSync.update(this.notifyUpdate);
|
|
185
|
+
cancelSync.render(this.render);
|
|
186
|
+
this.valueSubscriptions.forEach((remove) => remove());
|
|
187
|
+
(_b = this.removeFromVariantTree) === null || _b === void 0 ? void 0 : _b.call(this);
|
|
188
|
+
(_c = this.parent) === null || _c === void 0 ? void 0 : _c.children.delete(this);
|
|
189
|
+
for (const key in this.events) {
|
|
190
|
+
this.events[key].clear();
|
|
191
|
+
}
|
|
192
|
+
this.current = null;
|
|
193
|
+
}
|
|
194
|
+
bindToMotionValue(key, value) {
|
|
195
|
+
const removeOnChange = value.onChange((latestValue) => {
|
|
196
|
+
this.latestValues[key] = latestValue;
|
|
197
|
+
this.props.onUpdate &&
|
|
198
|
+
sync.update(this.notifyUpdate, false, true);
|
|
199
|
+
});
|
|
200
|
+
const removeOnRenderRequest = value.onRenderRequest(this.scheduleRender);
|
|
201
|
+
this.valueSubscriptions.set(key, () => {
|
|
202
|
+
removeOnChange();
|
|
203
|
+
removeOnRenderRequest();
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
sortNodePosition(other) {
|
|
207
|
+
/**
|
|
208
|
+
* If these nodes aren't even of the same type we can't compare their depth.
|
|
209
|
+
*/
|
|
210
|
+
if (!this.current ||
|
|
211
|
+
!this.sortInstanceNodePosition ||
|
|
212
|
+
this.type !== other.type)
|
|
213
|
+
return 0;
|
|
214
|
+
return this.sortInstanceNodePosition(this.current, other.current);
|
|
215
|
+
}
|
|
216
|
+
loadFeatures(renderedProps, isStrict, preloadedFeatures, projectionId, ProjectionNodeConstructor, initialLayoutGroupConfig) {
|
|
217
|
+
const features = [];
|
|
218
|
+
/**
|
|
219
|
+
* If we're in development mode, check to make sure we're not rendering a motion component
|
|
220
|
+
* as a child of LazyMotion, as this will break the file-size benefits of using it.
|
|
221
|
+
*/
|
|
222
|
+
if (env !== "production" && preloadedFeatures && isStrict) {
|
|
223
|
+
invariant(false, "You have rendered a `motion` component within a `LazyMotion` component. This will break tree shaking. Import and render a `m` component instead.");
|
|
224
|
+
}
|
|
225
|
+
for (let i = 0; i < numFeatures; i++) {
|
|
226
|
+
const name = featureNames[i];
|
|
227
|
+
const { isEnabled, Component } = featureDefinitions[name];
|
|
228
|
+
/**
|
|
229
|
+
* It might be possible in the future to use this moment to
|
|
230
|
+
* dynamically request functionality. In initial tests this
|
|
231
|
+
* was producing a lot of duplication amongst bundles.
|
|
232
|
+
*/
|
|
233
|
+
if (isEnabled(renderedProps) && Component) {
|
|
234
|
+
features.push(createElement(Component, {
|
|
235
|
+
key: name,
|
|
236
|
+
...renderedProps,
|
|
237
|
+
visualElement: this,
|
|
238
|
+
}));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (!this.projection && ProjectionNodeConstructor) {
|
|
242
|
+
this.projection = new ProjectionNodeConstructor(projectionId, this.latestValues, this.parent && this.parent.projection);
|
|
243
|
+
const { layoutId, layout, drag, dragConstraints, layoutScroll } = renderedProps;
|
|
244
|
+
this.projection.setOptions({
|
|
245
|
+
layoutId,
|
|
246
|
+
layout,
|
|
247
|
+
alwaysMeasureLayout: Boolean(drag) ||
|
|
248
|
+
(dragConstraints && isRefObject(dragConstraints)),
|
|
249
|
+
visualElement: this,
|
|
250
|
+
scheduleRender: () => this.scheduleRender(),
|
|
251
|
+
/**
|
|
252
|
+
* TODO: Update options in an effect. This could be tricky as it'll be too late
|
|
253
|
+
* to update by the time layout animations run.
|
|
254
|
+
* We also need to fix this safeToRemove by linking it up to the one returned by usePresence,
|
|
255
|
+
* ensuring it gets called if there's no potential layout animations.
|
|
256
|
+
*
|
|
257
|
+
*/
|
|
258
|
+
animationType: typeof layout === "string" ? layout : "both",
|
|
259
|
+
initialPromotionConfig: initialLayoutGroupConfig,
|
|
260
|
+
layoutScroll,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
return features;
|
|
264
|
+
}
|
|
265
|
+
triggerBuild() {
|
|
266
|
+
this.build(this.renderState, this.latestValues, this.options, this.props);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Measure the current viewport box with or without transforms.
|
|
270
|
+
* Only measures axis-aligned boxes, rotate and skew must be manually
|
|
271
|
+
* removed with a re-render to work.
|
|
272
|
+
*/
|
|
273
|
+
measureViewportBox() {
|
|
274
|
+
return this.current
|
|
275
|
+
? this.measureInstanceViewportBox(this.current, this.props)
|
|
276
|
+
: createBox();
|
|
277
|
+
}
|
|
278
|
+
getStaticValue(key) {
|
|
279
|
+
return this.latestValues[key];
|
|
280
|
+
}
|
|
281
|
+
setStaticValue(key, value) {
|
|
282
|
+
this.latestValues[key] = value;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Make a target animatable by Popmotion. For instance, if we're
|
|
286
|
+
* trying to animate width from 100px to 100vw we need to measure 100vw
|
|
287
|
+
* in pixels to determine what we really need to animate to. This is also
|
|
288
|
+
* pluggable to support Framer's custom value types like Color,
|
|
289
|
+
* and CSS variables.
|
|
290
|
+
*/
|
|
291
|
+
makeTargetAnimatable(target, canMutate = true) {
|
|
292
|
+
return this.makeTargetAnimatableFromInstance(target, this.props, canMutate);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Update the provided props. Ensure any newly-added motion values are
|
|
296
|
+
* added to our map, old ones removed, and listeners updated.
|
|
297
|
+
*/
|
|
298
|
+
setProps(props) {
|
|
299
|
+
if (props.transformTemplate || this.props.transformTemplate) {
|
|
300
|
+
this.scheduleRender();
|
|
301
|
+
}
|
|
302
|
+
this.props = props;
|
|
303
|
+
/**
|
|
304
|
+
* Update prop event handlers ie onAnimationStart, onAnimationComplete
|
|
305
|
+
*/
|
|
306
|
+
for (let i = 0; i < propEventHandlers.length; i++) {
|
|
307
|
+
const key = propEventHandlers[i];
|
|
308
|
+
if (this.propEventSubscriptions[key]) {
|
|
309
|
+
this.propEventSubscriptions[key]();
|
|
310
|
+
delete this.propEventSubscriptions[key];
|
|
311
|
+
}
|
|
312
|
+
const listener = props["on" + key];
|
|
313
|
+
if (listener) {
|
|
314
|
+
this.propEventSubscriptions[key] = this.on(key, listener);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
this.prevMotionValues = updateMotionValuesFromProps(this, this.scrapeMotionValuesFromProps(props), this.prevMotionValues);
|
|
318
|
+
}
|
|
319
|
+
getProps() {
|
|
320
|
+
return this.props;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Returns the variant definition with a given name.
|
|
324
|
+
*/
|
|
325
|
+
getVariant(name) {
|
|
326
|
+
var _a;
|
|
327
|
+
return (_a = this.props.variants) === null || _a === void 0 ? void 0 : _a[name];
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Returns the defined default transition on this component.
|
|
331
|
+
*/
|
|
332
|
+
getDefaultTransition() {
|
|
333
|
+
return this.props.transition;
|
|
334
|
+
}
|
|
335
|
+
getTransformPagePoint() {
|
|
336
|
+
return this.props.transformPagePoint;
|
|
337
|
+
}
|
|
338
|
+
getClosestVariantNode() {
|
|
339
|
+
var _a;
|
|
340
|
+
return this.isVariantNode ? this : (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getClosestVariantNode();
|
|
341
|
+
}
|
|
342
|
+
getVariantContext(startAtParent = false) {
|
|
343
|
+
var _a, _b;
|
|
344
|
+
if (startAtParent)
|
|
345
|
+
return (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getVariantContext();
|
|
346
|
+
if (!this.isControllingVariants) {
|
|
347
|
+
const context = ((_b = this.parent) === null || _b === void 0 ? void 0 : _b.getVariantContext()) || {};
|
|
348
|
+
if (this.props.initial !== undefined) {
|
|
349
|
+
context.initial = this.props.initial;
|
|
350
|
+
}
|
|
351
|
+
return context;
|
|
352
|
+
}
|
|
353
|
+
const context = {};
|
|
354
|
+
for (let i = 0; i < numVariantProps; i++) {
|
|
355
|
+
const name = variantProps[i];
|
|
356
|
+
const prop = this.props[name];
|
|
357
|
+
if (isVariantLabel(prop) || prop === false) {
|
|
358
|
+
context[name] = prop;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return context;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Add a child visual element to our set of children.
|
|
365
|
+
*/
|
|
366
|
+
addVariantChild(child) {
|
|
367
|
+
var _a;
|
|
368
|
+
const closestVariantNode = this.getClosestVariantNode();
|
|
369
|
+
if (closestVariantNode) {
|
|
370
|
+
(_a = closestVariantNode.variantChildren) === null || _a === void 0 ? void 0 : _a.add(child);
|
|
371
|
+
return () => closestVariantNode.variantChildren.delete(child);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Add a motion value and bind it to this visual element.
|
|
376
|
+
*/
|
|
377
|
+
addValue(key, value) {
|
|
378
|
+
// Remove existing value if it exists
|
|
379
|
+
if (this.hasValue(key))
|
|
380
|
+
this.removeValue(key);
|
|
381
|
+
this.values.set(key, value);
|
|
382
|
+
this.latestValues[key] = value.get();
|
|
383
|
+
this.bindToMotionValue(key, value);
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Remove a motion value and unbind any active subscriptions.
|
|
387
|
+
*/
|
|
388
|
+
removeValue(key) {
|
|
389
|
+
var _a;
|
|
390
|
+
this.values.delete(key);
|
|
391
|
+
(_a = this.valueSubscriptions.get(key)) === null || _a === void 0 ? void 0 : _a();
|
|
392
|
+
this.valueSubscriptions.delete(key);
|
|
393
|
+
delete this.latestValues[key];
|
|
394
|
+
this.removeValueFromRenderState(key, this.renderState);
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Check whether we have a motion value for this key
|
|
398
|
+
*/
|
|
399
|
+
hasValue(key) {
|
|
400
|
+
return this.values.has(key);
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Get a motion value for this key. If called with a default
|
|
404
|
+
* value, we'll create one if none exists.
|
|
405
|
+
*/
|
|
406
|
+
getValue(key, defaultValue) {
|
|
407
|
+
if (this.props.values && this.props.values[key]) {
|
|
408
|
+
return this.props.values[key];
|
|
409
|
+
}
|
|
410
|
+
let value = this.values.get(key);
|
|
411
|
+
if (value === undefined && defaultValue !== undefined) {
|
|
412
|
+
value = motionValue(defaultValue);
|
|
413
|
+
this.addValue(key, value);
|
|
414
|
+
}
|
|
415
|
+
return value;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* If we're trying to animate to a previously unencountered value,
|
|
419
|
+
* we need to check for it in our state and as a last resort read it
|
|
420
|
+
* directly from the instance (which might have performance implications).
|
|
421
|
+
*/
|
|
422
|
+
readValue(key) {
|
|
423
|
+
return this.latestValues[key] !== undefined || !this.current
|
|
424
|
+
? this.latestValues[key]
|
|
425
|
+
: this.readValueFromInstance(this.current, key, this.options);
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Set the base target to later animate back to. This is currently
|
|
429
|
+
* only hydrated on creation and when we first read a value.
|
|
430
|
+
*/
|
|
431
|
+
setBaseTarget(key, value) {
|
|
432
|
+
this.baseTarget[key] = value;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Find the base target for a value thats been removed from all animation
|
|
436
|
+
* props.
|
|
437
|
+
*/
|
|
438
|
+
getBaseTarget(key) {
|
|
439
|
+
var _a;
|
|
440
|
+
const { initial } = this.props;
|
|
441
|
+
const valueFromInitial = typeof initial === "string" || typeof initial === "object"
|
|
442
|
+
? (_a = resolveVariantFromProps(this.props, initial)) === null || _a === void 0 ? void 0 : _a[key]
|
|
443
|
+
: undefined;
|
|
444
|
+
/**
|
|
445
|
+
* If this value still exists in the current initial variant, read that.
|
|
446
|
+
*/
|
|
447
|
+
if (initial && valueFromInitial !== undefined) {
|
|
448
|
+
return valueFromInitial;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Alternatively, if this VisualElement config has defined a getBaseTarget
|
|
452
|
+
* so we can read the value from an alternative source, try that.
|
|
453
|
+
*/
|
|
454
|
+
const target = this.getBaseTargetFromProps(this.props, key);
|
|
455
|
+
if (target !== undefined && !isMotionValue(target))
|
|
456
|
+
return target;
|
|
457
|
+
/**
|
|
458
|
+
* If the value was initially defined on initial, but it doesn't any more,
|
|
459
|
+
* return undefined. Otherwise return the value as initially read from the DOM.
|
|
460
|
+
*/
|
|
461
|
+
return this.initialValues[key] !== undefined &&
|
|
462
|
+
valueFromInitial === undefined
|
|
463
|
+
? undefined
|
|
464
|
+
: this.baseTarget[key];
|
|
465
|
+
}
|
|
466
|
+
on(eventName, callback) {
|
|
467
|
+
if (!this.events[eventName]) {
|
|
468
|
+
this.events[eventName] = new SubscriptionManager();
|
|
469
|
+
}
|
|
470
|
+
return this.events[eventName].add(callback);
|
|
471
|
+
}
|
|
472
|
+
notify(eventName, ...args) {
|
|
473
|
+
var _a;
|
|
474
|
+
(_a = this.events[eventName]) === null || _a === void 0 ? void 0 : _a.notify(...args);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const variantProps = ["initial", ...variantPriorityOrder];
|
|
478
|
+
const numVariantProps = variantProps.length;
|
|
479
|
+
|
|
480
|
+
export { VisualElement };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getOrigin, checkTargetForNewValues } from '../utils/setters.mjs';
|
|
2
|
+
import { parseDomVariant } from './utils/parse-dom-variant.mjs';
|
|
3
|
+
import { VisualElement } from '../VisualElement.mjs';
|
|
4
|
+
|
|
5
|
+
class DOMVisualElement extends VisualElement {
|
|
6
|
+
sortInstanceNodePosition(a, b) {
|
|
7
|
+
/**
|
|
8
|
+
* compareDocumentPosition returns a bitmask, by using the bitwise &
|
|
9
|
+
* we're returning true if 2 in that bitmask is set to true. 2 is set
|
|
10
|
+
* to true if b preceeds a.
|
|
11
|
+
*/
|
|
12
|
+
return a.compareDocumentPosition(b) & 2 ? 1 : -1;
|
|
13
|
+
}
|
|
14
|
+
getBaseTargetFromProps(props, key) {
|
|
15
|
+
var _a;
|
|
16
|
+
return (_a = props.style) === null || _a === void 0 ? void 0 : _a[key];
|
|
17
|
+
}
|
|
18
|
+
removeValueFromRenderState(key, { vars, style }) {
|
|
19
|
+
delete vars[key];
|
|
20
|
+
delete style[key];
|
|
21
|
+
}
|
|
22
|
+
makeTargetAnimatableFromInstance({ transition, transitionEnd, ...target }, { transformValues }, isMounted) {
|
|
23
|
+
let origin = getOrigin(target, transition || {}, this);
|
|
24
|
+
/**
|
|
25
|
+
* If Framer has provided a function to convert `Color` etc value types, convert them
|
|
26
|
+
*/
|
|
27
|
+
if (transformValues) {
|
|
28
|
+
if (transitionEnd)
|
|
29
|
+
transitionEnd = transformValues(transitionEnd);
|
|
30
|
+
if (target)
|
|
31
|
+
target = transformValues(target);
|
|
32
|
+
if (origin)
|
|
33
|
+
origin = transformValues(origin);
|
|
34
|
+
}
|
|
35
|
+
if (isMounted) {
|
|
36
|
+
checkTargetForNewValues(this, target, origin);
|
|
37
|
+
const parsed = parseDomVariant(this, target, origin, transitionEnd);
|
|
38
|
+
transitionEnd = parsed.transitionEnd;
|
|
39
|
+
target = parsed.target;
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
transition,
|
|
43
|
+
transitionEnd,
|
|
44
|
+
...target,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { DOMVisualElement };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { HTMLVisualElement } from '../html/HTMLVisualElement.mjs';
|
|
2
|
+
import { SVGVisualElement } from '../svg/SVGVisualElement.mjs';
|
|
3
3
|
import { isSVGComponent } from './utils/is-svg-component.mjs';
|
|
4
4
|
|
|
5
5
|
const createDomVisualElement = (Component, options) => {
|
|
6
6
|
return isSVGComponent(Component)
|
|
7
|
-
?
|
|
8
|
-
:
|
|
7
|
+
? new SVGVisualElement(options, { enableHardwareAcceleration: false })
|
|
8
|
+
: new HTMLVisualElement(options, { enableHardwareAcceleration: true });
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
export { createDomVisualElement };
|
|
@@ -46,7 +46,7 @@ function getVariableValue(current, element, depth = 1) {
|
|
|
46
46
|
* @internal
|
|
47
47
|
*/
|
|
48
48
|
function resolveCSSVariables(visualElement, { ...target }, transitionEnd) {
|
|
49
|
-
const element = visualElement.
|
|
49
|
+
const element = visualElement.current;
|
|
50
50
|
if (!(element instanceof Element))
|
|
51
51
|
return { target, transitionEnd };
|
|
52
52
|
// If `transitionEnd` isn't `undefined`, clone it. We could clone `target` and `transitionEnd`
|
|
@@ -55,7 +55,7 @@ function resolveCSSVariables(visualElement, { ...target }, transitionEnd) {
|
|
|
55
55
|
transitionEnd = { ...transitionEnd };
|
|
56
56
|
}
|
|
57
57
|
// Go through existing `MotionValue`s and ensure any existing CSS variables are resolved
|
|
58
|
-
visualElement.
|
|
58
|
+
visualElement.values.forEach((value) => {
|
|
59
59
|
const current = value.get();
|
|
60
60
|
if (!isCSSVariable(current))
|
|
61
61
|
return;
|
|
@@ -66,7 +66,7 @@ function removeNonTranslationalTransform(visualElement) {
|
|
|
66
66
|
});
|
|
67
67
|
// Apply changes to element before measurement
|
|
68
68
|
if (removedTransforms.length)
|
|
69
|
-
visualElement.
|
|
69
|
+
visualElement.render();
|
|
70
70
|
return removedTransforms;
|
|
71
71
|
}
|
|
72
72
|
const positionalValues = {
|
|
@@ -83,7 +83,7 @@ const positionalValues = {
|
|
|
83
83
|
};
|
|
84
84
|
const convertChangedValueTypes = (target, visualElement, changedKeys) => {
|
|
85
85
|
const originBbox = visualElement.measureViewportBox();
|
|
86
|
-
const element = visualElement.
|
|
86
|
+
const element = visualElement.current;
|
|
87
87
|
const elementComputedStyle = getComputedStyle(element);
|
|
88
88
|
const { display } = elementComputedStyle;
|
|
89
89
|
const origin = {};
|
|
@@ -99,7 +99,7 @@ const convertChangedValueTypes = (target, visualElement, changedKeys) => {
|
|
|
99
99
|
origin[key] = positionalValues[key](originBbox, elementComputedStyle);
|
|
100
100
|
});
|
|
101
101
|
// Apply the latest values (as set in checkAndConvertChangedValueTypes)
|
|
102
|
-
visualElement.
|
|
102
|
+
visualElement.render();
|
|
103
103
|
const targetBbox = visualElement.measureViewportBox();
|
|
104
104
|
changedKeys.forEach((key) => {
|
|
105
105
|
// Restore styles to their **calculated computed style**, not their actual
|
|
@@ -206,7 +206,7 @@ const checkAndConvertChangedValueTypes = (visualElement, target, origin = {}, tr
|
|
|
206
206
|
});
|
|
207
207
|
}
|
|
208
208
|
// Reapply original values
|
|
209
|
-
visualElement.
|
|
209
|
+
visualElement.render();
|
|
210
210
|
// Restore scroll position
|
|
211
211
|
if (isBrowser && scrollY !== null) {
|
|
212
212
|
window.scrollTo({ top: scrollY });
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { buildHTMLStyles } from './utils/build-styles.mjs';
|
|
2
|
+
import { isCSSVariable } from '../dom/utils/is-css-variable.mjs';
|
|
3
|
+
import { transformProps } from './utils/transform.mjs';
|
|
4
|
+
import { scrapeMotionValuesFromProps } from './utils/scrape-motion-values.mjs';
|
|
5
|
+
import { renderHTML } from './utils/render.mjs';
|
|
6
|
+
import { getDefaultValueType } from '../dom/value-types/defaults.mjs';
|
|
7
|
+
import { measureViewportBox } from '../../projection/utils/measure.mjs';
|
|
8
|
+
import { DOMVisualElement } from '../dom/DOMVisualElement.mjs';
|
|
9
|
+
|
|
10
|
+
function getComputedStyle(element) {
|
|
11
|
+
return window.getComputedStyle(element);
|
|
12
|
+
}
|
|
13
|
+
class HTMLVisualElement extends DOMVisualElement {
|
|
14
|
+
readValueFromInstance(instance, key) {
|
|
15
|
+
if (transformProps.has(key)) {
|
|
16
|
+
const defaultType = getDefaultValueType(key);
|
|
17
|
+
return defaultType ? defaultType.default || 0 : 0;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
const computedStyle = getComputedStyle(instance);
|
|
21
|
+
const value = (isCSSVariable(key)
|
|
22
|
+
? computedStyle.getPropertyValue(key)
|
|
23
|
+
: computedStyle[key]) || 0;
|
|
24
|
+
return typeof value === "string" ? value.trim() : value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
measureInstanceViewportBox(instance, { transformPagePoint }) {
|
|
28
|
+
return measureViewportBox(instance, transformPagePoint);
|
|
29
|
+
}
|
|
30
|
+
build(renderState, latestValues, options, props) {
|
|
31
|
+
buildHTMLStyles(renderState, latestValues, options, props.transformTemplate);
|
|
32
|
+
}
|
|
33
|
+
scrapeMotionValuesFromProps(props) {
|
|
34
|
+
return scrapeMotionValuesFromProps(props);
|
|
35
|
+
}
|
|
36
|
+
renderInstance(instance, renderState, styleProp, projection) {
|
|
37
|
+
renderHTML(instance, renderState, styleProp, projection);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { HTMLVisualElement, getComputedStyle };
|