motion-start 0.0.1

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.
Files changed (233) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +39 -0
  3. package/package.json +64 -0
  4. package/src/animation/UseAnimatedState.svelte +86 -0
  5. package/src/animation/UseAnimation.svelte +61 -0
  6. package/src/animation/animate.ts +78 -0
  7. package/src/animation/animation-controls.ts +101 -0
  8. package/src/animation/types.ts +83 -0
  9. package/src/animation/use-animated-state.ts +1 -0
  10. package/src/animation/use-animation.ts +74 -0
  11. package/src/animation/utils/default-transitions.ts +70 -0
  12. package/src/animation/utils/easing.ts +55 -0
  13. package/src/animation/utils/is-animatable.ts +42 -0
  14. package/src/animation/utils/is-animation-controls.ts +17 -0
  15. package/src/animation/utils/is-keyframes-target.ts +17 -0
  16. package/src/animation/utils/transitions.ts +218 -0
  17. package/src/animation/utils/variant-resolvers.ts +15 -0
  18. package/src/components/AnimatePresence/AnimatePresence.svelte +180 -0
  19. package/src/components/AnimatePresence/PresenceChild/PresenceChild.svelte +78 -0
  20. package/src/components/AnimatePresence/PresenceChild/index.ts +7 -0
  21. package/src/components/AnimatePresence/PresenceChild/types.ts +10 -0
  22. package/src/components/AnimatePresence/index.ts +46 -0
  23. package/src/components/AnimatePresence/types.ts +79 -0
  24. package/src/components/AnimatePresence/use-presence.ts +90 -0
  25. package/src/components/AnimateSharedLayout/AnimateSharedLayout.svelte +239 -0
  26. package/src/components/AnimateSharedLayout/index.ts +11 -0
  27. package/src/components/AnimateSharedLayout/types.ts +111 -0
  28. package/src/components/AnimateSharedLayout/utils/batcher.ts +96 -0
  29. package/src/components/AnimateSharedLayout/utils/crossfader.ts +260 -0
  30. package/src/components/AnimateSharedLayout/utils/rotate.ts +48 -0
  31. package/src/components/AnimateSharedLayout/utils/stack.ts +160 -0
  32. package/src/components/LazyMotion/LazyMotion.svelte +82 -0
  33. package/src/components/LazyMotion/index.ts +42 -0
  34. package/src/components/LazyMotion/types.ts +58 -0
  35. package/src/components/MotionConfig/MotionConfig.svelte +56 -0
  36. package/src/components/MotionConfig/MotionConfigScaleCorrection.ts +47 -0
  37. package/src/components/MotionConfig/index.ts +20 -0
  38. package/src/components/MotionDiv.svelte +8 -0
  39. package/src/context/DOMcontext.ts +21 -0
  40. package/src/context/LayoutGroupContext.ts +13 -0
  41. package/src/context/LazyContext.ts +18 -0
  42. package/src/context/MotionConfigContext.ts +48 -0
  43. package/src/context/MotionContext/MotionContext.svelte +27 -0
  44. package/src/context/MotionContext/MotionContextProvider.svelte +22 -0
  45. package/src/context/MotionContext/UseCreateMotionContext.svelte +34 -0
  46. package/src/context/MotionContext/create.ts +1 -0
  47. package/src/context/MotionContext/index.ts +14 -0
  48. package/src/context/MotionContext/utils.ts +29 -0
  49. package/src/context/PresenceContext.ts +26 -0
  50. package/src/context/ScaleCorrectionProvider.svelte +27 -0
  51. package/src/context/SharedLayoutContext.ts +29 -0
  52. package/src/events/UseDomEvent.svelte +67 -0
  53. package/src/events/UsePointerEvent.svelte +76 -0
  54. package/src/events/event-info.ts +69 -0
  55. package/src/events/types.ts +15 -0
  56. package/src/events/use-dom-event.ts +48 -0
  57. package/src/events/use-pointer-event.ts +29 -0
  58. package/src/events/utils.ts +25 -0
  59. package/src/gestures/PanSession.ts +298 -0
  60. package/src/gestures/UseFocusGesture.svelte +31 -0
  61. package/src/gestures/UseGestures.svelte +17 -0
  62. package/src/gestures/UseHoverGesture.svelte +40 -0
  63. package/src/gestures/UsePanGesture.svelte +58 -0
  64. package/src/gestures/UseTapGesture.svelte +77 -0
  65. package/src/gestures/drag/UseDrag.svelte +55 -0
  66. package/src/gestures/drag/UseDragControls.svelte +145 -0
  67. package/src/gestures/drag/VisualElementDragControls.ts +632 -0
  68. package/src/gestures/drag/types.ts +307 -0
  69. package/src/gestures/drag/use-drag-controls.ts +148 -0
  70. package/src/gestures/drag/use-drag.ts +15 -0
  71. package/src/gestures/drag/utils/constraints.ts +157 -0
  72. package/src/gestures/drag/utils/lock.ts +69 -0
  73. package/src/gestures/types.ts +257 -0
  74. package/src/gestures/use-focus-gesture.ts +16 -0
  75. package/src/gestures/use-gestures.ts +2 -0
  76. package/src/gestures/use-hover-gesture.ts +10 -0
  77. package/src/gestures/use-pan-gesture.ts +22 -0
  78. package/src/gestures/use-tap-gesture.ts +14 -0
  79. package/src/gestures/utils/event-type.ts +24 -0
  80. package/src/gestures/utils/is-node-or-child.ts +31 -0
  81. package/src/index.ts +104 -0
  82. package/src/motion/Motion.svelte +246 -0
  83. package/src/motion/MotionSSR.svelte +244 -0
  84. package/src/motion/features/AnimationState.svelte +29 -0
  85. package/src/motion/features/Exit.svelte +32 -0
  86. package/src/motion/features/UseFeatures.svelte +39 -0
  87. package/src/motion/features/animations.ts +22 -0
  88. package/src/motion/features/definitions.ts +49 -0
  89. package/src/motion/features/drag.ts +24 -0
  90. package/src/motion/features/gestures.ts +24 -0
  91. package/src/motion/features/layout/Animate.svelte +314 -0
  92. package/src/motion/features/layout/Animate.ts +9 -0
  93. package/src/motion/features/layout/AnimateLayoutContextProvider.svelte +14 -0
  94. package/src/motion/features/layout/Measure.svelte +98 -0
  95. package/src/motion/features/layout/Measure.ts +9 -0
  96. package/src/motion/features/layout/MeasureContextProvider.svelte +32 -0
  97. package/src/motion/features/layout/index.ts +20 -0
  98. package/src/motion/features/layout/types.ts +71 -0
  99. package/src/motion/features/layout/utils.ts +40 -0
  100. package/src/motion/features/types.ts +53 -0
  101. package/src/motion/features/use-features.ts +16 -0
  102. package/src/motion/index.ts +64 -0
  103. package/src/motion/types.ts +278 -0
  104. package/src/motion/utils/UseLayoutId.svelte +18 -0
  105. package/src/motion/utils/UseVisualElement.svelte +104 -0
  106. package/src/motion/utils/UseVisualState.svelte +137 -0
  107. package/src/motion/utils/is-forced-motion-value.ts +23 -0
  108. package/src/motion/utils/make-renderless-component.ts +17 -0
  109. package/src/motion/utils/should-inhert-variant.ts +6 -0
  110. package/src/motion/utils/use-motion-ref.ts +41 -0
  111. package/src/motion/utils/use-visual-element.ts +13 -0
  112. package/src/motion/utils/use-visual-state.ts +24 -0
  113. package/src/motion/utils/valid-prop.ts +80 -0
  114. package/src/render/dom/M.svelte +16 -0
  115. package/src/render/dom/UseRender.svelte +37 -0
  116. package/src/render/dom/create-motion-class.ts +12 -0
  117. package/src/render/dom/create-visual-element.ts +22 -0
  118. package/src/render/dom/featureBundle.ts +22 -0
  119. package/src/render/dom/motion-minimal.ts +22 -0
  120. package/src/render/dom/motion-proxy.ts +107 -0
  121. package/src/render/dom/motion.ts +62 -0
  122. package/src/render/dom/projection/convert-to-relative.ts +40 -0
  123. package/src/render/dom/projection/default-scale-correctors.ts +138 -0
  124. package/src/render/dom/projection/measure.ts +28 -0
  125. package/src/render/dom/projection/relative-set.ts +27 -0
  126. package/src/render/dom/projection/scale-correction.ts +22 -0
  127. package/src/render/dom/projection/types.ts +13 -0
  128. package/src/render/dom/projection/utils.ts +69 -0
  129. package/src/render/dom/svg-visual-element.ts +114 -0
  130. package/src/render/dom/types.ts +32 -0
  131. package/src/render/dom/use-render.ts +11 -0
  132. package/src/render/dom/utils/UseInitialMotionProps.svelte +26 -0
  133. package/src/render/dom/utils/batch-layout.ts +77 -0
  134. package/src/render/dom/utils/camel-to-dash.ts +20 -0
  135. package/src/render/dom/utils/create-config.ts +33 -0
  136. package/src/render/dom/utils/css-variables-conversion.ts +121 -0
  137. package/src/render/dom/utils/filter-props.ts +55 -0
  138. package/src/render/dom/utils/is-css-variable.ts +18 -0
  139. package/src/render/dom/utils/is-svg-component.ts +41 -0
  140. package/src/render/dom/utils/parse-dom-variant.ts +26 -0
  141. package/src/render/dom/utils/unit-conversion.ts +258 -0
  142. package/src/render/dom/utils/use-html-props.ts +2 -0
  143. package/src/render/dom/utils/use-svg-props.ts +1 -0
  144. package/src/render/dom/value-types/animatable-none.ts +24 -0
  145. package/src/render/dom/value-types/defaults.ts +30 -0
  146. package/src/render/dom/value-types/dimensions.ts +27 -0
  147. package/src/render/dom/value-types/find.ts +31 -0
  148. package/src/render/dom/value-types/get-as-type.ts +21 -0
  149. package/src/render/dom/value-types/number.ts +83 -0
  150. package/src/render/dom/value-types/test.ts +17 -0
  151. package/src/render/dom/value-types/type-auto.ts +21 -0
  152. package/src/render/dom/value-types/type-int.ts +23 -0
  153. package/src/render/dom/value-types/types.ts +8 -0
  154. package/src/render/html/UseHTMLProps.svelte +33 -0
  155. package/src/render/html/UseInitialMotionValues.svelte +27 -0
  156. package/src/render/html/UseStyle.svelte +47 -0
  157. package/src/render/html/config-motion.ts +23 -0
  158. package/src/render/html/supported-elements.ts +10 -0
  159. package/src/render/html/types.ts +64 -0
  160. package/src/render/html/use-props.ts +14 -0
  161. package/src/render/html/utils/build-projection-transform.ts +53 -0
  162. package/src/render/html/utils/build-styles.ts +121 -0
  163. package/src/render/html/utils/build-transform.ts +79 -0
  164. package/src/render/html/utils/create-render-state.ts +18 -0
  165. package/src/render/html/utils/render.ts +22 -0
  166. package/src/render/html/utils/scrape-motion-values.ts +26 -0
  167. package/src/render/html/utils/transform.ts +51 -0
  168. package/src/render/html/visual-element.ts +129 -0
  169. package/src/render/index.ts +703 -0
  170. package/src/render/svg/UseSVGProps.svelte +34 -0
  171. package/src/render/svg/config-motion.ts +51 -0
  172. package/src/render/svg/lowercase-elements.ts +35 -0
  173. package/src/render/svg/supported-elements.ts +10 -0
  174. package/src/render/svg/types.ts +51 -0
  175. package/src/render/svg/use-props.ts +14 -0
  176. package/src/render/svg/utils/build-attrs.ts +58 -0
  177. package/src/render/svg/utils/camel-case-attrs.ts +27 -0
  178. package/src/render/svg/utils/create-render-state.ts +17 -0
  179. package/src/render/svg/utils/path.ts +49 -0
  180. package/src/render/svg/utils/render.ts +22 -0
  181. package/src/render/svg/utils/scrape-motion-values.ts +26 -0
  182. package/src/render/svg/utils/transform-origin.ts +30 -0
  183. package/src/render/svg/visual-element.ts +44 -0
  184. package/src/render/types.ts +148 -0
  185. package/src/render/utils/animation-state.ts +375 -0
  186. package/src/render/utils/animation.ts +167 -0
  187. package/src/render/utils/compare-by-depth.ts +18 -0
  188. package/src/render/utils/flat-tree.ts +35 -0
  189. package/src/render/utils/is-draggable.ts +17 -0
  190. package/src/render/utils/lifecycles.ts +172 -0
  191. package/src/render/utils/motion-values.ts +59 -0
  192. package/src/render/utils/projection.ts +38 -0
  193. package/src/render/utils/setters.ts +910 -0
  194. package/src/render/utils/state.ts +113 -0
  195. package/src/render/utils/types.ts +12 -0
  196. package/src/render/utils/variants.ts +76 -0
  197. package/src/types/geometry.ts +91 -0
  198. package/src/types.ts +1088 -0
  199. package/src/utils/UseUnmountEffect.svelte +11 -0
  200. package/src/utils/array.ts +18 -0
  201. package/src/utils/each-axis.ts +15 -0
  202. package/src/utils/errors.ts +22 -0
  203. package/src/utils/fix-process-env.ts +22 -0
  204. package/src/utils/geometry/delta-apply.ts +162 -0
  205. package/src/utils/geometry/delta-calc.ts +89 -0
  206. package/src/utils/geometry/index.ts +83 -0
  207. package/src/utils/is-browser.ts +12 -0
  208. package/src/utils/is-numerical-string.ts +15 -0
  209. package/src/utils/is-ref-object.ts +16 -0
  210. package/src/utils/noop.ts +15 -0
  211. package/src/utils/resolve-value.ts +23 -0
  212. package/src/utils/shallow-compare.ts +23 -0
  213. package/src/utils/subscription-manager.ts +49 -0
  214. package/src/utils/time-conversion.ts +18 -0
  215. package/src/utils/transform.ts +120 -0
  216. package/src/utils/use-constant.ts +23 -0
  217. package/src/utils/use-cycle.ts +78 -0
  218. package/src/utils/use-force-update.ts +7 -0
  219. package/src/utils/use-isomorphic-effect.ts +8 -0
  220. package/src/utils/use-reduced-motion.ts +70 -0
  221. package/src/utils/use-unmount-effect.ts +8 -0
  222. package/src/value/index.ts +409 -0
  223. package/src/value/scroll/use-element-scroll.ts +73 -0
  224. package/src/value/scroll/use-viewport-scroll.ts +81 -0
  225. package/src/value/scroll/utils.ts +76 -0
  226. package/src/value/use-combine-values.ts +53 -0
  227. package/src/value/use-motion-template.ts +57 -0
  228. package/src/value/use-motion-value.ts +27 -0
  229. package/src/value/use-spring.ts +84 -0
  230. package/src/value/use-transform.ts +216 -0
  231. package/src/value/use-velocity.ts +44 -0
  232. package/src/value/utils/is-motion-value.ts +15 -0
  233. package/src/value/utils/resolve-motion-value.ts +29 -0
@@ -0,0 +1,703 @@
1
+ /**
2
+ based on framer-motion@4.1.17,
3
+ Copyright (c) 2018 Framer B.V.
4
+ */
5
+ import type { VisualElement, VisualElementConfig, VisualElementOptions } from "./types";
6
+
7
+ /**
8
+ based on framer-motion@4.1.1,
9
+ Copyright (c) 2018 Framer B.V.
10
+ */
11
+ import sync, { cancelSync } from 'framesync';
12
+ import { pipe } from 'popmotion';
13
+ import { __assign, __read, __spreadArray } from 'tslib';
14
+ import { Presence } from '../components/AnimateSharedLayout/types.js';
15
+ import { eachAxis } from '../utils/each-axis.js';
16
+ import { applyBoxTransforms, removeBoxTransforms } from '../utils/geometry/delta-apply.js';
17
+ import { calcRelativeBox, updateBoxDelta } from '../utils/geometry/delta-calc.js';
18
+ import { axisBox } from '../utils/geometry/index.js';
19
+ import { motionValue } from '../value/index.js';
20
+ import { isMotionValue } from '../value/utils/is-motion-value.js';
21
+ import { setCurrentViewportBox } from './dom/projection/relative-set.js';
22
+ import { buildLayoutProjectionTransform } from './html/utils/build-projection-transform.js';
23
+ import { variantPriorityOrder } from './utils/animation-state.js';
24
+ import { FlatTree } from './utils/flat-tree.js';
25
+ import { isDraggable } from './utils/is-draggable.js';
26
+ import { createLifecycles } from './utils/lifecycles.js';
27
+ import { updateMotionValuesFromProps } from './utils/motion-values.js';
28
+ import { updateLayoutDeltas } from './utils/projection.js';
29
+ import { createLayoutState, createProjectionState } from './utils/state.js';
30
+ import { checkIfControllingVariants, checkIfVariantNode, isVariantLabel } from './utils/variants.js';
31
+
32
+ // TODO: make abstract class - future plans to match latest FramerMotion
33
+ var visualElement = function <Instance = any, MutableState = any, Options extends {} = {}>({ treeType, build, getBaseTarget, makeTargetAnimatable, measureViewportBox, render: renderInstance, readValueFromInstance, resetTransform, restoreTransform, removeValueFromRenderState, sortNodePosition, scrapeMotionValuesFromProps, }: VisualElementConfig<Instance, MutableState, Options>) {
34
+ return function ({ parent, props, presenceId, blockInitialAnimation, visualState, }: VisualElementOptions<Instance, any>, options?: Options) {
35
+ if (options === void 0) { options = <Options>{}; }
36
+ var latestValues = visualState.latestValues, renderState = visualState.renderState;
37
+ /**
38
+ * The instance of the render-specific node that will be hydrated by the
39
+ * exposed React ref. So for example, this visual element can host a
40
+ * HTMLElement, plain object, or Three.js object. The functions provided
41
+ * in VisualElementConfig allow us to interface with this instance.
42
+ */
43
+ var instance;
44
+ /**
45
+ * Manages the subscriptions for a visual element's lifecycle, for instance
46
+ * onRender and onViewportBoxUpdate.
47
+ */
48
+ var lifecycles = createLifecycles();
49
+ /**
50
+ *
51
+ */
52
+ var projection = createProjectionState();
53
+ /**
54
+ * A reference to the nearest projecting parent. This is either
55
+ * undefined if we haven't looked for the nearest projecting parent,
56
+ * false if there is no parent performing layout projection, or a reference
57
+ * to the projecting parent.
58
+ */
59
+ var projectionParent;
60
+ /**
61
+ * This is a reference to the visual state of the "lead" visual element.
62
+ * Usually, this will be this visual element. But if it shares a layoutId
63
+ * with other visual elements, only one of them will be designated lead by
64
+ * AnimateSharedLayout. All the other visual elements will take on the visual
65
+ * appearance of the lead while they crossfade to it.
66
+ */
67
+ var leadProjection = projection;
68
+ var leadLatestValues = latestValues;
69
+ var unsubscribeFromLeadVisualElement;
70
+ /**
71
+ * The latest layout measurements and calculated projections. This
72
+ * is seperate from the target projection data in visualState as
73
+ * many visual elements might point to the same piece of visualState as
74
+ * a target, whereas they might each have different layouts and thus
75
+ * projection calculations needed to project into the same viewport box.
76
+ */
77
+ var layoutState = createLayoutState();
78
+ /**
79
+ *
80
+ */
81
+ var crossfader;
82
+ /**
83
+ * Keep track of whether the viewport box has been updated since the
84
+ * last time the layout projection was re-calculated.
85
+ */
86
+ var hasViewportBoxUpdated = false;
87
+ /**
88
+ * A map of all motion values attached to this visual element. Motion
89
+ * values are source of truth for any given animated value. A motion
90
+ * value might be provided externally by the component via props.
91
+ */
92
+ var values = new Map();
93
+ /**
94
+ * A map of every subscription that binds the provided or generated
95
+ * motion values onChange listeners to this visual element.
96
+ */
97
+ var valueSubscriptions = new Map();
98
+ /**
99
+ * A reference to the previously-provided motion values as returned
100
+ * from scrapeMotionValuesFromProps. We use the keys in here to determine
101
+ * if any motion values need to be removed after props are updated.
102
+ */
103
+ var prevMotionValues = {};
104
+ /**
105
+ * x/y motion values that track the progress of initiated layout
106
+ * animations.
107
+ *
108
+ * TODO: Target for removal
109
+ */
110
+ var projectionTargetProgress;
111
+ /**
112
+ * When values are removed from all animation props we need to search
113
+ * for a fallback value to animate to. These values are tracked in baseTarget.
114
+ */
115
+ var baseTarget = __assign({}, latestValues);
116
+ // Internal methods ========================
117
+ /**
118
+ * On mount, this will be hydrated with a callback to disconnect
119
+ * this visual element from its parent on unmount.
120
+ */
121
+ var removeFromVariantTree;
122
+ /**
123
+ *
124
+ */
125
+ function render() {
126
+ if (!instance)
127
+ return;
128
+ if (element.isProjectionReady()) {
129
+ /**
130
+ * Apply the latest user-set transforms to the targetBox to produce the targetBoxFinal.
131
+ * This is the final box that we will then project into by calculating a transform delta and
132
+ * applying it to the corrected box.
133
+ */
134
+ applyBoxTransforms(leadProjection.targetFinal, leadProjection.target, leadLatestValues);
135
+ /**
136
+ * Update the delta between the corrected box and the final target box, after
137
+ * user-set transforms are applied to it. This will be used by the renderer to
138
+ * create a transform style that will reproject the element from its actual layout
139
+ * into the desired bounding box.
140
+ */
141
+ updateBoxDelta(layoutState.deltaFinal, layoutState.layoutCorrected, leadProjection.targetFinal, latestValues);
142
+ }
143
+ triggerBuild();
144
+ renderInstance(instance, renderState);
145
+ }
146
+ function triggerBuild() {
147
+ var valuesToRender = latestValues;
148
+ if (crossfader && crossfader.isActive()) {
149
+ var crossfadedValues = crossfader.getCrossfadeState(element);
150
+ if (crossfadedValues)
151
+ valuesToRender = crossfadedValues;
152
+ }
153
+ build(element, renderState, valuesToRender, leadProjection, layoutState, options, props);
154
+ }
155
+ function update() {
156
+ lifecycles.notifyUpdate(latestValues);
157
+ }
158
+ function updateLayoutProjection() {
159
+ if (!element.isProjectionReady())
160
+ return;
161
+ var delta = layoutState.delta, treeScale = layoutState.treeScale;
162
+ var prevTreeScaleX = treeScale.x;
163
+ var prevTreeScaleY = treeScale.y;
164
+ var prevDeltaTransform = layoutState.deltaTransform;
165
+ updateLayoutDeltas(layoutState, leadProjection, element.path, latestValues);
166
+ hasViewportBoxUpdated &&
167
+ element.notifyViewportBoxUpdate(leadProjection.target, delta);
168
+ hasViewportBoxUpdated = false;
169
+ var deltaTransform = buildLayoutProjectionTransform(delta, treeScale);
170
+ if (deltaTransform !== prevDeltaTransform ||
171
+ // Also compare calculated treeScale, for values that rely on this only for scale correction
172
+ prevTreeScaleX !== treeScale.x ||
173
+ prevTreeScaleY !== treeScale.y) {
174
+ element.scheduleRender();
175
+ }
176
+ layoutState.deltaTransform = deltaTransform;
177
+ }
178
+ function updateTreeLayoutProjection() {
179
+ element.layoutTree.forEach(fireUpdateLayoutProjection);
180
+ }
181
+ /**
182
+ *
183
+ */
184
+ function bindToMotionValue(key, value) {
185
+ var removeOnChange = value.onChange(function (latestValue) {
186
+ latestValues[key] = latestValue;
187
+ props.onUpdate && sync.update(update, false, true);
188
+ });
189
+ var removeOnRenderRequest = value.onRenderRequest(element.scheduleRender);
190
+ valueSubscriptions.set(key, function () {
191
+ removeOnChange();
192
+ removeOnRenderRequest();
193
+ });
194
+ }
195
+ /**
196
+ * Any motion values that are provided to the element when created
197
+ * aren't yet bound to the element, as this would technically be impure.
198
+ * However, we iterate through the motion values and set them to the
199
+ * initial values for this component.
200
+ *
201
+ * TODO: This is impure and we should look at changing this to run on mount.
202
+ * Doing so will break some tests but this isn't neccessarily a breaking change,
203
+ * more a reflection of the test.
204
+ */
205
+ var initialMotionValues = scrapeMotionValuesFromProps(props);
206
+ for (var key in initialMotionValues) {
207
+ var value = initialMotionValues[key];
208
+ if (latestValues[key] !== undefined && isMotionValue(value)) {
209
+ value.set(latestValues[key], false);
210
+ }
211
+ }
212
+ /**
213
+ * Determine what role this visual element should take in the variant tree.
214
+ */
215
+ var isControllingVariants = checkIfControllingVariants(props);
216
+ var isVariantNode = checkIfVariantNode(props);
217
+ var element: VisualElement<Instance, any> = __assign(__assign({ treeType: treeType,
218
+ /**
219
+ * This is a mirror of the internal instance prop, which keeps
220
+ * VisualElement type-compatible with React's RefObject.
221
+ */
222
+ current: null,
223
+ /**
224
+ * The depth of this visual element within the visual element tree.
225
+ */
226
+ depth: parent ? parent.depth + 1 : 0, parent: parent, children: new Set(),
227
+ /**
228
+ * An ancestor path back to the root visual element. This is used
229
+ * by layout projection to quickly recurse back up the tree.
230
+ */
231
+ path: parent ? __spreadArray(__spreadArray([], __read(parent.path)), [parent]) : [], layoutTree: parent ? parent.layoutTree : new FlatTree(),
232
+ /**
233
+ *
234
+ */
235
+ presenceId: presenceId,
236
+ projection: projection,
237
+ /**
238
+ * If this component is part of the variant tree, it should track
239
+ * any children that are also part of the tree. This is essentially
240
+ * a shadow tree to simplify logic around how to stagger over children.
241
+ */
242
+ variantChildren: isVariantNode ? new Set() : undefined,
243
+ /**
244
+ * Whether this instance is visible. This can be changed imperatively
245
+ * by AnimateSharedLayout, is analogous to CSS's visibility in that
246
+ * hidden elements should take up layout, and needs enacting by the configured
247
+ * render function.
248
+ */
249
+ isVisible: undefined,
250
+ /**
251
+ * Normally, if a component is controlled by a parent's variants, it can
252
+ * rely on that ancestor to trigger animations further down the tree.
253
+ * However, if a component is created after its parent is mounted, the parent
254
+ * won't trigger that mount animation so the child needs to.
255
+ *
256
+ * TODO: This might be better replaced with a method isParentMounted
257
+ */
258
+ manuallyAnimateOnMount: Boolean(parent === null || parent === void 0 ? void 0 : parent.isMounted()),
259
+ /**
260
+ * This can be set by AnimatePresence to force components that mount
261
+ * at the same time as it to mount as if they have initial={false} set.
262
+ */
263
+ blockInitialAnimation: blockInitialAnimation,
264
+ /**
265
+ * Determine whether this component has mounted yet. This is mostly used
266
+ * by variant children to determine whether they need to trigger their
267
+ * own animations on mount.
268
+ */
269
+ isMounted: function () { return Boolean(instance); }, mount: function (newInstance) {
270
+ instance = element.current = newInstance;
271
+ element.pointTo(element);
272
+ if (isVariantNode && parent && !isControllingVariants) {
273
+ removeFromVariantTree = parent === null || parent === void 0 ? void 0 : parent.addVariantChild(element);
274
+ }
275
+ parent === null || parent === void 0 ? void 0 : parent.children.add(element);
276
+ },
277
+ /**
278
+ *
279
+ */
280
+ unmount: function () {
281
+ cancelSync.update(update);
282
+ cancelSync.render(render);
283
+ cancelSync.preRender(element.updateLayoutProjection);
284
+ valueSubscriptions.forEach(function (remove) { return remove(); });
285
+ element.stopLayoutAnimation();
286
+ element.layoutTree.remove(element);
287
+ removeFromVariantTree === null || removeFromVariantTree === void 0 ? void 0 : removeFromVariantTree();
288
+ parent === null || parent === void 0 ? void 0 : parent.children.delete(element);
289
+ unsubscribeFromLeadVisualElement === null || unsubscribeFromLeadVisualElement === void 0 ? void 0 : unsubscribeFromLeadVisualElement();
290
+ lifecycles.clearAllListeners();
291
+ },
292
+ /**
293
+ * Add a child visual element to our set of children.
294
+ */
295
+ addVariantChild: function (child) {
296
+ var _a;
297
+ var closestVariantNode = element.getClosestVariantNode();
298
+ if (closestVariantNode) {
299
+ (_a = closestVariantNode.variantChildren) === null || _a === void 0 ? void 0 : _a.add(child);
300
+ return function () { return closestVariantNode.variantChildren.delete(child); };
301
+ }
302
+ },
303
+ sortNodePosition: function (other) {
304
+ /**
305
+ * If these nodes aren't even of the same type we can't compare their depth.
306
+ */
307
+ if (!sortNodePosition || treeType !== other.treeType)
308
+ return 0;
309
+ return sortNodePosition(element.getInstance(), other.getInstance());
310
+ },
311
+ /**
312
+ * Returns the closest variant node in the tree starting from
313
+ * this visual element.
314
+ */
315
+ getClosestVariantNode: function () {
316
+ return isVariantNode ? element : parent === null || parent === void 0 ? void 0 : parent.getClosestVariantNode();
317
+ },
318
+ /**
319
+ * A method that schedules an update to layout projections throughout
320
+ * the tree. We inherit from the parent so there's only ever one
321
+ * job scheduled on the next frame - that of the root visual element.
322
+ */
323
+ scheduleUpdateLayoutProjection: parent
324
+ ? parent.scheduleUpdateLayoutProjection
325
+ : function () {
326
+ return sync.preRender(element.updateTreeLayoutProjection, false, true);
327
+ },
328
+ /**
329
+ * Expose the latest layoutId prop.
330
+ */
331
+ getLayoutId: function () { return props.layoutId; },
332
+ /**
333
+ * Returns the current instance.
334
+ */
335
+ getInstance: function () { return instance; },
336
+ /**
337
+ * Get/set the latest static values.
338
+ */
339
+ getStaticValue: function (key) { return latestValues[key]; }, setStaticValue: function (key, value) { return (latestValues[key] = value); },
340
+ /**
341
+ * Returns the latest motion value state. Currently only used to take
342
+ * a snapshot of the visual element - perhaps this can return the whole
343
+ * visual state
344
+ */
345
+ getLatestValues: function () { return latestValues; },
346
+ /**
347
+ * Set the visiblity of the visual element. If it's changed, schedule
348
+ * a render to reflect these changes.
349
+ */
350
+ setVisibility: function (visibility) {
351
+ if (element.isVisible === visibility)
352
+ return;
353
+ element.isVisible = visibility;
354
+ element.scheduleRender();
355
+ },
356
+ /**
357
+ * Make a target animatable by Popmotion. For instance, if we're
358
+ * trying to animate width from 100px to 100vw we need to measure 100vw
359
+ * in pixels to determine what we really need to animate to. This is also
360
+ * pluggable to support Framer's custom value types like Color,
361
+ * and CSS variables.
362
+ */
363
+ makeTargetAnimatable: function (target, canMutate) {
364
+ if (canMutate === void 0) { canMutate = true; }
365
+ return makeTargetAnimatable(element, target, props, canMutate);
366
+ },
367
+ // Motion values ========================
368
+ /**
369
+ * Add a motion value and bind it to this visual element.
370
+ */
371
+ addValue: function (key, value) {
372
+ // Remove existing value if it exists
373
+ if (element.hasValue(key))
374
+ element.removeValue(key);
375
+ values.set(key, value);
376
+ latestValues[key] = value.get();
377
+ bindToMotionValue(key, value);
378
+ },
379
+ /**
380
+ * Remove a motion value and unbind any active subscriptions.
381
+ */
382
+ removeValue: function (key) {
383
+ var _a;
384
+ values.delete(key);
385
+ (_a = valueSubscriptions.get(key)) === null || _a === void 0 ? void 0 : _a();
386
+ valueSubscriptions.delete(key);
387
+ delete latestValues[key];
388
+ removeValueFromRenderState(key, renderState);
389
+ },
390
+ /**
391
+ * Check whether we have a motion value for this key
392
+ */
393
+ hasValue: function (key) { return values.has(key); },
394
+ /**
395
+ * Get a motion value for this key. If called with a default
396
+ * value, we'll create one if none exists.
397
+ */
398
+ getValue: function (key, defaultValue) {
399
+ var value = values.get(key);
400
+ if (value === undefined && defaultValue !== undefined) {
401
+ value = motionValue(defaultValue);
402
+ element.addValue(key, value);
403
+ }
404
+ return value;
405
+ },
406
+ /**
407
+ * Iterate over our motion values.
408
+ */
409
+ forEachValue: function (callback) { return values.forEach(callback); },
410
+ /**
411
+ * If we're trying to animate to a previously unencountered value,
412
+ * we need to check for it in our state and as a last resort read it
413
+ * directly from the instance (which might have performance implications).
414
+ */
415
+ readValue: function (key) { var _a; return (_a = latestValues[key]) !== null && _a !== void 0 ? _a : readValueFromInstance(instance, key, options); },
416
+ /**
417
+ * Set the base target to later animate back to. This is currently
418
+ * only hydrated on creation and when we first read a value.
419
+ */
420
+ setBaseTarget: function (key, value) {
421
+ baseTarget[key] = value;
422
+ },
423
+ /**
424
+ * Find the base target for a value thats been removed from all animation
425
+ * props.
426
+ */
427
+ getBaseTarget: function (key) {
428
+ if (getBaseTarget) {
429
+ var target = getBaseTarget(props, key);
430
+ if (target !== undefined && !isMotionValue(target))
431
+ return target;
432
+ }
433
+ return baseTarget[key];
434
+ } }, lifecycles), {
435
+ /**
436
+ * Build the renderer state based on the latest visual state.
437
+ */
438
+ build: function () {
439
+ triggerBuild();
440
+ return renderState;
441
+ },
442
+ /**
443
+ * Schedule a render on the next animation frame.
444
+ */
445
+ scheduleRender: function () {
446
+ sync.render(render, false, true);
447
+ },
448
+ /**
449
+ * Synchronously fire render. It's prefered that we batch renders but
450
+ * in many circumstances, like layout measurement, we need to run this
451
+ * synchronously. However in those instances other measures should be taken
452
+ * to batch reads/writes.
453
+ */
454
+ syncRender: render,
455
+ /**
456
+ * Update the provided props. Ensure any newly-added motion values are
457
+ * added to our map, old ones removed, and listeners updated.
458
+ */
459
+ setProps: function (newProps) {
460
+ props = newProps;
461
+ lifecycles.updatePropListeners(newProps);
462
+ prevMotionValues = updateMotionValuesFromProps(element, scrapeMotionValuesFromProps(props), prevMotionValues);
463
+ }, getProps: function () { return props; },
464
+ // Variants ==============================
465
+ /**
466
+ * Returns the variant definition with a given name.
467
+ */
468
+ getVariant: function (name) { var _a; return (_a = props.variants) === null || _a === void 0 ? void 0 : _a[name]; },
469
+ /**
470
+ * Returns the defined default transition on this component.
471
+ */
472
+ getDefaultTransition: function () { return props.transition; },
473
+ /**
474
+ * Used by child variant nodes to get the closest ancestor variant props.
475
+ */
476
+ getVariantContext: function (startAtParent) {
477
+ if (startAtParent === void 0) { startAtParent = false; }
478
+ if (startAtParent)
479
+ return parent === null || parent === void 0 ? void 0 : parent.getVariantContext();
480
+ if (!isControllingVariants) {
481
+ var context_1 = (parent === null || parent === void 0 ? void 0 : parent.getVariantContext()) || {};
482
+ if (props.initial !== undefined) {
483
+ context_1.initial = props.initial;
484
+ }
485
+ return context_1;
486
+ }
487
+ var context = {};
488
+ for (var i = 0; i < numVariantProps; i++) {
489
+ var name_1 = variantProps[i];
490
+ var prop = props[name_1];
491
+ if (isVariantLabel(prop) || prop === false) {
492
+ context[name_1] = prop;
493
+ }
494
+ }
495
+ return context;
496
+ },
497
+ // Layout projection ==============================
498
+ /**
499
+ * Enable layout projection for this visual element. Won't actually
500
+ * occur until we also have hydrated layout measurements.
501
+ */
502
+ enableLayoutProjection: function () {
503
+ projection.isEnabled = true;
504
+ element.layoutTree.add(element);
505
+ },
506
+ /**
507
+ * Lock the projection target, for instance when dragging, so
508
+ * nothing else can try and animate it.
509
+ */
510
+ lockProjectionTarget: function () {
511
+ projection.isTargetLocked = true;
512
+ },
513
+ unlockProjectionTarget: function () {
514
+ element.stopLayoutAnimation();
515
+ projection.isTargetLocked = false;
516
+ }, getLayoutState: function () { return layoutState; }, setCrossfader: function (newCrossfader) {
517
+ crossfader = newCrossfader;
518
+ }, isProjectionReady: function () {
519
+ return projection.isEnabled &&
520
+ projection.isHydrated &&
521
+ layoutState.isHydrated;
522
+ },
523
+ /**
524
+ * Start a layout animation on a given axis.
525
+ */
526
+ startLayoutAnimation: function (axis, transition, isRelative) {
527
+ if (isRelative === void 0) { isRelative = false; }
528
+ var progress = element.getProjectionAnimationProgress()[axis];
529
+ var _a = isRelative
530
+ ? projection.relativeTarget[axis]
531
+ : projection.target[axis], min = _a.min, max = _a.max;
532
+ var length = max - min;
533
+ progress.clearListeners();
534
+ progress.set(min);
535
+ progress.set(min); // Set twice to hard-reset velocity
536
+ progress.onChange(function (v) {
537
+ element.setProjectionTargetAxis(axis, v, v + length, isRelative);
538
+ });
539
+ return element.animateMotionValue(axis, progress, 0, transition);
540
+ },
541
+ /**
542
+ * Stop layout animations.
543
+ */
544
+ stopLayoutAnimation: function () {
545
+ eachAxis(function (axis) {
546
+ return element.getProjectionAnimationProgress()[axis].stop();
547
+ });
548
+ },
549
+ /**
550
+ * Measure the current viewport box with or without transforms.
551
+ * Only measures axis-aligned boxes, rotate and skew must be manually
552
+ * removed with a re-render to work.
553
+ */
554
+ measureViewportBox: function (withTransform) {
555
+ if (withTransform === void 0) { withTransform = true; }
556
+ var viewportBox = measureViewportBox(instance, options);
557
+ if (!withTransform)
558
+ removeBoxTransforms(viewportBox, latestValues);
559
+ return viewportBox;
560
+ },
561
+ /**
562
+ * Get the motion values tracking the layout animations on each
563
+ * axis. Lazy init if not already created.
564
+ */
565
+ getProjectionAnimationProgress: function () {
566
+ projectionTargetProgress || (projectionTargetProgress = {
567
+ x: motionValue(0),
568
+ y: motionValue(0),
569
+ });
570
+ return projectionTargetProgress;
571
+ },
572
+ /**
573
+ * Update the projection of a single axis. Schedule an update to
574
+ * the tree layout projection.
575
+ */
576
+ setProjectionTargetAxis: function (axis, min, max, isRelative) {
577
+ if (isRelative === void 0) { isRelative = false; }
578
+ var target;
579
+ if (isRelative) {
580
+ if (!projection.relativeTarget) {
581
+ projection.relativeTarget = axisBox();
582
+ }
583
+ target = projection.relativeTarget[axis];
584
+ }
585
+ else {
586
+ projection.relativeTarget = undefined;
587
+ target = projection.target[axis];
588
+ }
589
+ projection.isHydrated = true;
590
+ target.min = min;
591
+ target.max = max;
592
+ // Flag that we want to fire the onViewportBoxUpdate event handler
593
+ hasViewportBoxUpdated = true;
594
+ lifecycles.notifySetAxisTarget();
595
+ },
596
+ /**
597
+ * Rebase the projection target on top of the provided viewport box
598
+ * or the measured layout. This ensures that non-animating elements
599
+ * don't fall out of sync differences in measurements vs projections
600
+ * after a page scroll or other relayout.
601
+ */
602
+ rebaseProjectionTarget: function (force, box) {
603
+ if (box === void 0) { box = layoutState.layout; }
604
+ var _a = element.getProjectionAnimationProgress(), x = _a.x, y = _a.y;
605
+ var shouldRebase = !projection.relativeTarget &&
606
+ !projection.isTargetLocked &&
607
+ !x.isAnimating() &&
608
+ !y.isAnimating();
609
+ if (force || shouldRebase) {
610
+ eachAxis(function (axis) {
611
+ var _a = box[axis], min = _a.min, max = _a.max;
612
+ element.setProjectionTargetAxis(axis, min, max);
613
+ });
614
+ }
615
+ },
616
+ /**
617
+ * Notify the visual element that its layout is up-to-date.
618
+ * Currently Animate.tsx uses this to check whether a layout animation
619
+ * needs to be performed.
620
+ */
621
+ notifyLayoutReady: function (config) {
622
+ setCurrentViewportBox(element);
623
+ element.notifyLayoutUpdate(layoutState.layout, element.prevViewportBox || layoutState.layout, config);
624
+ },
625
+ /**
626
+ * Temporarily reset the transform of the instance.
627
+ */
628
+ resetTransform: function () { return resetTransform(element, instance, props); }, restoreTransform: function () { return restoreTransform(instance, renderState); }, updateLayoutProjection: updateLayoutProjection,
629
+ updateTreeLayoutProjection: function () {
630
+ element.layoutTree.forEach(fireResolveRelativeTargetBox);
631
+ /**
632
+ * Schedule the projection updates at the end of the current preRender
633
+ * step. This will ensure that all layout trees will first resolve
634
+ * relative projection boxes into viewport boxes, and *then*
635
+ * update projections.
636
+ */
637
+ sync.preRender(updateTreeLayoutProjection, false, true);
638
+ // sync.postRender(() => element.scheduleUpdateLayoutProjection())
639
+ },
640
+ getProjectionParent: function () {
641
+ if (projectionParent === undefined) {
642
+ var foundParent = false;
643
+ // Search backwards through the tree path
644
+ for (var i = element.path.length - 1; i >= 0; i--) {
645
+ var ancestor = element.path[i];
646
+ if (ancestor.projection.isEnabled) {
647
+ foundParent = ancestor;
648
+ break;
649
+ }
650
+ }
651
+ projectionParent = foundParent;
652
+ }
653
+ return projectionParent;
654
+ },
655
+ resolveRelativeTargetBox: function () {
656
+ var relativeParent = element.getProjectionParent();
657
+ if (!projection.relativeTarget || !relativeParent)
658
+ return;
659
+ calcRelativeBox(projection, relativeParent.projection);
660
+ if (isDraggable(relativeParent)) {
661
+ var target = projection.target;
662
+ applyBoxTransforms(target, target, relativeParent.getLatestValues());
663
+ }
664
+ },
665
+ shouldResetTransform: function () {
666
+ return Boolean(props._layoutResetTransform);
667
+ },
668
+ /**
669
+ *
670
+ */
671
+ pointTo: function (newLead) {
672
+ leadProjection = newLead.projection;
673
+ leadLatestValues = newLead.getLatestValues();
674
+ /**
675
+ * Subscribe to lead component's layout animations
676
+ */
677
+ unsubscribeFromLeadVisualElement === null || unsubscribeFromLeadVisualElement === void 0 ? void 0 : unsubscribeFromLeadVisualElement();
678
+ unsubscribeFromLeadVisualElement = pipe(newLead.onSetAxisTarget(element.scheduleUpdateLayoutProjection), newLead.onLayoutAnimationComplete(function () {
679
+ var _a;
680
+ if (element.isPresent) {
681
+ element.presence = Presence.Present;
682
+ }
683
+ else {
684
+ (_a = element.layoutSafeToRemove) === null || _a === void 0 ? void 0 : _a.call(element);
685
+ }
686
+ }));
687
+ },
688
+ // TODO: Clean this up
689
+ isPresent: true, presence: Presence.Entering });
690
+ return element;
691
+ };
692
+ };
693
+ function fireResolveRelativeTargetBox(child) {
694
+ child.resolveRelativeTargetBox();
695
+ }
696
+ function fireUpdateLayoutProjection(child) {
697
+ child.updateLayoutProjection();
698
+ }
699
+ var variantProps = __spreadArray(["initial"], __read(variantPriorityOrder));
700
+ var numVariantProps = variantProps.length;
701
+
702
+ export { visualElement };
703
+