{"version":3,"file":"animate-view.js","sources":["../../src/components/AnimateView/animate-view-layers.ts","../../src/components/AnimateView/hooks/use-reset-view-transitions.ts","../../src/components/AnimateView/shared-props.ts","../../src/components/AnimateView/index.tsx"],"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","import { useInsertionEffect } from \"react\"\n\nlet hasInsertedTransitionResetStyle = false\n\nexport function useResetViewTransitions(): void {\n useInsertionEffect(() => {\n if (hasInsertedTransitionResetStyle) return\n hasInsertedTransitionResetStyle = true\n const style = document.createElement(\"style\")\n style.textContent = `\n ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) {\n animation-timing-function: linear !important;\n }\n `\n document.head.appendChild(style)\n }, [])\n}\n","import { AnimateViewProps } from \"./types\"\n\nexport const sharedProps = new Map()\n","\"use client\"\n\nimport React, {\n ComponentType,\n PropsWithChildren,\n useInsertionEffect,\n} from \"react\"\nimport { animateViewLayers } from \"./animate-view-layers\"\nimport { useResetViewTransitions } from \"./hooks/use-reset-view-transitions\"\nimport { sharedProps } from \"./shared-props\"\nimport { AnimateViewProps, ViewAnimationType } from \"./types\"\n\ntype ViewTransitionEvent = (\n instance: { name: string },\n types: string[]\n) => VoidFunction\n\ninterface ViewTransitionProps {\n name?: string\n onEnter: ViewTransitionEvent\n onExit: ViewTransitionEvent\n onShare: ViewTransitionEvent\n onUpdate: ViewTransitionEvent\n}\n\n// Keep the entry point importable with React 18 and its type definitions.\nconst ViewTransition = (\n React as unknown as {\n ViewTransition?: ComponentType>\n }\n).ViewTransition\n\n/**\n * Animate enter, exit, update and shared view transitions.\n * Requires React and React DOM 19.3 or later.\n */\nexport function AnimateView({\n children,\n ...props\n}: PropsWithChildren): React.ReactElement {\n if (!ViewTransition) {\n throw new Error(\"AnimateView requires React 19.3 or later.\")\n }\n\n useResetViewTransitions()\n const { name } = props\n\n useInsertionEffect(() => {\n if (!name) return\n sharedProps.set(name, props)\n return () => {\n if (sharedProps.get(name) === props) sharedProps.delete(name)\n }\n })\n\n const createAnimation =\n (type: ViewAnimationType): ViewTransitionEvent =>\n ({ name: viewName }, types) =>\n animateViewLayers(\n viewName,\n type,\n sharedProps.get(viewName) || props,\n types\n )\n\n return (\n \n {children}\n \n )\n}\n"],"names":["getViewAnimations","getViewAnimationLayerInfo","getValueTransition","secondsToMilliseconds","applyGeneratorOptions","mapEasingToNativeEasing","NativeAnimationWrapper","GroupAnimation","NativeAnimation","useInsertionEffect","_jsx"],"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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