framer-motion
Version:
A simple and powerful JavaScript animation library
196 lines (193 loc) • 8.75 kB
JavaScript
"use client";
import { optimizedAppearDataAttribute } from 'motion-dom';
import { useContext, useRef, useInsertionEffect, useEffect } from 'react';
import { LazyContext } from '../../context/LazyContext.mjs';
import { MotionConfigContext } from '../../context/MotionConfigContext.mjs';
import { MotionContext } from '../../context/MotionContext/index.mjs';
import { PresenceContext } from '../../context/PresenceContext.mjs';
import { SwitchLayoutGroupContext } from '../../context/SwitchLayoutGroupContext.mjs';
import { isRefObject } from '../../utils/is-ref-object.mjs';
import { useIsomorphicLayoutEffect } from '../../utils/use-isomorphic-effect.mjs';
function useVisualElement(Component, visualState, props, createVisualElement, ProjectionNodeConstructor, isSVG) {
const { visualElement: parent } = useContext(MotionContext);
const lazyContext = useContext(LazyContext);
const presenceContext = useContext(PresenceContext);
const motionConfig = useContext(MotionConfigContext);
const reducedMotionConfig = motionConfig.reducedMotion;
const skipAnimations = motionConfig.skipAnimations;
const visualElementRef = useRef(null);
/**
* Track whether the component has been through React's commit phase.
* Used to detect when LazyMotion features load after the component has mounted.
*/
const hasMountedOnce = useRef(false);
/**
* If we haven't preloaded a renderer, check to see if we have one lazy-loaded
*/
createVisualElement =
createVisualElement ||
lazyContext.renderer;
if (!visualElementRef.current && createVisualElement) {
visualElementRef.current = createVisualElement(Component, {
visualState,
parent,
props,
presenceContext,
blockInitialAnimation: presenceContext
? presenceContext.initial === false
: false,
reducedMotionConfig,
skipAnimations,
isSVG,
});
/**
* If the component has already mounted before features loaded (e.g. via
* LazyMotion with async feature loading), we need to force the initial
* animation to run. Otherwise state changes that occurred before features
* loaded will be lost and the element will snap to its final state.
*/
if (hasMountedOnce.current && visualElementRef.current) {
visualElementRef.current.manuallyAnimateOnMount = true;
}
}
const visualElement = visualElementRef.current;
/**
* Load Motion gesture and animation features. These are rendered as renderless
* components so each feature can optionally make use of React lifecycle methods.
*/
const initialLayoutGroupConfig = useContext(SwitchLayoutGroupContext);
if (visualElement &&
!visualElement.projection &&
ProjectionNodeConstructor &&
(visualElement.type === "html" || visualElement.type === "svg")) {
createProjectionNode(visualElementRef.current, props, ProjectionNodeConstructor, initialLayoutGroupConfig);
}
useInsertionEffect(() => {
/**
* Skip the update if the visual element was created with these props.
* This isn't gated on the layout effect below having run, as a hidden
* <Activity> runs insertion effects but not layout effects, and
* props from renders made while hidden must still reach the element.
*/
if (visualElement && visualElement.props !== props) {
visualElement.update(props, presenceContext);
}
});
/**
* Cache this value as we want to know whether HandoffAppearAnimations
* was present on initial render - it will be deleted after this.
*/
const optimisedAppearId = props[optimizedAppearDataAttribute];
const wantsHandoff = useRef(Boolean(optimisedAppearId) &&
typeof window !== "undefined" &&
!window.MotionHandoffIsComplete?.(optimisedAppearId) &&
window.MotionHasOptimisedAnimation?.(optimisedAppearId));
/**
* Layout effects re-run when a hidden Suspense/Activity boundary is
* revealed, but passive effects only re-run if this component re-renders
* (not the case for memoized elements). Replay animations on every
* reconnection. Must be declared before the effect that sets hasMountedOnce.
*/
useIsomorphicLayoutEffect(() => {
if (!hasMountedOnce.current || !visualElement)
return;
visualElement.animationState?.animateChanges();
visualElement.enteringChildren = undefined;
}, []);
useIsomorphicLayoutEffect(() => {
/**
* Track that this component has mounted. This is used to detect when
* LazyMotion features load after the component has already committed.
*/
hasMountedOnce.current = true;
if (!visualElement)
return;
window.MotionIsMounted = true;
visualElement.updateFeatures();
visualElement.scheduleRenderMicrotask();
/**
* Ideally this function would always run in a useEffect.
*
* However, if we have optimised appear animations to handoff from,
* it needs to happen synchronously to ensure there's no flash of
* incorrect styles in the event of a hydration error.
*
* So if we detect a situation where optimised appear animations
* are running, we use useLayoutEffect to trigger animations.
*/
if (wantsHandoff.current && visualElement.animationState) {
visualElement.animationState.animateChanges();
}
});
useEffect(() => {
if (!visualElement)
return;
if (!wantsHandoff.current && visualElement.animationState) {
visualElement.animationState.animateChanges();
}
if (wantsHandoff.current) {
// This ensures all future calls to animateChanges() in this component will run in useEffect
queueMicrotask(() => {
window.MotionHandoffMarkAsComplete?.(optimisedAppearId);
});
wantsHandoff.current = false;
}
/**
* Now we've finished triggering animations for this element we
* can wipe the enteringChildren set for the next render.
*/
visualElement.enteringChildren = undefined;
});
return visualElement;
}
function createProjectionNode(visualElement, props, ProjectionNodeConstructor, initialPromotionConfig) {
const { layoutId, layout, drag, dragConstraints, layoutScroll, layoutRoot, layoutAnchor, layoutCrossfade, } = props;
const projection = (visualElement.projection =
new ProjectionNodeConstructor(visualElement.latestValues, props["data-framer-portal-id"]
? undefined
: getClosestProjectingNode(visualElement.parent)));
projection.setOptions({
layoutId,
layout,
alwaysMeasureLayout: Boolean(drag) || (dragConstraints && isRefObject(dragConstraints)),
visualElement,
/**
* TODO: Update options in an effect. This could be tricky as it'll be too late
* to update by the time layout animations run.
* We also need to fix this safeToRemove by linking it up to the one returned by usePresence,
* ensuring it gets called if there's no potential layout animations.
*
*/
animationType: typeof layout === "string" ? layout : "both",
initialPromotionConfig,
crossfade: layoutCrossfade,
layoutScroll,
layoutRoot,
layoutAnchor,
});
/**
* This node will take over from its layoutId's current lead, so snapshot
* the lead now, while rendering. By the time the lead unmounts (or this
* node mounts) React may already have committed DOM changes that come
* before it in the tree, like a sibling changing height, and a snapshot
* taken then starts the animation from a position never seen on screen.
*
* If React discards this render (e.g. it suspends), nothing else
* completes the update, so check it after the next frame as an
* unmounting node does.
*/
const lead = projection.getStack()?.lead;
if (lead && !projection.root.isUpdateBlocked()) {
lead.willUpdate();
lead.scheduleCheckAfterUnmount();
}
}
function getClosestProjectingNode(visualElement) {
if (!visualElement)
return undefined;
return visualElement.options.allowProjection !== false
? visualElement.projection
: getClosestProjectingNode(visualElement.parent);
}
export { useVisualElement };
//# sourceMappingURL=use-visual-element.mjs.map