motion-dom 12.24.10 → 12.26.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 +6214 -1836
- package/dist/es/animation/NativeAnimation.mjs +10 -2
- package/dist/es/animation/NativeAnimationExtended.mjs +11 -4
- package/dist/es/animation/animate/single-value.mjs +11 -0
- package/dist/es/animation/interfaces/motion-value.mjs +102 -0
- package/dist/es/animation/interfaces/visual-element-target.mjs +85 -0
- package/dist/es/animation/interfaces/visual-element-variant.mjs +59 -0
- package/dist/es/animation/interfaces/visual-element.mjs +26 -0
- package/dist/es/animation/optimized-appear/data-id.mjs +6 -0
- package/dist/es/animation/optimized-appear/get-appear-id.mjs +7 -0
- package/dist/es/animation/utils/calc-child-stagger.mjs +15 -0
- package/dist/es/animation/utils/default-transitions.mjs +40 -0
- package/dist/es/animation/utils/get-final-keyframe.mjs +12 -0
- package/dist/es/animation/utils/is-transition-defined.mjs +10 -0
- package/dist/es/events/add-dom-event.mjs +6 -0
- package/dist/es/index.mjs +71 -0
- package/dist/es/projection/animation/mix-values.mjs +66 -0
- package/dist/es/projection/geometry/conversion.mjs +33 -0
- package/dist/es/projection/geometry/copy.mjs +31 -0
- package/dist/es/projection/geometry/delta-apply.mjs +119 -0
- package/dist/es/projection/geometry/delta-calc.mjs +52 -0
- package/dist/es/projection/geometry/delta-remove.mjs +54 -0
- package/dist/es/projection/geometry/models.mjs +17 -0
- package/dist/es/projection/geometry/utils.mjs +31 -0
- package/dist/es/projection/node/DocumentProjectionNode.mjs +13 -0
- package/dist/es/projection/node/HTMLProjectionNode.mjs +27 -0
- package/dist/es/projection/node/create-projection-node.mjs +1628 -0
- package/dist/es/projection/node/group.mjs +24 -0
- package/dist/es/projection/node/state.mjs +19 -0
- package/dist/es/projection/shared/stack.mjs +100 -0
- package/dist/es/projection/styles/scale-border-radius.mjs +41 -0
- package/dist/es/projection/styles/scale-box-shadow.mjs +35 -0
- package/dist/es/projection/styles/scale-correction.mjs +30 -0
- package/dist/es/projection/styles/transform.mjs +49 -0
- package/dist/es/projection/utils/compare-by-depth.mjs +3 -0
- package/dist/es/projection/utils/each-axis.mjs +5 -0
- package/dist/es/projection/utils/flat-tree.mjs +24 -0
- package/dist/es/projection/utils/has-transform.mjs +26 -0
- package/dist/es/projection/utils/measure.mjs +17 -0
- package/dist/es/render/Feature.mjs +14 -0
- package/dist/es/render/VisualElement.mjs +510 -0
- package/dist/es/render/dom/DOMVisualElement.mjs +42 -0
- package/dist/es/render/html/HTMLVisualElement.mjs +44 -0
- package/dist/es/render/html/utils/build-styles.mjs +65 -0
- package/dist/es/render/html/utils/build-transform.mjs +62 -0
- package/dist/es/render/html/utils/render.mjs +17 -0
- package/dist/es/render/html/utils/scrape-motion-values.mjs +21 -0
- package/dist/es/render/object/ObjectVisualElement.mjs +41 -0
- package/dist/es/render/store.mjs +3 -0
- package/dist/es/render/svg/SVGVisualElement.mjs +45 -0
- package/dist/es/render/svg/utils/build-attrs.mjs +72 -0
- package/dist/es/render/svg/utils/camel-case-attrs.mjs +30 -0
- package/dist/es/render/svg/utils/is-svg-tag.mjs +3 -0
- package/dist/es/render/svg/utils/path.mjs +32 -0
- package/dist/es/render/svg/utils/render.mjs +12 -0
- package/dist/es/render/svg/utils/scrape-motion-values.mjs +19 -0
- package/dist/es/render/utils/animation-state.mjs +364 -0
- package/dist/es/render/utils/get-variant-context.mjs +32 -0
- package/dist/es/render/utils/is-animation-controls.mjs +7 -0
- package/dist/es/render/utils/is-controlling-variants.mjs +13 -0
- package/dist/es/render/utils/is-forced-motion-value.mjs +12 -0
- package/dist/es/render/utils/is-keyframes-target.mjs +5 -0
- package/dist/es/render/utils/is-variant-label.mjs +8 -0
- package/dist/es/render/utils/motion-values.mjs +55 -0
- package/dist/es/render/utils/reduced-motion/index.mjs +19 -0
- package/dist/es/render/utils/reduced-motion/state.mjs +5 -0
- package/dist/es/render/utils/resolve-dynamic-variants.mjs +8 -0
- package/dist/es/render/utils/resolve-variants.mjs +36 -0
- package/dist/es/render/utils/setters.mjs +31 -0
- package/dist/es/render/utils/shallow-compare.mjs +14 -0
- package/dist/es/render/utils/variant-props.mjs +12 -0
- package/dist/es/utils/delay.mjs +24 -0
- package/dist/es/value/utils/resolve-motion-value.mjs +10 -0
- package/dist/es/value/will-change/add-will-change.mjs +20 -0
- package/dist/es/value/will-change/is.mjs +7 -0
- package/dist/index.d.ts +3851 -2128
- package/dist/motion-dom.dev.js +6214 -1836
- package/dist/motion-dom.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
import { warnOnce, isNumericalString, isZeroValueString, SubscriptionManager } from 'motion-utils';
|
|
2
|
+
import { microtask } from '../frameloop/microtask.mjs';
|
|
3
|
+
import { time } from '../frameloop/sync-time.mjs';
|
|
4
|
+
import { motionValue } from '../value/index.mjs';
|
|
5
|
+
import { isMotionValue } from '../value/utils/is-motion-value.mjs';
|
|
6
|
+
import { KeyframeResolver } from '../animation/keyframes/KeyframesResolver.mjs';
|
|
7
|
+
import { transformProps } from './utils/keys-transform.mjs';
|
|
8
|
+
import { complex } from '../value/types/complex/index.mjs';
|
|
9
|
+
import { findValueType } from '../value/types/utils/find.mjs';
|
|
10
|
+
import { getAnimatableNone } from '../value/types/utils/animatable-none.mjs';
|
|
11
|
+
import { createBox } from '../projection/geometry/models.mjs';
|
|
12
|
+
import { initPrefersReducedMotion } from './utils/reduced-motion/index.mjs';
|
|
13
|
+
import { visualElementStore } from './store.mjs';
|
|
14
|
+
import { isControllingVariants, isVariantNode } from './utils/is-controlling-variants.mjs';
|
|
15
|
+
import { updateMotionValuesFromProps } from './utils/motion-values.mjs';
|
|
16
|
+
import { resolveVariantFromProps } from './utils/resolve-variants.mjs';
|
|
17
|
+
import { hasReducedMotionListener, prefersReducedMotion } from './utils/reduced-motion/state.mjs';
|
|
18
|
+
import { frame, cancelFrame } from '../frameloop/frame.mjs';
|
|
19
|
+
|
|
20
|
+
const propEventHandlers = [
|
|
21
|
+
"AnimationStart",
|
|
22
|
+
"AnimationComplete",
|
|
23
|
+
"Update",
|
|
24
|
+
"BeforeLayoutMeasure",
|
|
25
|
+
"LayoutMeasure",
|
|
26
|
+
"LayoutAnimationStart",
|
|
27
|
+
"LayoutAnimationComplete",
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* Static feature definitions - can be injected by framework layer
|
|
31
|
+
*/
|
|
32
|
+
let featureDefinitions = {};
|
|
33
|
+
/**
|
|
34
|
+
* Set feature definitions for all VisualElements.
|
|
35
|
+
* This should be called by the framework layer (e.g., framer-motion) during initialization.
|
|
36
|
+
*/
|
|
37
|
+
function setFeatureDefinitions(definitions) {
|
|
38
|
+
featureDefinitions = definitions;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Get the current feature definitions
|
|
42
|
+
*/
|
|
43
|
+
function getFeatureDefinitions() {
|
|
44
|
+
return featureDefinitions;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A VisualElement is an imperative abstraction around UI elements such as
|
|
48
|
+
* HTMLElement, SVGElement, Three.Object3D etc.
|
|
49
|
+
*/
|
|
50
|
+
class VisualElement {
|
|
51
|
+
/**
|
|
52
|
+
* This method takes React props and returns found MotionValues. For example, HTML
|
|
53
|
+
* MotionValues will be found within the style prop, whereas for Three.js within attribute arrays.
|
|
54
|
+
*
|
|
55
|
+
* This isn't an abstract method as it needs calling in the constructor, but it is
|
|
56
|
+
* intended to be one.
|
|
57
|
+
*/
|
|
58
|
+
scrapeMotionValuesFromProps(_props, _prevProps, _visualElement) {
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
constructor({ parent, props, presenceContext, reducedMotionConfig, blockInitialAnimation, visualState, }, options = {}) {
|
|
62
|
+
/**
|
|
63
|
+
* A reference to the current underlying Instance, e.g. a HTMLElement
|
|
64
|
+
* or Three.Mesh etc.
|
|
65
|
+
*/
|
|
66
|
+
this.current = null;
|
|
67
|
+
/**
|
|
68
|
+
* A set containing references to this VisualElement's children.
|
|
69
|
+
*/
|
|
70
|
+
this.children = new Set();
|
|
71
|
+
/**
|
|
72
|
+
* Determine what role this visual element should take in the variant tree.
|
|
73
|
+
*/
|
|
74
|
+
this.isVariantNode = false;
|
|
75
|
+
this.isControllingVariants = false;
|
|
76
|
+
/**
|
|
77
|
+
* Decides whether this VisualElement should animate in reduced motion
|
|
78
|
+
* mode.
|
|
79
|
+
*
|
|
80
|
+
* TODO: This is currently set on every individual VisualElement but feels
|
|
81
|
+
* like it could be set globally.
|
|
82
|
+
*/
|
|
83
|
+
this.shouldReduceMotion = null;
|
|
84
|
+
/**
|
|
85
|
+
* A map of all motion values attached to this visual element. Motion
|
|
86
|
+
* values are source of truth for any given animated value. A motion
|
|
87
|
+
* value might be provided externally by the component via props.
|
|
88
|
+
*/
|
|
89
|
+
this.values = new Map();
|
|
90
|
+
this.KeyframeResolver = KeyframeResolver;
|
|
91
|
+
/**
|
|
92
|
+
* Cleanup functions for active features (hover/tap/exit etc)
|
|
93
|
+
*/
|
|
94
|
+
this.features = {};
|
|
95
|
+
/**
|
|
96
|
+
* A map of every subscription that binds the provided or generated
|
|
97
|
+
* motion values onChange listeners to this visual element.
|
|
98
|
+
*/
|
|
99
|
+
this.valueSubscriptions = new Map();
|
|
100
|
+
/**
|
|
101
|
+
* A reference to the previously-provided motion values as returned
|
|
102
|
+
* from scrapeMotionValuesFromProps. We use the keys in here to determine
|
|
103
|
+
* if any motion values need to be removed after props are updated.
|
|
104
|
+
*/
|
|
105
|
+
this.prevMotionValues = {};
|
|
106
|
+
/**
|
|
107
|
+
* An object containing a SubscriptionManager for each active event.
|
|
108
|
+
*/
|
|
109
|
+
this.events = {};
|
|
110
|
+
/**
|
|
111
|
+
* An object containing an unsubscribe function for each prop event subscription.
|
|
112
|
+
* For example, every "Update" event can have multiple subscribers via
|
|
113
|
+
* VisualElement.on(), but only one of those can be defined via the onUpdate prop.
|
|
114
|
+
*/
|
|
115
|
+
this.propEventSubscriptions = {};
|
|
116
|
+
this.notifyUpdate = () => this.notify("Update", this.latestValues);
|
|
117
|
+
this.render = () => {
|
|
118
|
+
if (!this.current)
|
|
119
|
+
return;
|
|
120
|
+
this.triggerBuild();
|
|
121
|
+
this.renderInstance(this.current, this.renderState, this.props.style, this.projection);
|
|
122
|
+
};
|
|
123
|
+
this.renderScheduledAt = 0.0;
|
|
124
|
+
this.scheduleRender = () => {
|
|
125
|
+
const now = time.now();
|
|
126
|
+
if (this.renderScheduledAt < now) {
|
|
127
|
+
this.renderScheduledAt = now;
|
|
128
|
+
frame.render(this.render, false, true);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const { latestValues, renderState } = visualState;
|
|
132
|
+
this.latestValues = latestValues;
|
|
133
|
+
this.baseTarget = { ...latestValues };
|
|
134
|
+
this.initialValues = props.initial ? { ...latestValues } : {};
|
|
135
|
+
this.renderState = renderState;
|
|
136
|
+
this.parent = parent;
|
|
137
|
+
this.props = props;
|
|
138
|
+
this.presenceContext = presenceContext;
|
|
139
|
+
this.depth = parent ? parent.depth + 1 : 0;
|
|
140
|
+
this.reducedMotionConfig = reducedMotionConfig;
|
|
141
|
+
this.options = options;
|
|
142
|
+
this.blockInitialAnimation = Boolean(blockInitialAnimation);
|
|
143
|
+
this.isControllingVariants = isControllingVariants(props);
|
|
144
|
+
this.isVariantNode = isVariantNode(props);
|
|
145
|
+
if (this.isVariantNode) {
|
|
146
|
+
this.variantChildren = new Set();
|
|
147
|
+
}
|
|
148
|
+
this.manuallyAnimateOnMount = Boolean(parent && parent.current);
|
|
149
|
+
/**
|
|
150
|
+
* Any motion values that are provided to the element when created
|
|
151
|
+
* aren't yet bound to the element, as this would technically be impure.
|
|
152
|
+
* However, we iterate through the motion values and set them to the
|
|
153
|
+
* initial values for this component.
|
|
154
|
+
*
|
|
155
|
+
* TODO: This is impure and we should look at changing this to run on mount.
|
|
156
|
+
* Doing so will break some tests but this isn't necessarily a breaking change,
|
|
157
|
+
* more a reflection of the test.
|
|
158
|
+
*/
|
|
159
|
+
const { willChange, ...initialMotionValues } = this.scrapeMotionValuesFromProps(props, {}, this);
|
|
160
|
+
for (const key in initialMotionValues) {
|
|
161
|
+
const value = initialMotionValues[key];
|
|
162
|
+
if (latestValues[key] !== undefined && isMotionValue(value)) {
|
|
163
|
+
value.set(latestValues[key]);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
mount(instance) {
|
|
168
|
+
this.current = instance;
|
|
169
|
+
visualElementStore.set(instance, this);
|
|
170
|
+
if (this.projection && !this.projection.instance) {
|
|
171
|
+
this.projection.mount(instance);
|
|
172
|
+
}
|
|
173
|
+
if (this.parent && this.isVariantNode && !this.isControllingVariants) {
|
|
174
|
+
this.removeFromVariantTree = this.parent.addVariantChild(this);
|
|
175
|
+
}
|
|
176
|
+
this.values.forEach((value, key) => this.bindToMotionValue(key, value));
|
|
177
|
+
/**
|
|
178
|
+
* Determine reduced motion preference. Only initialize the matchMedia
|
|
179
|
+
* listener if we actually need the dynamic value (i.e., when config
|
|
180
|
+
* is neither "never" nor "always").
|
|
181
|
+
*/
|
|
182
|
+
if (this.reducedMotionConfig === "never") {
|
|
183
|
+
this.shouldReduceMotion = false;
|
|
184
|
+
}
|
|
185
|
+
else if (this.reducedMotionConfig === "always") {
|
|
186
|
+
this.shouldReduceMotion = true;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
if (!hasReducedMotionListener.current) {
|
|
190
|
+
initPrefersReducedMotion();
|
|
191
|
+
}
|
|
192
|
+
this.shouldReduceMotion = prefersReducedMotion.current;
|
|
193
|
+
}
|
|
194
|
+
if (process.env.NODE_ENV !== "production") {
|
|
195
|
+
warnOnce(this.shouldReduceMotion !== true, "You have Reduced Motion enabled on your device. Animations may not appear as expected.", "reduced-motion-disabled");
|
|
196
|
+
}
|
|
197
|
+
this.parent?.addChild(this);
|
|
198
|
+
this.update(this.props, this.presenceContext);
|
|
199
|
+
}
|
|
200
|
+
unmount() {
|
|
201
|
+
this.projection && this.projection.unmount();
|
|
202
|
+
cancelFrame(this.notifyUpdate);
|
|
203
|
+
cancelFrame(this.render);
|
|
204
|
+
this.valueSubscriptions.forEach((remove) => remove());
|
|
205
|
+
this.valueSubscriptions.clear();
|
|
206
|
+
this.removeFromVariantTree && this.removeFromVariantTree();
|
|
207
|
+
this.parent?.removeChild(this);
|
|
208
|
+
for (const key in this.events) {
|
|
209
|
+
this.events[key].clear();
|
|
210
|
+
}
|
|
211
|
+
for (const key in this.features) {
|
|
212
|
+
const feature = this.features[key];
|
|
213
|
+
if (feature) {
|
|
214
|
+
feature.unmount();
|
|
215
|
+
feature.isMounted = false;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
this.current = null;
|
|
219
|
+
}
|
|
220
|
+
addChild(child) {
|
|
221
|
+
this.children.add(child);
|
|
222
|
+
this.enteringChildren ?? (this.enteringChildren = new Set());
|
|
223
|
+
this.enteringChildren.add(child);
|
|
224
|
+
}
|
|
225
|
+
removeChild(child) {
|
|
226
|
+
this.children.delete(child);
|
|
227
|
+
this.enteringChildren && this.enteringChildren.delete(child);
|
|
228
|
+
}
|
|
229
|
+
bindToMotionValue(key, value) {
|
|
230
|
+
if (this.valueSubscriptions.has(key)) {
|
|
231
|
+
this.valueSubscriptions.get(key)();
|
|
232
|
+
}
|
|
233
|
+
const valueIsTransform = transformProps.has(key);
|
|
234
|
+
if (valueIsTransform && this.onBindTransform) {
|
|
235
|
+
this.onBindTransform();
|
|
236
|
+
}
|
|
237
|
+
const removeOnChange = value.on("change", (latestValue) => {
|
|
238
|
+
this.latestValues[key] = latestValue;
|
|
239
|
+
this.props.onUpdate && frame.preRender(this.notifyUpdate);
|
|
240
|
+
if (valueIsTransform && this.projection) {
|
|
241
|
+
this.projection.isTransformDirty = true;
|
|
242
|
+
}
|
|
243
|
+
this.scheduleRender();
|
|
244
|
+
});
|
|
245
|
+
let removeSyncCheck;
|
|
246
|
+
if (typeof window !== "undefined" && window.MotionCheckAppearSync) {
|
|
247
|
+
removeSyncCheck = window.MotionCheckAppearSync(this, key, value);
|
|
248
|
+
}
|
|
249
|
+
this.valueSubscriptions.set(key, () => {
|
|
250
|
+
removeOnChange();
|
|
251
|
+
if (removeSyncCheck)
|
|
252
|
+
removeSyncCheck();
|
|
253
|
+
if (value.owner)
|
|
254
|
+
value.stop();
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
sortNodePosition(other) {
|
|
258
|
+
/**
|
|
259
|
+
* If these nodes aren't even of the same type we can't compare their depth.
|
|
260
|
+
*/
|
|
261
|
+
if (!this.current ||
|
|
262
|
+
!this.sortInstanceNodePosition ||
|
|
263
|
+
this.type !== other.type) {
|
|
264
|
+
return 0;
|
|
265
|
+
}
|
|
266
|
+
return this.sortInstanceNodePosition(this.current, other.current);
|
|
267
|
+
}
|
|
268
|
+
updateFeatures() {
|
|
269
|
+
let key = "animation";
|
|
270
|
+
for (key in featureDefinitions) {
|
|
271
|
+
const featureDefinition = featureDefinitions[key];
|
|
272
|
+
if (!featureDefinition)
|
|
273
|
+
continue;
|
|
274
|
+
const { isEnabled, Feature: FeatureConstructor } = featureDefinition;
|
|
275
|
+
/**
|
|
276
|
+
* If this feature is enabled but not active, make a new instance.
|
|
277
|
+
*/
|
|
278
|
+
if (!this.features[key] &&
|
|
279
|
+
FeatureConstructor &&
|
|
280
|
+
isEnabled(this.props)) {
|
|
281
|
+
this.features[key] = new FeatureConstructor(this);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* If we have a feature, mount or update it.
|
|
285
|
+
*/
|
|
286
|
+
if (this.features[key]) {
|
|
287
|
+
const feature = this.features[key];
|
|
288
|
+
if (feature.isMounted) {
|
|
289
|
+
feature.update();
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
feature.mount();
|
|
293
|
+
feature.isMounted = true;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
triggerBuild() {
|
|
299
|
+
this.build(this.renderState, this.latestValues, this.props);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Measure the current viewport box with or without transforms.
|
|
303
|
+
* Only measures axis-aligned boxes, rotate and skew must be manually
|
|
304
|
+
* removed with a re-render to work.
|
|
305
|
+
*/
|
|
306
|
+
measureViewportBox() {
|
|
307
|
+
return this.current
|
|
308
|
+
? this.measureInstanceViewportBox(this.current, this.props)
|
|
309
|
+
: createBox();
|
|
310
|
+
}
|
|
311
|
+
getStaticValue(key) {
|
|
312
|
+
return this.latestValues[key];
|
|
313
|
+
}
|
|
314
|
+
setStaticValue(key, value) {
|
|
315
|
+
this.latestValues[key] = value;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Update the provided props. Ensure any newly-added motion values are
|
|
319
|
+
* added to our map, old ones removed, and listeners updated.
|
|
320
|
+
*/
|
|
321
|
+
update(props, presenceContext) {
|
|
322
|
+
if (props.transformTemplate || this.props.transformTemplate) {
|
|
323
|
+
this.scheduleRender();
|
|
324
|
+
}
|
|
325
|
+
this.prevProps = this.props;
|
|
326
|
+
this.props = props;
|
|
327
|
+
this.prevPresenceContext = this.presenceContext;
|
|
328
|
+
this.presenceContext = presenceContext;
|
|
329
|
+
/**
|
|
330
|
+
* Update prop event handlers ie onAnimationStart, onAnimationComplete
|
|
331
|
+
*/
|
|
332
|
+
for (let i = 0; i < propEventHandlers.length; i++) {
|
|
333
|
+
const key = propEventHandlers[i];
|
|
334
|
+
if (this.propEventSubscriptions[key]) {
|
|
335
|
+
this.propEventSubscriptions[key]();
|
|
336
|
+
delete this.propEventSubscriptions[key];
|
|
337
|
+
}
|
|
338
|
+
const listenerName = ("on" + key);
|
|
339
|
+
const listener = props[listenerName];
|
|
340
|
+
if (listener) {
|
|
341
|
+
this.propEventSubscriptions[key] = this.on(key, listener);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
this.prevMotionValues = updateMotionValuesFromProps(this, this.scrapeMotionValuesFromProps(props, this.prevProps || {}, this), this.prevMotionValues);
|
|
345
|
+
if (this.handleChildMotionValue) {
|
|
346
|
+
this.handleChildMotionValue();
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
getProps() {
|
|
350
|
+
return this.props;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Returns the variant definition with a given name.
|
|
354
|
+
*/
|
|
355
|
+
getVariant(name) {
|
|
356
|
+
return this.props.variants ? this.props.variants[name] : undefined;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Returns the defined default transition on this component.
|
|
360
|
+
*/
|
|
361
|
+
getDefaultTransition() {
|
|
362
|
+
return this.props.transition;
|
|
363
|
+
}
|
|
364
|
+
getTransformPagePoint() {
|
|
365
|
+
return this.props.transformPagePoint;
|
|
366
|
+
}
|
|
367
|
+
getClosestVariantNode() {
|
|
368
|
+
return this.isVariantNode
|
|
369
|
+
? this
|
|
370
|
+
: this.parent
|
|
371
|
+
? this.parent.getClosestVariantNode()
|
|
372
|
+
: undefined;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Add a child visual element to our set of children.
|
|
376
|
+
*/
|
|
377
|
+
addVariantChild(child) {
|
|
378
|
+
const closestVariantNode = this.getClosestVariantNode();
|
|
379
|
+
if (closestVariantNode) {
|
|
380
|
+
closestVariantNode.variantChildren &&
|
|
381
|
+
closestVariantNode.variantChildren.add(child);
|
|
382
|
+
return () => closestVariantNode.variantChildren.delete(child);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Add a motion value and bind it to this visual element.
|
|
387
|
+
*/
|
|
388
|
+
addValue(key, value) {
|
|
389
|
+
// Remove existing value if it exists
|
|
390
|
+
const existingValue = this.values.get(key);
|
|
391
|
+
if (value !== existingValue) {
|
|
392
|
+
if (existingValue)
|
|
393
|
+
this.removeValue(key);
|
|
394
|
+
this.bindToMotionValue(key, value);
|
|
395
|
+
this.values.set(key, value);
|
|
396
|
+
this.latestValues[key] = value.get();
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Remove a motion value and unbind any active subscriptions.
|
|
401
|
+
*/
|
|
402
|
+
removeValue(key) {
|
|
403
|
+
this.values.delete(key);
|
|
404
|
+
const unsubscribe = this.valueSubscriptions.get(key);
|
|
405
|
+
if (unsubscribe) {
|
|
406
|
+
unsubscribe();
|
|
407
|
+
this.valueSubscriptions.delete(key);
|
|
408
|
+
}
|
|
409
|
+
delete this.latestValues[key];
|
|
410
|
+
this.removeValueFromRenderState(key, this.renderState);
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Check whether we have a motion value for this key
|
|
414
|
+
*/
|
|
415
|
+
hasValue(key) {
|
|
416
|
+
return this.values.has(key);
|
|
417
|
+
}
|
|
418
|
+
getValue(key, defaultValue) {
|
|
419
|
+
if (this.props.values && this.props.values[key]) {
|
|
420
|
+
return this.props.values[key];
|
|
421
|
+
}
|
|
422
|
+
let value = this.values.get(key);
|
|
423
|
+
if (value === undefined && defaultValue !== undefined) {
|
|
424
|
+
value = motionValue(defaultValue === null ? undefined : defaultValue, { owner: this });
|
|
425
|
+
this.addValue(key, value);
|
|
426
|
+
}
|
|
427
|
+
return value;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* If we're trying to animate to a previously unencountered value,
|
|
431
|
+
* we need to check for it in our state and as a last resort read it
|
|
432
|
+
* directly from the instance (which might have performance implications).
|
|
433
|
+
*/
|
|
434
|
+
readValue(key, target) {
|
|
435
|
+
let value = this.latestValues[key] !== undefined || !this.current
|
|
436
|
+
? this.latestValues[key]
|
|
437
|
+
: this.getBaseTargetFromProps(this.props, key) ??
|
|
438
|
+
this.readValueFromInstance(this.current, key, this.options);
|
|
439
|
+
if (value !== undefined && value !== null) {
|
|
440
|
+
if (typeof value === "string" &&
|
|
441
|
+
(isNumericalString(value) || isZeroValueString(value))) {
|
|
442
|
+
// If this is a number read as a string, ie "0" or "200", convert it to a number
|
|
443
|
+
value = parseFloat(value);
|
|
444
|
+
}
|
|
445
|
+
else if (!findValueType(value) && complex.test(target)) {
|
|
446
|
+
value = getAnimatableNone(key, target);
|
|
447
|
+
}
|
|
448
|
+
this.setBaseTarget(key, isMotionValue(value) ? value.get() : value);
|
|
449
|
+
}
|
|
450
|
+
return isMotionValue(value) ? value.get() : value;
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Set the base target to later animate back to. This is currently
|
|
454
|
+
* only hydrated on creation and when we first read a value.
|
|
455
|
+
*/
|
|
456
|
+
setBaseTarget(key, value) {
|
|
457
|
+
this.baseTarget[key] = value;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Find the base target for a value thats been removed from all animation
|
|
461
|
+
* props.
|
|
462
|
+
*/
|
|
463
|
+
getBaseTarget(key) {
|
|
464
|
+
const { initial } = this.props;
|
|
465
|
+
let valueFromInitial;
|
|
466
|
+
if (typeof initial === "string" || typeof initial === "object") {
|
|
467
|
+
const variant = resolveVariantFromProps(this.props, initial, this.presenceContext?.custom);
|
|
468
|
+
if (variant) {
|
|
469
|
+
valueFromInitial = variant[key];
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* If this value still exists in the current initial variant, read that.
|
|
474
|
+
*/
|
|
475
|
+
if (initial && valueFromInitial !== undefined) {
|
|
476
|
+
return valueFromInitial;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Alternatively, if this VisualElement config has defined a getBaseTarget
|
|
480
|
+
* so we can read the value from an alternative source, try that.
|
|
481
|
+
*/
|
|
482
|
+
const target = this.getBaseTargetFromProps(this.props, key);
|
|
483
|
+
if (target !== undefined && !isMotionValue(target))
|
|
484
|
+
return target;
|
|
485
|
+
/**
|
|
486
|
+
* If the value was initially defined on initial, but it doesn't any more,
|
|
487
|
+
* return undefined. Otherwise return the value as initially read from the DOM.
|
|
488
|
+
*/
|
|
489
|
+
return this.initialValues[key] !== undefined &&
|
|
490
|
+
valueFromInitial === undefined
|
|
491
|
+
? undefined
|
|
492
|
+
: this.baseTarget[key];
|
|
493
|
+
}
|
|
494
|
+
on(eventName, callback) {
|
|
495
|
+
if (!this.events[eventName]) {
|
|
496
|
+
this.events[eventName] = new SubscriptionManager();
|
|
497
|
+
}
|
|
498
|
+
return this.events[eventName].add(callback);
|
|
499
|
+
}
|
|
500
|
+
notify(eventName, ...args) {
|
|
501
|
+
if (this.events[eventName]) {
|
|
502
|
+
this.events[eventName].notify(...args);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
scheduleRenderMicrotask() {
|
|
506
|
+
microtask.render(this.render);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
export { VisualElement, getFeatureDefinitions, setFeatureDefinitions };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { isMotionValue } from '../../value/utils/is-motion-value.mjs';
|
|
2
|
+
import { DOMKeyframesResolver } from '../../animation/keyframes/DOMKeyframesResolver.mjs';
|
|
3
|
+
import { VisualElement } from '../VisualElement.mjs';
|
|
4
|
+
|
|
5
|
+
class DOMVisualElement extends VisualElement {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
this.KeyframeResolver = DOMKeyframesResolver;
|
|
9
|
+
}
|
|
10
|
+
sortInstanceNodePosition(a, b) {
|
|
11
|
+
/**
|
|
12
|
+
* compareDocumentPosition returns a bitmask, by using the bitwise &
|
|
13
|
+
* we're returning true if 2 in that bitmask is set to true. 2 is set
|
|
14
|
+
* to true if b preceeds a.
|
|
15
|
+
*/
|
|
16
|
+
return a.compareDocumentPosition(b) & 2 ? 1 : -1;
|
|
17
|
+
}
|
|
18
|
+
getBaseTargetFromProps(props, key) {
|
|
19
|
+
const style = props.style;
|
|
20
|
+
return style ? style[key] : undefined;
|
|
21
|
+
}
|
|
22
|
+
removeValueFromRenderState(key, { vars, style }) {
|
|
23
|
+
delete vars[key];
|
|
24
|
+
delete style[key];
|
|
25
|
+
}
|
|
26
|
+
handleChildMotionValue() {
|
|
27
|
+
if (this.childSubscription) {
|
|
28
|
+
this.childSubscription();
|
|
29
|
+
delete this.childSubscription;
|
|
30
|
+
}
|
|
31
|
+
const { children } = this.props;
|
|
32
|
+
if (isMotionValue(children)) {
|
|
33
|
+
this.childSubscription = children.on("change", (latest) => {
|
|
34
|
+
if (this.current) {
|
|
35
|
+
this.current.textContent = `${latest}`;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { DOMVisualElement };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isCSSVariableName } from '../../animation/utils/is-css-variable.mjs';
|
|
2
|
+
import { transformProps } from '../utils/keys-transform.mjs';
|
|
3
|
+
import { defaultTransformValue, readTransformValue } from '../dom/parse-transform.mjs';
|
|
4
|
+
import { measureViewportBox } from '../../projection/utils/measure.mjs';
|
|
5
|
+
import { DOMVisualElement } from '../dom/DOMVisualElement.mjs';
|
|
6
|
+
import { buildHTMLStyles } from './utils/build-styles.mjs';
|
|
7
|
+
import { renderHTML } from './utils/render.mjs';
|
|
8
|
+
import { scrapeMotionValuesFromProps } from './utils/scrape-motion-values.mjs';
|
|
9
|
+
|
|
10
|
+
function getComputedStyle(element) {
|
|
11
|
+
return window.getComputedStyle(element);
|
|
12
|
+
}
|
|
13
|
+
class HTMLVisualElement extends DOMVisualElement {
|
|
14
|
+
constructor() {
|
|
15
|
+
super(...arguments);
|
|
16
|
+
this.type = "html";
|
|
17
|
+
this.renderInstance = renderHTML;
|
|
18
|
+
}
|
|
19
|
+
readValueFromInstance(instance, key) {
|
|
20
|
+
if (transformProps.has(key)) {
|
|
21
|
+
return this.projection?.isProjecting
|
|
22
|
+
? defaultTransformValue(key)
|
|
23
|
+
: readTransformValue(instance, key);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const computedStyle = getComputedStyle(instance);
|
|
27
|
+
const value = (isCSSVariableName(key)
|
|
28
|
+
? computedStyle.getPropertyValue(key)
|
|
29
|
+
: computedStyle[key]) || 0;
|
|
30
|
+
return typeof value === "string" ? value.trim() : value;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
measureInstanceViewportBox(instance, { transformPagePoint }) {
|
|
34
|
+
return measureViewportBox(instance, transformPagePoint);
|
|
35
|
+
}
|
|
36
|
+
build(renderState, latestValues, props) {
|
|
37
|
+
buildHTMLStyles(renderState, latestValues, props.transformTemplate);
|
|
38
|
+
}
|
|
39
|
+
scrapeMotionValuesFromProps(props, prevProps, visualElement) {
|
|
40
|
+
return scrapeMotionValuesFromProps(props, prevProps, visualElement);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { HTMLVisualElement, getComputedStyle };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { getValueAsType } from '../../../value/types/utils/get-as-type.mjs';
|
|
2
|
+
import { numberValueTypes } from '../../../value/types/maps/number.mjs';
|
|
3
|
+
import { transformProps } from '../../utils/keys-transform.mjs';
|
|
4
|
+
import { isCSSVariableName } from '../../../animation/utils/is-css-variable.mjs';
|
|
5
|
+
import { buildTransform } from './build-transform.mjs';
|
|
6
|
+
|
|
7
|
+
function buildHTMLStyles(state, latestValues, transformTemplate) {
|
|
8
|
+
const { style, vars, transformOrigin } = state;
|
|
9
|
+
// Track whether we encounter any transform or transformOrigin values.
|
|
10
|
+
let hasTransform = false;
|
|
11
|
+
let hasTransformOrigin = false;
|
|
12
|
+
/**
|
|
13
|
+
* Loop over all our latest animated values and decide whether to handle them
|
|
14
|
+
* as a style or CSS variable.
|
|
15
|
+
*
|
|
16
|
+
* Transforms and transform origins are kept separately for further processing.
|
|
17
|
+
*/
|
|
18
|
+
for (const key in latestValues) {
|
|
19
|
+
const value = latestValues[key];
|
|
20
|
+
if (transformProps.has(key)) {
|
|
21
|
+
// If this is a transform, flag to enable further transform processing
|
|
22
|
+
hasTransform = true;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
else if (isCSSVariableName(key)) {
|
|
26
|
+
vars[key] = value;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// Convert the value to its default value type, ie 0 -> "0px"
|
|
31
|
+
const valueAsType = getValueAsType(value, numberValueTypes[key]);
|
|
32
|
+
if (key.startsWith("origin")) {
|
|
33
|
+
// If this is a transform origin, flag and enable further transform-origin processing
|
|
34
|
+
hasTransformOrigin = true;
|
|
35
|
+
transformOrigin[key] =
|
|
36
|
+
valueAsType;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
style[key] = valueAsType;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (!latestValues.transform) {
|
|
44
|
+
if (hasTransform || transformTemplate) {
|
|
45
|
+
style.transform = buildTransform(latestValues, state.transform, transformTemplate);
|
|
46
|
+
}
|
|
47
|
+
else if (style.transform) {
|
|
48
|
+
/**
|
|
49
|
+
* If we have previously created a transform but currently don't have any,
|
|
50
|
+
* reset transform style to none.
|
|
51
|
+
*/
|
|
52
|
+
style.transform = "none";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Build a transformOrigin style. Uses the same defaults as the browser for
|
|
57
|
+
* undefined origins.
|
|
58
|
+
*/
|
|
59
|
+
if (hasTransformOrigin) {
|
|
60
|
+
const { originX = "50%", originY = "50%", originZ = 0, } = transformOrigin;
|
|
61
|
+
style.transformOrigin = `${originX} ${originY} ${originZ}`;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { buildHTMLStyles };
|