index.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { isAnimationControls } from '../../../animation/utils/is-animation-controls.mjs';
  2. import { createAnimationState } from '../../../render/utils/animation-state.mjs';
  3. import { Feature } from '../Feature.mjs';
  4. class AnimationFeature extends Feature {
  5. /**
  6. * We dynamically generate the AnimationState manager as it contains a reference
  7. * to the underlying animation library. We only want to load that if we load this,
  8. * so people can optionally code split it out using the `m` component.
  9. */
  10. constructor(node) {
  11. super(node);
  12. node.animationState || (node.animationState = createAnimationState(node));
  13. }
  14. updateAnimationControlsSubscription() {
  15. const { animate } = this.node.getProps();
  16. if (isAnimationControls(animate)) {
  17. this.unmountControls = animate.subscribe(this.node);
  18. }
  19. }
  20. /**
  21. * Subscribe any provided AnimationControls to the component's VisualElement
  22. */
  23. mount() {
  24. this.updateAnimationControlsSubscription();
  25. }
  26. update() {
  27. const { animate } = this.node.getProps();
  28. const { animate: prevAnimate } = this.node.prevProps || {};
  29. if (animate !== prevAnimate) {
  30. this.updateAnimationControlsSubscription();
  31. }
  32. }
  33. unmount() {
  34. this.node.animationState.reset();
  35. this.unmountControls?.();
  36. }
  37. }
  38. export { AnimationFeature };