start.mjs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { startWaapiAnimation } from 'motion-dom';
  2. import { noop } from 'motion-utils';
  3. import { optimizedAppearDataId } from './data-id.mjs';
  4. import { getOptimisedAppearId } from './get-appear-id.mjs';
  5. import { handoffOptimizedAppearAnimation } from './handoff.mjs';
  6. import { appearAnimationStore, appearComplete } from './store.mjs';
  7. import { appearStoreId } from './store-id.mjs';
  8. /**
  9. * A single time to use across all animations to manually set startTime
  10. * and ensure they're all in sync.
  11. */
  12. let startFrameTime;
  13. /**
  14. * A dummy animation to detect when Chrome is ready to start
  15. * painting the page and hold off from triggering the real animation
  16. * until then. We only need one animation to detect paint ready.
  17. *
  18. * https://bugs.chromium.org/p/chromium/issues/detail?id=1406850
  19. */
  20. let readyAnimation;
  21. /**
  22. * Keep track of animations that were suspended vs cancelled so we
  23. * can easily resume them when we're done measuring layout.
  24. */
  25. const suspendedAnimations = new Set();
  26. function resumeSuspendedAnimations() {
  27. suspendedAnimations.forEach((data) => {
  28. data.animation.play();
  29. data.animation.startTime = data.startTime;
  30. });
  31. suspendedAnimations.clear();
  32. }
  33. function startOptimizedAppearAnimation(element, name, keyframes, options, onReady) {
  34. // Prevent optimised appear animations if Motion has already started animating.
  35. if (window.MotionIsMounted) {
  36. return;
  37. }
  38. const id = element.dataset[optimizedAppearDataId];
  39. if (!id)
  40. return;
  41. window.MotionHandoffAnimation = handoffOptimizedAppearAnimation;
  42. const storeId = appearStoreId(id, name);
  43. if (!readyAnimation) {
  44. readyAnimation = startWaapiAnimation(element, name, [keyframes[0], keyframes[0]],
  45. /**
  46. * 10 secs is basically just a super-safe duration to give Chrome
  47. * long enough to get the animation ready.
  48. */
  49. { duration: 10000, ease: "linear" });
  50. appearAnimationStore.set(storeId, {
  51. animation: readyAnimation,
  52. startTime: null,
  53. });
  54. /**
  55. * If there's no readyAnimation then there's been no instantiation
  56. * of handoff animations.
  57. */
  58. window.MotionHandoffAnimation = handoffOptimizedAppearAnimation;
  59. window.MotionHasOptimisedAnimation = (elementId, valueName) => {
  60. if (!elementId)
  61. return false;
  62. /**
  63. * Keep a map of elementIds that have started animating. We check
  64. * via ID instead of Element because of hydration errors and
  65. * pre-hydration checks. We also actively record IDs as they start
  66. * animating rather than simply checking for data-appear-id as
  67. * this attrbute might be present but not lead to an animation, for
  68. * instance if the element's appear animation is on a different
  69. * breakpoint.
  70. */
  71. if (!valueName) {
  72. return appearComplete.has(elementId);
  73. }
  74. const animationId = appearStoreId(elementId, valueName);
  75. return Boolean(appearAnimationStore.get(animationId));
  76. };
  77. window.MotionHandoffMarkAsComplete = (elementId) => {
  78. if (appearComplete.has(elementId)) {
  79. appearComplete.set(elementId, true);
  80. }
  81. };
  82. window.MotionHandoffIsComplete = (elementId) => {
  83. return appearComplete.get(elementId) === true;
  84. };
  85. /**
  86. * We only need to cancel transform animations as
  87. * they're the ones that will interfere with the
  88. * layout animation measurements.
  89. */
  90. window.MotionCancelOptimisedAnimation = (elementId, valueName, frame, canResume) => {
  91. const animationId = appearStoreId(elementId, valueName);
  92. const data = appearAnimationStore.get(animationId);
  93. if (!data)
  94. return;
  95. if (frame && canResume === undefined) {
  96. /**
  97. * Wait until the end of the subsequent frame to cancel the animation
  98. * to ensure we don't remove the animation before the main thread has
  99. * had a chance to resolve keyframes and render.
  100. */
  101. frame.postRender(() => {
  102. frame.postRender(() => {
  103. data.animation.cancel();
  104. });
  105. });
  106. }
  107. else {
  108. data.animation.cancel();
  109. }
  110. if (frame && canResume) {
  111. suspendedAnimations.add(data);
  112. frame.render(resumeSuspendedAnimations);
  113. }
  114. else {
  115. appearAnimationStore.delete(animationId);
  116. /**
  117. * If there are no more animations left, we can remove the cancel function.
  118. * This will let us know when we can stop checking for conflicting layout animations.
  119. */
  120. if (!appearAnimationStore.size) {
  121. window.MotionCancelOptimisedAnimation = undefined;
  122. }
  123. }
  124. };
  125. window.MotionCheckAppearSync = (visualElement, valueName, value) => {
  126. const appearId = getOptimisedAppearId(visualElement);
  127. if (!appearId)
  128. return;
  129. const valueIsOptimised = window.MotionHasOptimisedAnimation?.(appearId, valueName);
  130. const externalAnimationValue = visualElement.props.values?.[valueName];
  131. if (!valueIsOptimised || !externalAnimationValue)
  132. return;
  133. const removeSyncCheck = value.on("change", (latestValue) => {
  134. if (externalAnimationValue.get() !== latestValue) {
  135. window.MotionCancelOptimisedAnimation?.(appearId, valueName);
  136. removeSyncCheck();
  137. }
  138. });
  139. return removeSyncCheck;
  140. };
  141. }
  142. const startAnimation = () => {
  143. readyAnimation.cancel();
  144. const appearAnimation = startWaapiAnimation(element, name, keyframes, options);
  145. /**
  146. * Record the time of the first started animation. We call performance.now() once
  147. * here and once in handoff to ensure we're getting
  148. * close to a frame-locked time. This keeps all animations in sync.
  149. */
  150. if (startFrameTime === undefined) {
  151. startFrameTime = performance.now();
  152. }
  153. appearAnimation.startTime = startFrameTime;
  154. appearAnimationStore.set(storeId, {
  155. animation: appearAnimation,
  156. startTime: startFrameTime,
  157. });
  158. if (onReady)
  159. onReady(appearAnimation);
  160. };
  161. appearComplete.set(id, false);
  162. if (readyAnimation.ready) {
  163. readyAnimation.ready.then(startAnimation).catch(noop);
  164. }
  165. else {
  166. startAnimation();
  167. }
  168. }
  169. export { startOptimizedAppearAnimation };