@krate/motion 0.0.0-stage → 0.5.0-rc.18
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/LICENSE +201 -0
- package/NOTICE +36 -0
- package/README.md +57 -2
- package/package.json +66 -3
- package/src/components.tsx +59 -0
- package/src/context.ts +9 -0
- package/src/engine/animate.ts +340 -0
- package/src/engine/animation.ts +852 -0
- package/src/engine/drag.ts +584 -0
- package/src/engine/easing.ts +108 -0
- package/src/engine/factories.ts +192 -0
- package/src/engine/fallback.ts +44 -0
- package/src/engine/frameloop.ts +179 -0
- package/src/engine/generators.ts +342 -0
- package/src/engine/gesture-features.ts +136 -0
- package/src/engine/gestures.ts +229 -0
- package/src/engine/index.ts +22 -0
- package/src/engine/krate.ts +402 -0
- package/src/engine/layout.ts +365 -0
- package/src/engine/motion-value.ts +234 -0
- package/src/engine/node.ts +988 -0
- package/src/engine/render.ts +295 -0
- package/src/engine/reorder.ts +65 -0
- package/src/engine/runtime.ts +74 -0
- package/src/engine/scope.ts +83 -0
- package/src/engine/scroll.ts +272 -0
- package/src/engine/signals.ts +108 -0
- package/src/engine/utils.ts +122 -0
- package/src/engine/values.ts +508 -0
- package/src/hooks.ts +191 -0
- package/src/index.ts +143 -0
- package/src/island/index.ts +696 -0
- package/src/motion-types.ts +25 -0
- package/src/reorder.tsx +112 -0
- package/src/types.ts +179 -0
|
@@ -0,0 +1,988 @@
|
|
|
1
|
+
// MotionNode: the engine side of one motion element. It owns the element's
|
|
2
|
+
// motion values, resolves variants, runs the animation state machine
|
|
3
|
+
// (animate / while* gestures / exit, in priority order) and writes styles on
|
|
4
|
+
// the frame loop. Hosts (compiled Krate elements, Preact islands) only pass it
|
|
5
|
+
// props, a parent and a presence/config scope. Ported from framer-motion
|
|
6
|
+
// (MIT, Copyright (c) 2018 Framer B.V.), see NOTICE.
|
|
7
|
+
|
|
8
|
+
import { animateMotionValue, getValueTransition, type Transition } from './animation.js';
|
|
9
|
+
import { cancelFrame, frame, microtask, time } from './frameloop.js';
|
|
10
|
+
import { isMotionValue, motionValue, type AnimationPlaybackControls, type MotionValue } from './motion-value.js';
|
|
11
|
+
import {
|
|
12
|
+
buildHTMLStyles,
|
|
13
|
+
buildSVGAttrs,
|
|
14
|
+
createRenderState,
|
|
15
|
+
isSVGComponent,
|
|
16
|
+
positionalKeys,
|
|
17
|
+
readDOMValue,
|
|
18
|
+
renderHTML,
|
|
19
|
+
renderSVG,
|
|
20
|
+
transformProps,
|
|
21
|
+
type RenderState,
|
|
22
|
+
} from './render.js';
|
|
23
|
+
import { complex, findValueType, getAnimatableNone } from './values.js';
|
|
24
|
+
import { isNumericalString, isZeroValueString, SubscriptionManager, warnOnce } from './utils.js';
|
|
25
|
+
|
|
26
|
+
export type VariantLabels = string | string[];
|
|
27
|
+
export type TargetAndTransition = Record<string, any> & { transition?: Transition; transitionEnd?: Record<string, any> };
|
|
28
|
+
export type Variant = TargetAndTransition | ((custom: any, current: Record<string, any>, velocity: Record<string, any>) => TargetAndTransition | string);
|
|
29
|
+
export type Variants = Record<string, Variant>;
|
|
30
|
+
|
|
31
|
+
export interface AnimationControlsLike {
|
|
32
|
+
start(definition: any, transitionOverride?: Transition): Promise<any>;
|
|
33
|
+
subscribe(node: MotionNode): () => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type AnimationType = 'animate' | 'whileInView' | 'whileFocus' | 'whileHover' | 'whileTap' | 'whileDrag' | 'exit';
|
|
37
|
+
|
|
38
|
+
export const variantPriorityOrder: AnimationType[] = ['animate', 'whileInView', 'whileFocus', 'whileHover', 'whileTap', 'whileDrag', 'exit'];
|
|
39
|
+
export const variantProps = ['initial', ...variantPriorityOrder];
|
|
40
|
+
|
|
41
|
+
export const isVariantLabel = (v: unknown): v is VariantLabels => typeof v === 'string' || Array.isArray(v);
|
|
42
|
+
export const isAnimationControls = (v: unknown): v is AnimationControlsLike =>
|
|
43
|
+
v !== null && typeof v === 'object' && typeof (v as AnimationControlsLike).start === 'function';
|
|
44
|
+
export const isKeyframesTarget = (v: unknown): v is any[] => Array.isArray(v);
|
|
45
|
+
|
|
46
|
+
export function isControllingVariants(props: Record<string, any>): boolean {
|
|
47
|
+
return isAnimationControls(props.animate) || variantProps.some((name) => isVariantLabel(props[name]));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isVariantNode(props: Record<string, any>): boolean {
|
|
51
|
+
return Boolean(isControllingVariants(props) || props.variants);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function shallowCompare(next: any[], prev: unknown): boolean {
|
|
55
|
+
if (!Array.isArray(prev) || prev.length !== next.length) return false;
|
|
56
|
+
for (let i = 0; i < prev.length; i++) if (prev[i] !== next[i]) return false;
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Presence of a node: whether it is leaving, and the AnimatePresence custom value. */
|
|
61
|
+
export interface PresenceContext {
|
|
62
|
+
isPresent: boolean;
|
|
63
|
+
initial?: false | VariantLabels;
|
|
64
|
+
custom?: any;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** MotionConfig values a node reads. */
|
|
68
|
+
export interface ConfigContext {
|
|
69
|
+
transition?: Transition;
|
|
70
|
+
reducedMotion?: 'user' | 'always' | 'never';
|
|
71
|
+
transformPagePoint?: (p: { x: number; y: number }) => { x: number; y: number };
|
|
72
|
+
isStatic?: boolean;
|
|
73
|
+
nonce?: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Reduced motion
|
|
77
|
+
|
|
78
|
+
export const prefersReducedMotion: { current: boolean | null } = { current: null };
|
|
79
|
+
export const hasReducedMotionListener = { current: false };
|
|
80
|
+
const reducedMotionSubscribers = new SubscriptionManager<(v: boolean) => void>();
|
|
81
|
+
|
|
82
|
+
export function initPrefersReducedMotion(): void {
|
|
83
|
+
hasReducedMotionListener.current = true;
|
|
84
|
+
if (typeof window === 'undefined') return;
|
|
85
|
+
if (window.matchMedia) {
|
|
86
|
+
const mq = window.matchMedia('(prefers-reduced-motion)');
|
|
87
|
+
const set = (): void => {
|
|
88
|
+
prefersReducedMotion.current = mq.matches;
|
|
89
|
+
reducedMotionSubscribers.notify(mq.matches);
|
|
90
|
+
};
|
|
91
|
+
if (mq.addEventListener) mq.addEventListener('change', set);
|
|
92
|
+
set();
|
|
93
|
+
} else {
|
|
94
|
+
prefersReducedMotion.current = false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Subscribes to changes of the device's reduced-motion setting. */
|
|
99
|
+
export function onReducedMotionChange(cb: (v: boolean) => void): () => void {
|
|
100
|
+
if (!hasReducedMotionListener.current) initPrefersReducedMotion();
|
|
101
|
+
return reducedMotionSubscribers.add(cb);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Variants
|
|
105
|
+
|
|
106
|
+
function getValueState(node?: MotionNode): [Record<string, any>, Record<string, any>] {
|
|
107
|
+
const state: [Record<string, any>, Record<string, any>] = [{}, {}];
|
|
108
|
+
node?.values.forEach((value, key) => {
|
|
109
|
+
state[0][key] = value.get();
|
|
110
|
+
state[1][key] = value.getVelocity();
|
|
111
|
+
});
|
|
112
|
+
return state;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function resolveVariantFromProps(props: Record<string, any>, definition: any, custom?: any, node?: MotionNode): TargetAndTransition | undefined {
|
|
116
|
+
if (typeof definition === 'function') {
|
|
117
|
+
const [current, velocity] = getValueState(node);
|
|
118
|
+
definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
|
|
119
|
+
}
|
|
120
|
+
if (typeof definition === 'string') definition = props.variants && props.variants[definition];
|
|
121
|
+
if (typeof definition === 'function') {
|
|
122
|
+
const [current, velocity] = getValueState(node);
|
|
123
|
+
definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
|
|
124
|
+
}
|
|
125
|
+
return definition;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function resolveVariant(node: MotionNode, definition: any, custom?: any): TargetAndTransition | undefined {
|
|
129
|
+
const props = node.getProps();
|
|
130
|
+
return resolveVariantFromProps(props, definition, custom !== undefined ? custom : props.custom, node);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The values an element starts with, from `initial` (or `animate` when the
|
|
135
|
+
* initial animation is blocked by `initial={false}` here or on its
|
|
136
|
+
* AnimatePresence), its inherited variant labels and motion values in style.
|
|
137
|
+
* This is also what the server renders.
|
|
138
|
+
*/
|
|
139
|
+
export function makeLatestValues(props: Record<string, any>, context: { initial?: any; animate?: any } | undefined, presence: PresenceContext | null | undefined, motionValues: Record<string, any>): Record<string, any> {
|
|
140
|
+
const values: Record<string, any> = {};
|
|
141
|
+
for (const key in motionValues) {
|
|
142
|
+
const v = motionValues[key];
|
|
143
|
+
values[key] = isMotionValue(v) ? v.get() : v;
|
|
144
|
+
}
|
|
145
|
+
let { initial, animate } = props;
|
|
146
|
+
if (context && isVariantNode(props) && !isControllingVariants(props) && props.inherit !== false) {
|
|
147
|
+
if (initial === undefined) initial = context.initial;
|
|
148
|
+
if (animate === undefined) animate = context.animate;
|
|
149
|
+
}
|
|
150
|
+
const blocked = (presence ? presence.initial === false : false) || initial === false;
|
|
151
|
+
const toSet = blocked ? animate : initial;
|
|
152
|
+
if (toSet && typeof toSet !== 'boolean' && !isAnimationControls(toSet)) {
|
|
153
|
+
for (const def of Array.isArray(toSet) ? toSet : [toSet]) {
|
|
154
|
+
const resolved = resolveVariantFromProps(props, def);
|
|
155
|
+
if (!resolved) continue;
|
|
156
|
+
const { transitionEnd, transition: _t, ...target } = resolved;
|
|
157
|
+
for (const key in target) {
|
|
158
|
+
let v = target[key];
|
|
159
|
+
if (Array.isArray(v)) v = v[blocked ? v.length - 1 : 0];
|
|
160
|
+
if (v !== null) values[key] = v;
|
|
161
|
+
}
|
|
162
|
+
for (const key in transitionEnd) values[key] = transitionEnd[key];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return values;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The variant labels a node passes down to its children. */
|
|
169
|
+
export function getVariantContext(node?: MotionNode | null): Record<string, any> | undefined {
|
|
170
|
+
if (!node) return undefined;
|
|
171
|
+
if (!node.isControllingVariants) {
|
|
172
|
+
const ctx = node.parent ? getVariantContext(node.parent) || {} : {};
|
|
173
|
+
if (node.props.initial !== undefined) ctx.initial = node.props.initial;
|
|
174
|
+
return ctx;
|
|
175
|
+
}
|
|
176
|
+
const ctx: Record<string, any> = {};
|
|
177
|
+
for (const name of variantProps) {
|
|
178
|
+
const p = node.props[name];
|
|
179
|
+
if (isVariantLabel(p) || p === false) ctx[name] = p;
|
|
180
|
+
}
|
|
181
|
+
return ctx;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Context a child created under `parent` inherits (initial/animate labels). */
|
|
185
|
+
export function childVariantContext(parent: MotionNode | null): { initial?: any; animate?: any } | undefined {
|
|
186
|
+
if (!parent) return undefined;
|
|
187
|
+
return getVariantContext(parent);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function setMotionValue(node: MotionNode, key: string, value: any): void {
|
|
191
|
+
if (node.hasValue(key)) node.getValue(key)!.set(value);
|
|
192
|
+
else node.addValue(key, motionValue(value));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Jumps a node to a target without animating (transitionEnd, controls.set). */
|
|
196
|
+
export function setTarget(node: MotionNode, definition: any): void {
|
|
197
|
+
const resolved = resolveVariant(node, definition);
|
|
198
|
+
const { transitionEnd = {}, transition: _t = {}, ...rest } = resolved || ({} as TargetAndTransition);
|
|
199
|
+
const target: Record<string, any> = { ...rest, ...transitionEnd };
|
|
200
|
+
for (const key in target) {
|
|
201
|
+
const v = target[key];
|
|
202
|
+
setMotionValue(node, key, isKeyframesTarget(v) ? v[v.length - 1] || 0 : v);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function calcChildStagger(children: Set<MotionNode>, child: MotionNode, delayChildren: any, staggerChildren = 0, staggerDirection = 1): number {
|
|
207
|
+
const index = Array.from(children)
|
|
208
|
+
.sort((a, b) => a.sortNodePosition(b))
|
|
209
|
+
.indexOf(child);
|
|
210
|
+
const n = children.size;
|
|
211
|
+
if (typeof delayChildren === 'function') return delayChildren(index, n);
|
|
212
|
+
return staggerDirection === 1 ? index * staggerChildren : (n - 1) * staggerChildren - index * staggerChildren;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
interface AnimateOptions {
|
|
216
|
+
delay?: number;
|
|
217
|
+
transitionOverride?: Transition;
|
|
218
|
+
custom?: any;
|
|
219
|
+
type?: AnimationType;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function shouldBlockAnimation({ protectedKeys, needsAnimating }: TypeState, key: string): boolean {
|
|
223
|
+
const block = Object.prototype.hasOwnProperty.call(protectedKeys, key) && needsAnimating[key] !== true;
|
|
224
|
+
needsAnimating[key] = false;
|
|
225
|
+
return block;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function animateTarget(node: MotionNode, targetAndTransition: TargetAndTransition, { delay = 0, transitionOverride, type }: AnimateOptions = {}): AnimationPlaybackControls[] {
|
|
229
|
+
const { transition: own = node.getDefaultTransition(), transitionEnd, ...target } = targetAndTransition;
|
|
230
|
+
const transition = transitionOverride || own;
|
|
231
|
+
const animations: AnimationPlaybackControls[] = [];
|
|
232
|
+
const typeState = type && node.animationState && node.animationState.getState()[type];
|
|
233
|
+
for (const key in target) {
|
|
234
|
+
const value = node.getValue(key, node.latestValues[key] ?? null)!;
|
|
235
|
+
const valueTarget = target[key];
|
|
236
|
+
if (valueTarget === undefined || (typeState && shouldBlockAnimation(typeState, key))) continue;
|
|
237
|
+
const valueTransition: Transition = { delay, ...getValueTransition(transition || {}, key) };
|
|
238
|
+
const current = value.get();
|
|
239
|
+
if (current !== undefined && !value.isAnimating() && !Array.isArray(valueTarget) && valueTarget === current && !valueTransition.velocity) continue;
|
|
240
|
+
const reduce = node.shouldReduceMotion && positionalKeys.has(key);
|
|
241
|
+
void value.start(animateMotionValue(key, value, valueTarget, reduce ? { type: false } : valueTransition, node));
|
|
242
|
+
if (value.animation) animations.push(value.animation);
|
|
243
|
+
}
|
|
244
|
+
if (transitionEnd) {
|
|
245
|
+
void Promise.all(animations).then(() => {
|
|
246
|
+
frame.update(() => {
|
|
247
|
+
if (transitionEnd) setTarget(node, transitionEnd);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return animations;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function animateVariant(node: MotionNode, variant: string, options: AnimateOptions = {}): Promise<any> {
|
|
255
|
+
const resolved = resolveVariant(node, variant, options.type === 'exit' ? node.presence?.custom : undefined);
|
|
256
|
+
let { transition = node.getDefaultTransition() || {} } = resolved || {};
|
|
257
|
+
if (options.transitionOverride) transition = options.transitionOverride;
|
|
258
|
+
const getAnimation = resolved ? () => Promise.all(animateTarget(node, resolved, options)) : () => Promise.resolve();
|
|
259
|
+
const getChildren =
|
|
260
|
+
node.variantChildren && node.variantChildren.size
|
|
261
|
+
? (forwardDelay = 0) => {
|
|
262
|
+
const { delayChildren = 0, staggerChildren, staggerDirection } = transition as Transition;
|
|
263
|
+
return animateChildren(node, variant, forwardDelay, delayChildren, staggerChildren, staggerDirection, options);
|
|
264
|
+
}
|
|
265
|
+
: () => Promise.resolve();
|
|
266
|
+
const { when } = transition as Transition;
|
|
267
|
+
if (when) {
|
|
268
|
+
const [first, last] = when === 'beforeChildren' ? [getAnimation, getChildren] : [getChildren, getAnimation];
|
|
269
|
+
return first().then(() => last());
|
|
270
|
+
}
|
|
271
|
+
return Promise.all([getAnimation(), getChildren(options.delay)]);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function animateChildren(node: MotionNode, variant: string, delay = 0, delayChildren: any = 0, staggerChildren = 0, staggerDirection = 1, options: AnimateOptions): Promise<any> {
|
|
275
|
+
const animations: Array<Promise<any>> = [];
|
|
276
|
+
for (const child of node.variantChildren!) {
|
|
277
|
+
child.notify('AnimationStart', variant);
|
|
278
|
+
animations.push(
|
|
279
|
+
animateVariant(child, variant, {
|
|
280
|
+
...options,
|
|
281
|
+
delay: delay + (typeof delayChildren === 'function' ? 0 : delayChildren) + calcChildStagger(node.variantChildren!, child, delayChildren, staggerChildren, staggerDirection),
|
|
282
|
+
}).then(() => child.notify('AnimationComplete', variant)),
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return Promise.all(animations);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Animates a node to a variant label, labels, a target or a function. */
|
|
289
|
+
export function animateVisualElement(node: MotionNode, definition: any, options: AnimateOptions = {}): Promise<void> {
|
|
290
|
+
node.notify('AnimationStart', definition);
|
|
291
|
+
let animation: Promise<any>;
|
|
292
|
+
if (Array.isArray(definition)) {
|
|
293
|
+
animation = Promise.all(definition.map((v) => animateVariant(node, v, options)));
|
|
294
|
+
} else if (typeof definition === 'string') {
|
|
295
|
+
animation = animateVariant(node, definition, options);
|
|
296
|
+
} else {
|
|
297
|
+
const resolved = typeof definition === 'function' ? resolveVariant(node, definition, options.custom) : definition;
|
|
298
|
+
animation = Promise.all(animateTarget(node, resolved, options));
|
|
299
|
+
}
|
|
300
|
+
return animation.then(() => {
|
|
301
|
+
node.notify('AnimationComplete', definition);
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Animation state
|
|
306
|
+
|
|
307
|
+
interface TypeState {
|
|
308
|
+
isActive: boolean;
|
|
309
|
+
protectedKeys: Record<string, true>;
|
|
310
|
+
needsAnimating: Record<string, boolean>;
|
|
311
|
+
prevResolvedValues: Record<string, any>;
|
|
312
|
+
prevProp?: any;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const createTypeState = (isActive = false): TypeState => ({ isActive, protectedKeys: {}, needsAnimating: {}, prevResolvedValues: {} });
|
|
316
|
+
const createState = (): Record<AnimationType, TypeState> => ({
|
|
317
|
+
animate: createTypeState(true),
|
|
318
|
+
whileInView: createTypeState(),
|
|
319
|
+
whileHover: createTypeState(),
|
|
320
|
+
whileTap: createTypeState(),
|
|
321
|
+
whileDrag: createTypeState(),
|
|
322
|
+
whileFocus: createTypeState(),
|
|
323
|
+
exit: createTypeState(),
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
function checkVariantsDidChange(prev: any, next: any): boolean {
|
|
327
|
+
if (typeof next === 'string') return next !== prev;
|
|
328
|
+
if (Array.isArray(next)) return !shallowCompare(next, prev);
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export interface AnimationState {
|
|
333
|
+
animateChanges(changedActiveType?: AnimationType): Promise<any>;
|
|
334
|
+
setActive(type: AnimationType, isActive: boolean): Promise<any>;
|
|
335
|
+
setAnimateFunction(make: (node: MotionNode) => (animations: Array<{ animation: any; options?: AnimateOptions }>) => Promise<any>): void;
|
|
336
|
+
getState(): Record<AnimationType, TypeState>;
|
|
337
|
+
reset(): void;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const reversePriorityOrder = [...variantPriorityOrder].reverse();
|
|
341
|
+
|
|
342
|
+
export function createAnimationState(node: MotionNode): AnimationState {
|
|
343
|
+
let animate = (list: Array<{ animation: any; options?: AnimateOptions }>) => Promise.all(list.map(({ animation, options }) => animateVisualElement(node, animation, options)));
|
|
344
|
+
let state = createState();
|
|
345
|
+
let isInitialRender = true;
|
|
346
|
+
|
|
347
|
+
const buildResolved = (type: AnimationType) => (acc: Record<string, any>, def: any) => {
|
|
348
|
+
const resolved = resolveVariant(node, def, type === 'exit' ? node.presence?.custom : undefined);
|
|
349
|
+
if (resolved) {
|
|
350
|
+
const { transition: _t, transitionEnd, ...target } = resolved;
|
|
351
|
+
acc = { ...acc, ...target, ...transitionEnd };
|
|
352
|
+
}
|
|
353
|
+
return acc;
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
function animateChanges(changedActiveType?: AnimationType): Promise<any> {
|
|
357
|
+
const { props } = node;
|
|
358
|
+
const context = getVariantContext(node.parent) || {};
|
|
359
|
+
const animations: Array<{ animation: any; options?: AnimateOptions }> = [];
|
|
360
|
+
const removedKeys = new Set<string>();
|
|
361
|
+
let encountered: Record<string, any> = {};
|
|
362
|
+
let removedVariantIndex = Infinity;
|
|
363
|
+
for (let i = 0; i < reversePriorityOrder.length; i++) {
|
|
364
|
+
const type = reversePriorityOrder[i];
|
|
365
|
+
const typeState = state[type];
|
|
366
|
+
const prop = props[type] !== undefined ? props[type] : context[type];
|
|
367
|
+
const propIsVariant = isVariantLabel(prop);
|
|
368
|
+
const activeDelta = type === changedActiveType ? typeState.isActive : null;
|
|
369
|
+
if (activeDelta === false) removedVariantIndex = i;
|
|
370
|
+
let isInherited = prop === context[type] && prop !== props[type] && propIsVariant;
|
|
371
|
+
if (isInherited && isInitialRender && node.manuallyAnimateOnMount) isInherited = false;
|
|
372
|
+
typeState.protectedKeys = { ...encountered };
|
|
373
|
+
if ((!typeState.isActive && activeDelta === null) || (!prop && !typeState.prevProp) || isAnimationControls(prop) || typeof prop === 'boolean') continue;
|
|
374
|
+
const variantDidChange = checkVariantsDidChange(typeState.prevProp, prop);
|
|
375
|
+
let shouldAnimateType = variantDidChange || (type === changedActiveType && typeState.isActive && !isInherited && propIsVariant) || (i > removedVariantIndex && propIsVariant);
|
|
376
|
+
let handledRemoved = false;
|
|
377
|
+
const list = Array.isArray(prop) ? prop : [prop];
|
|
378
|
+
let resolvedValues = list.reduce(buildResolved(type), {});
|
|
379
|
+
if (activeDelta === false) resolvedValues = {};
|
|
380
|
+
const { prevResolvedValues = {} } = typeState;
|
|
381
|
+
const allKeys = { ...prevResolvedValues, ...resolvedValues };
|
|
382
|
+
const markToAnimate = (key: string): void => {
|
|
383
|
+
shouldAnimateType = true;
|
|
384
|
+
if (removedKeys.has(key)) {
|
|
385
|
+
handledRemoved = true;
|
|
386
|
+
removedKeys.delete(key);
|
|
387
|
+
}
|
|
388
|
+
typeState.needsAnimating[key] = true;
|
|
389
|
+
const mv = node.getValue(key) as (MotionValue & { liveStyle?: boolean }) | undefined;
|
|
390
|
+
if (mv) mv.liveStyle = false;
|
|
391
|
+
};
|
|
392
|
+
for (const key in allKeys) {
|
|
393
|
+
const next = resolvedValues[key];
|
|
394
|
+
const prev = prevResolvedValues[key];
|
|
395
|
+
if (Object.prototype.hasOwnProperty.call(encountered, key)) continue;
|
|
396
|
+
const changed = isKeyframesTarget(next) && isKeyframesTarget(prev) ? !shallowCompare(next, prev) : next !== prev;
|
|
397
|
+
if (changed) {
|
|
398
|
+
if (next !== undefined && next !== null) markToAnimate(key);
|
|
399
|
+
else removedKeys.add(key);
|
|
400
|
+
} else if (next !== undefined && removedKeys.has(key)) {
|
|
401
|
+
markToAnimate(key);
|
|
402
|
+
} else {
|
|
403
|
+
typeState.protectedKeys[key] = true;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
typeState.prevProp = prop;
|
|
407
|
+
typeState.prevResolvedValues = resolvedValues;
|
|
408
|
+
if (typeState.isActive) encountered = { ...encountered, ...resolvedValues };
|
|
409
|
+
if (isInitialRender && node.blockInitialAnimation) shouldAnimateType = false;
|
|
410
|
+
const willAnimateViaParent = isInherited && variantDidChange;
|
|
411
|
+
const needsAnimating = !willAnimateViaParent || handledRemoved;
|
|
412
|
+
if (shouldAnimateType && needsAnimating) {
|
|
413
|
+
for (const animation of list) {
|
|
414
|
+
const options: AnimateOptions = { type };
|
|
415
|
+
if (typeof animation === 'string' && isInitialRender && !willAnimateViaParent && node.manuallyAnimateOnMount && node.parent) {
|
|
416
|
+
const { parent } = node;
|
|
417
|
+
const parentVariant = resolveVariant(parent, animation);
|
|
418
|
+
if (parent.enteringChildren && parentVariant) {
|
|
419
|
+
const { delayChildren } = parentVariant.transition || {};
|
|
420
|
+
options.delay = calcChildStagger(parent.enteringChildren, node, delayChildren);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
animations.push({ animation, options });
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (removedKeys.size) {
|
|
428
|
+
const fallback: Record<string, any> = {};
|
|
429
|
+
if (typeof props.initial !== 'boolean') {
|
|
430
|
+
const initialTransition = resolveVariant(node, Array.isArray(props.initial) ? props.initial[0] : props.initial);
|
|
431
|
+
if (initialTransition && initialTransition.transition) fallback.transition = initialTransition.transition;
|
|
432
|
+
}
|
|
433
|
+
removedKeys.forEach((key) => {
|
|
434
|
+
const target = node.getBaseTarget(key);
|
|
435
|
+
const mv = node.getValue(key) as (MotionValue & { liveStyle?: boolean }) | undefined;
|
|
436
|
+
if (mv) mv.liveStyle = true;
|
|
437
|
+
fallback[key] = target ?? null;
|
|
438
|
+
});
|
|
439
|
+
animations.push({ animation: fallback });
|
|
440
|
+
}
|
|
441
|
+
let shouldAnimate = Boolean(animations.length);
|
|
442
|
+
if (isInitialRender && (props.initial === false || props.initial === props.animate) && !node.manuallyAnimateOnMount) shouldAnimate = false;
|
|
443
|
+
isInitialRender = false;
|
|
444
|
+
return shouldAnimate ? animate(animations) : Promise.resolve();
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function setActive(type: AnimationType, isActive: boolean): Promise<any> {
|
|
448
|
+
if (state[type].isActive === isActive) return Promise.resolve();
|
|
449
|
+
node.variantChildren?.forEach((child) => child.animationState?.setActive(type, isActive));
|
|
450
|
+
state[type].isActive = isActive;
|
|
451
|
+
const result = animateChanges(type);
|
|
452
|
+
for (const key in state) state[key as AnimationType].protectedKeys = {};
|
|
453
|
+
return result;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return {
|
|
457
|
+
animateChanges,
|
|
458
|
+
setActive,
|
|
459
|
+
setAnimateFunction(make) {
|
|
460
|
+
animate = make(node);
|
|
461
|
+
},
|
|
462
|
+
getState: () => state,
|
|
463
|
+
reset() {
|
|
464
|
+
state = createState();
|
|
465
|
+
},
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// Features
|
|
470
|
+
|
|
471
|
+
/** A piece of behaviour (a gesture, drag, layout) attached to a node while its props enable it. */
|
|
472
|
+
export abstract class Feature {
|
|
473
|
+
isMounted = false;
|
|
474
|
+
constructor(public node: MotionNode) {}
|
|
475
|
+
abstract mount(): void;
|
|
476
|
+
update(): void {}
|
|
477
|
+
unmount(): void {}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
export interface FeatureDefinition {
|
|
481
|
+
isEnabled: (props: Record<string, any>) => boolean;
|
|
482
|
+
Feature: new (node: MotionNode) => Feature;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
class AnimationFeature extends Feature {
|
|
486
|
+
private unmountControls?: () => void;
|
|
487
|
+
private subscribe(): void {
|
|
488
|
+
this.unmountControls?.();
|
|
489
|
+
this.unmountControls = undefined;
|
|
490
|
+
const { animate } = this.node.getProps();
|
|
491
|
+
if (isAnimationControls(animate)) this.unmountControls = animate.subscribe(this.node);
|
|
492
|
+
}
|
|
493
|
+
mount(): void {
|
|
494
|
+
this.subscribe();
|
|
495
|
+
}
|
|
496
|
+
update(): void {
|
|
497
|
+
if (this.node.getProps().animate !== this.node.prevProps?.animate) this.subscribe();
|
|
498
|
+
}
|
|
499
|
+
unmount(): void {
|
|
500
|
+
this.node.animationState.reset();
|
|
501
|
+
this.unmountControls?.();
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** Feature definitions every node has; gestures, drag and layout come from their own modules. */
|
|
506
|
+
export const featureDefinitions: Record<string, FeatureDefinition | undefined> = {
|
|
507
|
+
animation: { isEnabled: () => true, Feature: AnimationFeature },
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
/** Adds feature definitions (drag, pan, layout) to every node. */
|
|
511
|
+
export function loadFeatures(defs: Record<string, FeatureDefinition>): void {
|
|
512
|
+
for (const key in defs) featureDefinitions[key] = defs[key];
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// The node
|
|
516
|
+
|
|
517
|
+
const propEventHandlers = ['AnimationStart', 'AnimationComplete', 'Update', 'BeforeLayoutMeasure', 'LayoutMeasure', 'LayoutAnimationStart', 'LayoutAnimationComplete'];
|
|
518
|
+
|
|
519
|
+
/** Element to node, for parent lookups through the DOM and animate() on elements. */
|
|
520
|
+
export const nodeStore: WeakMap<object, MotionNode> = new WeakMap();
|
|
521
|
+
|
|
522
|
+
export interface MotionNodeOptions {
|
|
523
|
+
parent?: MotionNode | null;
|
|
524
|
+
props: Record<string, any>;
|
|
525
|
+
presence?: PresenceContext | null;
|
|
526
|
+
config?: ConfigContext | null;
|
|
527
|
+
blockInitialAnimation?: boolean;
|
|
528
|
+
svg?: boolean;
|
|
529
|
+
tag?: string;
|
|
530
|
+
/** Values the element was rendered with (the server style), when known. */
|
|
531
|
+
latestValues?: Record<string, any>;
|
|
532
|
+
/** Extra features for this node (drag, layout) on top of the shared ones. */
|
|
533
|
+
features?: Record<string, FeatureDefinition | undefined>;
|
|
534
|
+
/**
|
|
535
|
+
* Whether the node runs its own initial animation (its parent was mounted
|
|
536
|
+
* before it). By default: the parent already has an element.
|
|
537
|
+
*/
|
|
538
|
+
manuallyAnimateOnMount?: boolean;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
export class MotionNode {
|
|
542
|
+
current: (HTMLElement | SVGElement) | null = null;
|
|
543
|
+
type: 'html' | 'svg';
|
|
544
|
+
isSVGTag = false;
|
|
545
|
+
parent: MotionNode | null;
|
|
546
|
+
props: Record<string, any>;
|
|
547
|
+
prevProps?: Record<string, any>;
|
|
548
|
+
presence: PresenceContext | null;
|
|
549
|
+
prevPresence: PresenceContext | null = null;
|
|
550
|
+
config: ConfigContext;
|
|
551
|
+
depth: number;
|
|
552
|
+
children = new Set<MotionNode>();
|
|
553
|
+
enteringChildren?: Set<MotionNode>;
|
|
554
|
+
variantChildren?: Set<MotionNode>;
|
|
555
|
+
isVariantNode = false;
|
|
556
|
+
isControllingVariants = false;
|
|
557
|
+
shouldReduceMotion: boolean | null = null;
|
|
558
|
+
blockInitialAnimation: boolean;
|
|
559
|
+
manuallyAnimateOnMount: boolean;
|
|
560
|
+
values = new Map<string, MotionValue>();
|
|
561
|
+
latestValues: Record<string, any>;
|
|
562
|
+
baseTarget: Record<string, any>;
|
|
563
|
+
initialValues: Record<string, any>;
|
|
564
|
+
renderState: RenderState = createRenderState();
|
|
565
|
+
animationState!: AnimationState;
|
|
566
|
+
features: Record<string, Feature | undefined> = {};
|
|
567
|
+
projection?: any;
|
|
568
|
+
/** The LayoutGroup scope layoutId is namespaced by. */
|
|
569
|
+
layoutScope: any = null;
|
|
570
|
+
/** The mount batch of a compiled element (see engine/krate.ts). */
|
|
571
|
+
mountBatch = -1;
|
|
572
|
+
removeFromVariantTree?: () => void;
|
|
573
|
+
private valueSubscriptions = new Map<string, () => void>();
|
|
574
|
+
private prevMotionValues: Record<string, any> = {};
|
|
575
|
+
private events: Record<string, SubscriptionManager> = {};
|
|
576
|
+
private propEventSubscriptions: Record<string, () => void> = {};
|
|
577
|
+
private renderScheduledAt = -1;
|
|
578
|
+
private childSubscription?: () => void;
|
|
579
|
+
private reducedMotionOff?: () => void;
|
|
580
|
+
private extraFeatures?: Record<string, FeatureDefinition | undefined>;
|
|
581
|
+
|
|
582
|
+
constructor({ parent = null, props, presence = null, config = null, blockInitialAnimation, svg, tag, latestValues, features, manuallyAnimateOnMount }: MotionNodeOptions) {
|
|
583
|
+
this.extraFeatures = features;
|
|
584
|
+
this.type = (svg ?? isSVGComponent(tag)) ? 'svg' : 'html';
|
|
585
|
+
this.parent = parent;
|
|
586
|
+
this.props = props;
|
|
587
|
+
this.presence = presence;
|
|
588
|
+
this.config = config || {};
|
|
589
|
+
this.depth = parent ? parent.depth + 1 : 0;
|
|
590
|
+
this.blockInitialAnimation = Boolean(blockInitialAnimation);
|
|
591
|
+
this.isControllingVariants = isControllingVariants(props);
|
|
592
|
+
this.isVariantNode = isVariantNode(props);
|
|
593
|
+
if (this.isVariantNode) this.variantChildren = new Set();
|
|
594
|
+
this.manuallyAnimateOnMount = manuallyAnimateOnMount ?? Boolean(parent && parent.current);
|
|
595
|
+
const scraped = this.scrapeMotionValues(props, {});
|
|
596
|
+
this.latestValues = latestValues || makeLatestValues(props, childVariantContext(parent), presence, scraped);
|
|
597
|
+
this.baseTarget = { ...this.latestValues };
|
|
598
|
+
this.initialValues = props.initial ? { ...this.latestValues } : {};
|
|
599
|
+
for (const key in scraped) {
|
|
600
|
+
const v = scraped[key];
|
|
601
|
+
if (this.latestValues[key] !== undefined && isMotionValue(v)) v.set(this.latestValues[key]);
|
|
602
|
+
}
|
|
603
|
+
this.animationState = createAnimationState(this);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
notifyUpdate = (): void => this.notify('Update', this.latestValues);
|
|
607
|
+
|
|
608
|
+
render = (): void => {
|
|
609
|
+
if (!this.current) return;
|
|
610
|
+
this.build();
|
|
611
|
+
if (this.type === 'svg') renderSVG(this.current as SVGElement, this.renderState);
|
|
612
|
+
else renderHTML(this.current, this.renderState);
|
|
613
|
+
this.projection?.applyProjectionStyles?.(this.current.style, this.props.style);
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
scheduleRender = (): void => {
|
|
617
|
+
const now = time.now();
|
|
618
|
+
if (this.renderScheduledAt < now) {
|
|
619
|
+
this.renderScheduledAt = now;
|
|
620
|
+
frame.render(this.render, false, true);
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
scheduleRenderMicrotask(): void {
|
|
625
|
+
microtask.render(this.render);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
build(): void {
|
|
629
|
+
this.renderState = createRenderState();
|
|
630
|
+
if (this.type === 'svg') buildSVGAttrs(this.renderState, this.latestValues, this.isSVGTag, this.props.transformTemplate, this.props.style);
|
|
631
|
+
else buildHTMLStyles(this.renderState, this.latestValues, this.props.transformTemplate);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** Motion values (and forced transform values) found in style and, for SVG, in props. */
|
|
635
|
+
scrapeMotionValues(props: Record<string, any>, prevProps: Record<string, any>): Record<string, any> {
|
|
636
|
+
const style = styleObject(props.style);
|
|
637
|
+
const prevStyle = styleObject(prevProps.style);
|
|
638
|
+
const out: Record<string, any> = {};
|
|
639
|
+
for (const key in style) {
|
|
640
|
+
if (isMotionValue(style[key]) || (prevStyle && isMotionValue(prevStyle[key])) || isForcedMotionValue(key, props) || (this.getValue(key) as any)?.liveStyle !== undefined) out[key] = style[key];
|
|
641
|
+
}
|
|
642
|
+
if (this.type === 'svg') {
|
|
643
|
+
for (const key in props) {
|
|
644
|
+
if (isMotionValue(props[key]) || isMotionValue(prevProps[key])) {
|
|
645
|
+
out[transformProps.has(key) ? 'attr' + key.charAt(0).toUpperCase() + key.substring(1) : key] = props[key];
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return out;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
mount(instance: HTMLElement | SVGElement): void {
|
|
653
|
+
this.current = instance;
|
|
654
|
+
this.isSVGTag = this.type === 'svg' && instance.tagName.toLowerCase() === 'svg';
|
|
655
|
+
nodeStore.set(instance, this);
|
|
656
|
+
if (this.projection && !this.projection.instance) this.projection.mount(instance);
|
|
657
|
+
if (this.parent && this.isVariantNode && !this.isControllingVariants) this.removeFromVariantTree = this.parent.addVariantChild(this);
|
|
658
|
+
this.values.forEach((value, key) => this.bindToMotionValue(key, value));
|
|
659
|
+
this.updateReducedMotion();
|
|
660
|
+
if (this.config.reducedMotion === 'user' || this.config.reducedMotion === undefined) {
|
|
661
|
+
this.reducedMotionOff = onReducedMotionChange(() => this.updateReducedMotion());
|
|
662
|
+
}
|
|
663
|
+
this.parent?.addChild(this);
|
|
664
|
+
this.update(this.props, this.presence);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
private updateReducedMotion(): void {
|
|
668
|
+
if (!hasReducedMotionListener.current) initPrefersReducedMotion();
|
|
669
|
+
const cfg = this.config.reducedMotion;
|
|
670
|
+
this.shouldReduceMotion = cfg === 'never' ? false : cfg === 'always' ? true : prefersReducedMotion.current;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
unmount(): void {
|
|
674
|
+
this.projection?.unmount();
|
|
675
|
+
cancelFrame(this.notifyUpdate);
|
|
676
|
+
cancelFrame(this.render);
|
|
677
|
+
this.valueSubscriptions.forEach((remove) => remove());
|
|
678
|
+
this.valueSubscriptions.clear();
|
|
679
|
+
this.removeFromVariantTree?.();
|
|
680
|
+
this.parent?.removeChild(this);
|
|
681
|
+
this.reducedMotionOff?.();
|
|
682
|
+
for (const key in this.events) this.events[key].clear();
|
|
683
|
+
for (const key in this.features) {
|
|
684
|
+
const f = this.features[key];
|
|
685
|
+
if (f) {
|
|
686
|
+
f.unmount();
|
|
687
|
+
f.isMounted = false;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
this.features = {};
|
|
691
|
+
this.childSubscription?.();
|
|
692
|
+
if (this.current && nodeStore.get(this.current) === this) nodeStore.delete(this.current);
|
|
693
|
+
this.current = null;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
addChild(child: MotionNode): void {
|
|
697
|
+
this.children.add(child);
|
|
698
|
+
(this.enteringChildren ??= new Set()).add(child);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
removeChild(child: MotionNode): void {
|
|
702
|
+
this.children.delete(child);
|
|
703
|
+
this.enteringChildren?.delete(child);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
bindToMotionValue(key: string, value: MotionValue): void {
|
|
707
|
+
this.valueSubscriptions.get(key)?.();
|
|
708
|
+
const isTransform = transformProps.has(key);
|
|
709
|
+
const off = value.on('change', (latest) => {
|
|
710
|
+
this.latestValues[key] = latest;
|
|
711
|
+
if (this.props.onUpdate) frame.preRender(this.notifyUpdate);
|
|
712
|
+
if (isTransform && this.projection) this.projection.isTransformDirty = true;
|
|
713
|
+
this.scheduleRender();
|
|
714
|
+
});
|
|
715
|
+
this.valueSubscriptions.set(key, () => {
|
|
716
|
+
off();
|
|
717
|
+
if (value.owner) value.stop();
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
sortNodePosition(other: MotionNode): number {
|
|
722
|
+
if (!this.current || !other.current || this.type !== other.type) return 0;
|
|
723
|
+
return this.current.compareDocumentPosition(other.current) & 2 ? 1 : -1;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/** Mounts, updates and unmounts features after props changed. */
|
|
727
|
+
updateFeatures(): void {
|
|
728
|
+
const defs = this.extraFeatures ? { ...featureDefinitions, ...this.extraFeatures } : featureDefinitions;
|
|
729
|
+
for (const key in defs) {
|
|
730
|
+
const def = defs[key];
|
|
731
|
+
if (!def) continue;
|
|
732
|
+
const enabled = def.isEnabled(this.props);
|
|
733
|
+
if (!this.features[key] && enabled) this.features[key] = new def.Feature(this);
|
|
734
|
+
const f = this.features[key];
|
|
735
|
+
if (!f) continue;
|
|
736
|
+
if (f.isMounted) f.update();
|
|
737
|
+
else if (this.current) {
|
|
738
|
+
f.mount();
|
|
739
|
+
f.isMounted = true;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
measureViewportBox(): { x: { min: number; max: number }; y: { min: number; max: number } } {
|
|
745
|
+
if (!this.current) return { x: { min: 0, max: 0 }, y: { min: 0, max: 0 } };
|
|
746
|
+
const r = this.current.getBoundingClientRect();
|
|
747
|
+
return { x: { min: r.left, max: r.right }, y: { min: r.top, max: r.bottom } };
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
getStaticValue(key: string): any {
|
|
751
|
+
return this.latestValues[key];
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
setStaticValue(key: string, value: any): void {
|
|
755
|
+
this.latestValues[key] = value;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/** Applies new props: event handler props, motion values in style, text children motion values. */
|
|
759
|
+
update(props: Record<string, any>, presence: PresenceContext | null): void {
|
|
760
|
+
if (props.transformTemplate || this.props.transformTemplate) this.scheduleRender();
|
|
761
|
+
this.prevProps = this.props;
|
|
762
|
+
this.props = props;
|
|
763
|
+
this.prevPresence = this.presence;
|
|
764
|
+
this.presence = presence;
|
|
765
|
+
this.isControllingVariants = isControllingVariants(props);
|
|
766
|
+
for (const key of propEventHandlers) {
|
|
767
|
+
if (this.propEventSubscriptions[key]) {
|
|
768
|
+
this.propEventSubscriptions[key]();
|
|
769
|
+
delete this.propEventSubscriptions[key];
|
|
770
|
+
}
|
|
771
|
+
const listener = props['on' + key];
|
|
772
|
+
if (listener) this.propEventSubscriptions[key] = this.on(key, listener);
|
|
773
|
+
}
|
|
774
|
+
this.prevMotionValues = this.updateMotionValuesFromProps(this.scrapeMotionValues(props, this.prevProps || {}), this.prevMotionValues);
|
|
775
|
+
this.childSubscription?.();
|
|
776
|
+
this.childSubscription = undefined;
|
|
777
|
+
if (isMotionValue(props.children)) {
|
|
778
|
+
const mv = props.children;
|
|
779
|
+
this.childSubscription = mv.on('change', (latest: any) => {
|
|
780
|
+
if (this.current) this.current.textContent = `${latest}`;
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
private updateMotionValuesFromProps(next: Record<string, any>, prev: Record<string, any>): Record<string, any> {
|
|
786
|
+
for (const key in next) {
|
|
787
|
+
const n = next[key];
|
|
788
|
+
const p = prev[key];
|
|
789
|
+
if (isMotionValue(n)) {
|
|
790
|
+
this.addValue(key, n);
|
|
791
|
+
} else if (isMotionValue(p)) {
|
|
792
|
+
this.addValue(key, motionValue(n, { owner: this }));
|
|
793
|
+
} else if (p !== n) {
|
|
794
|
+
if (this.hasValue(key)) {
|
|
795
|
+
const existing = this.getValue(key) as MotionValue & { liveStyle?: boolean };
|
|
796
|
+
if (existing.liveStyle === true) existing.jump(n);
|
|
797
|
+
else if (!existing.hasAnimated) existing.set(n);
|
|
798
|
+
} else {
|
|
799
|
+
const latest = this.getStaticValue(key);
|
|
800
|
+
this.addValue(key, motionValue(latest !== undefined ? latest : n, { owner: this }));
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
for (const key in prev) if (next[key] === undefined) this.removeValue(key);
|
|
805
|
+
return next;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
getProps(): Record<string, any> {
|
|
809
|
+
return this.props;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
getVariant(name: string): Variant | undefined {
|
|
813
|
+
return this.props.variants ? this.props.variants[name] : undefined;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
getDefaultTransition(): Transition | undefined {
|
|
817
|
+
return this.props.transition ?? this.config.transition;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
getTransformPagePoint(): any {
|
|
821
|
+
return this.props.transformPagePoint ?? this.config.transformPagePoint;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
getClosestVariantNode(): MotionNode | undefined {
|
|
825
|
+
return this.isVariantNode ? this : this.parent ? this.parent.getClosestVariantNode() : undefined;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
addVariantChild(child: MotionNode): (() => void) | undefined {
|
|
829
|
+
const closest = this.getClosestVariantNode();
|
|
830
|
+
if (closest) {
|
|
831
|
+
closest.variantChildren?.add(child);
|
|
832
|
+
return () => closest.variantChildren!.delete(child);
|
|
833
|
+
}
|
|
834
|
+
return undefined;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
addValue(key: string, value: MotionValue): void {
|
|
838
|
+
const existing = this.values.get(key);
|
|
839
|
+
if (value !== existing) {
|
|
840
|
+
if (existing) this.removeValue(key);
|
|
841
|
+
this.bindToMotionValue(key, value);
|
|
842
|
+
this.values.set(key, value);
|
|
843
|
+
this.latestValues[key] = value.get();
|
|
844
|
+
if (this.current) this.scheduleRender();
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
removeValue(key: string): void {
|
|
849
|
+
this.values.delete(key);
|
|
850
|
+
const off = this.valueSubscriptions.get(key);
|
|
851
|
+
if (off) {
|
|
852
|
+
off();
|
|
853
|
+
this.valueSubscriptions.delete(key);
|
|
854
|
+
}
|
|
855
|
+
delete this.latestValues[key];
|
|
856
|
+
delete this.renderState.vars[key];
|
|
857
|
+
delete this.renderState.style[key];
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
hasValue(key: string): boolean {
|
|
861
|
+
return this.values.has(key);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
getValue(key: string, defaultValue?: any): MotionValue | undefined {
|
|
865
|
+
if (this.props.values && this.props.values[key]) return this.props.values[key];
|
|
866
|
+
let value = this.values.get(key);
|
|
867
|
+
if (value === undefined && defaultValue !== undefined) {
|
|
868
|
+
value = motionValue(defaultValue === null ? undefined : defaultValue, { owner: this });
|
|
869
|
+
this.addValue(key, value);
|
|
870
|
+
}
|
|
871
|
+
return value;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
/** The current value of `key`, read from the element when no motion value has it yet. */
|
|
875
|
+
readValue(key: string, target?: any): any {
|
|
876
|
+
let value =
|
|
877
|
+
this.latestValues[key] !== undefined || !this.current
|
|
878
|
+
? this.latestValues[key]
|
|
879
|
+
: (this.getBaseTargetFromProps(key) ?? readDOMValue(this.current, key, this.type === 'svg'));
|
|
880
|
+
if (value !== undefined && value !== null) {
|
|
881
|
+
if (typeof value === 'string' && (isNumericalString(value) || isZeroValueString(value))) value = parseFloat(value);
|
|
882
|
+
else if (!findValueType(value) && complex.test(target)) value = getAnimatableNone(key, target);
|
|
883
|
+
this.baseTarget[key] = isMotionValue(value) ? value.get() : value;
|
|
884
|
+
}
|
|
885
|
+
return isMotionValue(value) ? value.get() : value;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
private getBaseTargetFromProps(key: string): any {
|
|
889
|
+
if (this.type === 'svg' && this.props[key] !== undefined) return this.props[key];
|
|
890
|
+
const style = styleObject(this.props.style);
|
|
891
|
+
return style ? style[key] : undefined;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
setBaseTarget(key: string, value: any): void {
|
|
895
|
+
this.baseTarget[key] = value;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
getBaseTarget(key: string): any {
|
|
899
|
+
const { initial } = this.props;
|
|
900
|
+
let fromInitial: any;
|
|
901
|
+
if (typeof initial === 'string' || typeof initial === 'object') {
|
|
902
|
+
const v = resolveVariantFromProps(this.props, initial, this.presence?.custom);
|
|
903
|
+
if (v) fromInitial = v[key];
|
|
904
|
+
}
|
|
905
|
+
if (initial && fromInitial !== undefined) return fromInitial;
|
|
906
|
+
const target = this.getBaseTargetFromProps(key);
|
|
907
|
+
if (target !== undefined && !isMotionValue(target)) return target;
|
|
908
|
+
return this.initialValues[key] !== undefined && fromInitial === undefined ? undefined : this.baseTarget[key];
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
on(event: string, cb: (...args: any[]) => void): () => void {
|
|
912
|
+
if (!this.events[event]) this.events[event] = new SubscriptionManager();
|
|
913
|
+
return this.events[event].add(cb);
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
notify(event: string, ...args: any[]): void {
|
|
917
|
+
this.events[event]?.notify(...args);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
/** A style prop as an object (a server-rendered style string is parsed). */
|
|
922
|
+
export function styleObject(style: unknown): Record<string, any> | undefined {
|
|
923
|
+
if (!style) return undefined;
|
|
924
|
+
if (typeof style !== 'string') return style as Record<string, any>;
|
|
925
|
+
const out: Record<string, any> = {};
|
|
926
|
+
for (const part of style.split(';')) {
|
|
927
|
+
const i = part.indexOf(':');
|
|
928
|
+
if (i === -1) continue;
|
|
929
|
+
const name = part.slice(0, i).trim();
|
|
930
|
+
if (!name) continue;
|
|
931
|
+
const key = name.startsWith('--') ? name : name.replace(/-([a-z])/g, (_m, c: string) => c.toUpperCase());
|
|
932
|
+
out[key] = part.slice(i + 1).trim();
|
|
933
|
+
}
|
|
934
|
+
return out;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
export const scaleCorrectors: Record<string, any> = {};
|
|
938
|
+
|
|
939
|
+
export function isForcedMotionValue(key: string, { layout, layoutId }: Record<string, any>): boolean {
|
|
940
|
+
return transformProps.has(key) || key.startsWith('origin') || ((layout || layoutId !== undefined) && (!!scaleCorrectors[key] || key === 'opacity'));
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
/** Whether a prop is a Motion prop (never forwarded to the DOM element). */
|
|
944
|
+
const validMotionProps = new Set([
|
|
945
|
+
'animate',
|
|
946
|
+
'exit',
|
|
947
|
+
'variants',
|
|
948
|
+
'initial',
|
|
949
|
+
'style',
|
|
950
|
+
'values',
|
|
951
|
+
'transition',
|
|
952
|
+
'transformTemplate',
|
|
953
|
+
'custom',
|
|
954
|
+
'inherit',
|
|
955
|
+
'onBeforeLayoutMeasure',
|
|
956
|
+
'onAnimationStart',
|
|
957
|
+
'onAnimationComplete',
|
|
958
|
+
'onUpdate',
|
|
959
|
+
'onDragStart',
|
|
960
|
+
'onDrag',
|
|
961
|
+
'onDragEnd',
|
|
962
|
+
'onMeasureDragConstraints',
|
|
963
|
+
'onDirectionLock',
|
|
964
|
+
'onDragTransitionEnd',
|
|
965
|
+
'_dragX',
|
|
966
|
+
'_dragY',
|
|
967
|
+
'onHoverStart',
|
|
968
|
+
'onHoverEnd',
|
|
969
|
+
'onViewportEnter',
|
|
970
|
+
'onViewportLeave',
|
|
971
|
+
'globalTapTarget',
|
|
972
|
+
'ignoreStrict',
|
|
973
|
+
'viewport',
|
|
974
|
+
]);
|
|
975
|
+
|
|
976
|
+
export function isValidMotionProp(key: string): boolean {
|
|
977
|
+
return (
|
|
978
|
+
key.startsWith('while') ||
|
|
979
|
+
(key.startsWith('drag') && key !== 'draggable') ||
|
|
980
|
+
key.startsWith('layout') ||
|
|
981
|
+
key.startsWith('onTap') ||
|
|
982
|
+
key.startsWith('onPan') ||
|
|
983
|
+
key.startsWith('onLayout') ||
|
|
984
|
+
validMotionProps.has(key)
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
export { warnOnce };
|