UNPKG

framer-motion

Version:

A simple and powerful JavaScript animation library

1 lines 7.9 kB
{"version":3,"file":"animate-view-layers.mjs","sources":["../../../../src/components/AnimateView/animate-view-layers.ts"],"sourcesContent":["import {\n AnimationPlaybackControls,\n applyGeneratorOptions,\n getValueTransition,\n getViewAnimationLayerInfo,\n getViewAnimations,\n GroupAnimation,\n mapEasingToNativeEasing,\n NativeAnimation,\n NativeAnimationWrapper,\n Target,\n Transition,\n} from \"motion-dom\"\nimport { secondsToMilliseconds } from \"motion-utils\"\nimport { ViewAnimationOptions, ViewAnimationType } from \"./types\"\n\n/**\n * Animate the pseudo-element layers React's ViewTransition has generated for\n * a named view boundary, either retiming the browser's animations or\n * replacing its crossfade with custom keyframes.\n */\nexport function animateViewLayers(\n name: string,\n animationType: ViewAnimationType,\n {\n transition,\n onAnimationStart,\n onAnimationComplete,\n ...props\n }: ViewAnimationOptions,\n types: string[]\n): VoidFunction {\n const layerAnimations: AnimationPlaybackControls[] = []\n const definition = props[animationType]\n const { transition: typeTransition, ...values } =\n (typeof definition === \"function\" ? definition(types) : definition) ||\n {}\n const hasValues = Object.keys(values).length > 0\n let hasMatchingAnimation = false\n\n for (const viewAnimation of getViewAnimations()) {\n if (viewAnimation.playState === \"finished\") continue\n\n const { effect } = viewAnimation\n if (!(effect instanceof KeyframeEffect) || !effect.pseudoElement)\n continue\n\n const info = getViewAnimationLayerInfo(effect.pseudoElement)\n if (!info || info.layer !== name) continue\n hasMatchingAnimation = true\n\n /**\n * Custom values replace the browser's crossfade (old/new layers).\n * The group layer carries the position/size morph, so keep it running\n * with Motion's timing.\n */\n if (hasValues && info.type !== \"group\") {\n viewAnimation.cancel()\n } else {\n const transitionName = info.type === \"group\" ? \"layout\" : \"\"\n let options = {\n ...getValueTransition(transition, transitionName),\n ...getValueTransition(typeTransition, transitionName),\n }\n options.duration = secondsToMilliseconds(options.duration ?? 0.3)\n options = applyGeneratorOptions(options)\n\n effect.updateTiming({\n delay: secondsToMilliseconds(options.delay ?? 0),\n duration: options.duration,\n easing: mapEasingToNativeEasing(\n options.ease,\n options.duration!\n ) as string,\n })\n layerAnimations.push(new NativeAnimationWrapper(viewAnimation))\n }\n }\n\n if (hasValues && hasMatchingAnimation) {\n layerAnimations.push(\n ...createLayerAnimations(\n name,\n animationType,\n values,\n transition,\n typeTransition\n )\n )\n }\n\n const animation = new GroupAnimation(layerAnimations)\n let active = true\n onAnimationStart?.(animation, animationType)\n if (onAnimationComplete) {\n animation.finished.then(() => {\n if (active) onAnimationComplete(animationType)\n })\n }\n\n return () => {\n active = false\n animation.cancel()\n }\n}\n\nfunction createLayerAnimations(\n layerName: string,\n animationType: ViewAnimationType,\n values: Target,\n defaultTransition: Transition | undefined,\n transition: Transition | undefined\n): AnimationPlaybackControls[] {\n const animations: AnimationPlaybackControls[] = []\n\n for (const [name, value] of Object.entries(values)) {\n let keyframes = value\n const options = {\n ...getValueTransition(defaultTransition, name),\n ...getValueTransition(transition, name),\n }\n\n options.duration &&= secondsToMilliseconds(options.duration)\n\n options.delay &&= secondsToMilliseconds(options.delay)\n\n if (name === \"opacity\" && !Array.isArray(keyframes)) {\n const initialValue = animationType === \"enter\" ? 0 : 1\n keyframes = [initialValue, keyframes]\n }\n\n const animation = new NativeAnimation({\n ...options,\n element: document.documentElement,\n name,\n pseudoElement: `::view-transition-${\n animationType === \"enter\" ? \"new\" : \"old\"\n }(${layerName})`,\n keyframes,\n })\n\n animations.push(animation)\n }\n\n return animations\n}\n"],"names":[],"mappings":";;;AAgBA;;;;AAIG;SACa,iBAAiB,CAC7B,IAAY,EACZ,aAAgC,EAChC,EACI,UAAU,EACV,gBAAgB,EAChB,mBAAmB,EACnB,GAAG,KAAK,EACW,EACvB,KAAe,EAAA;IAEf,MAAM,eAAe,GAAgC,EAAE;AACvD,IAAA,MAAM,UAAU,GAAG,KAAK,CAAC,aAAa,CAAC;IACvC,MAAM,EAAE,UAAU,EAAE,cAAc,EAAE,GAAG,MAAM,EAAE,GAC3C,CAAC,OAAO,U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