{"version":3,"names":["isWindowAvailable","setDummyPosition","snapshots","Animations","PREDEFINED_WEB_ANIMATIONS_ID","CUSTOM_WEB_ANIMATIONS_ID","animationNameToIndex","Map","animationNameList","isObserverSet","configureWebLayoutAnimations","document","getElementById","predefinedAnimationsStyleTag","createElement","id","onload","sheet","console","error","animationName","insertRule","style","customAnimationsStyleTag","head","appendChild","insertWebAnimation","keyframe","styleTag","unshift","set","i","length","nextAnimationName","nextAnimationIndex","get","undefined","Error","removeWebAnimation","_styleTag$sheet","currentAnimationIndex","deleteRule","splice","delete","timeoutScale","frameDurationMs","minimumFrames","scheduleAnimationCleanup","animationDuration","timeoutValue","Math","max","setTimeout","reattachElementToAncestor","child","parent","childSnapshot","removedAfterAnimation","originalOnAnimationEnd","onanimationend","event","removeChild","call","findDescendantWithExitingAnimation","node","root","reanimatedDummy","children","Array","from","checkIfScreenWasChanged","mutationTarget","_mutationTarget$react","_mutationTarget$react2","_mutationTarget$react3","reactFiberKey","key","Object","keys","startsWith","memoizedProps","navigation","addHTMLMutationObserver","observer","MutationObserver","mutationsList","rootMutation","target","removedNodes","observe","body","childList","subtree","areDOMRectsEqual","r1","r2","x","y","width","height"],"sources":["domUtils.ts"],"sourcesContent":["'use strict';\n\nimport type { ReanimatedHTMLElement } from '../../js-reanimated';\nimport { isWindowAvailable } from '../../PlatformChecker';\nimport { setDummyPosition, snapshots } from './componentStyle';\nimport { Animations } from './config';\nimport type { AnimationNames } from './config';\n\nconst PREDEFINED_WEB_ANIMATIONS_ID = 'ReanimatedPredefinedWebAnimationsStyle';\nconst CUSTOM_WEB_ANIMATIONS_ID = 'ReanimatedCustomWebAnimationsStyle';\n\n// Since we cannot remove keyframe from DOM by its name, we have to store its id\nconst animationNameToIndex = new Map();\nconst animationNameList: string[] = [];\n\nlet isObserverSet = false;\n\n/**\n * Creates `HTMLStyleElement`, inserts it into DOM and then inserts CSS rules into the stylesheet.\n * If style element already exists, nothing happens.\n */\nexport function configureWebLayoutAnimations() {\n if (\n !isWindowAvailable() || // Without this check SSR crashes because document is undefined (NextExample on CI)\n document.getElementById(PREDEFINED_WEB_ANIMATIONS_ID) !== null\n ) {\n return;\n }\n\n const predefinedAnimationsStyleTag = document.createElement('style');\n predefinedAnimationsStyleTag.id = PREDEFINED_WEB_ANIMATIONS_ID;\n\n predefinedAnimationsStyleTag.onload = () => {\n if (!predefinedAnimationsStyleTag.sheet) {\n console.error(\n '[Reanimated] Failed to create layout animations stylesheet.'\n );\n return;\n }\n\n for (const animationName in Animations) {\n predefinedAnimationsStyleTag.sheet.insertRule(\n Animations[animationName as AnimationNames].style\n );\n }\n };\n\n const customAnimationsStyleTag = document.createElement('style');\n customAnimationsStyleTag.id = CUSTOM_WEB_ANIMATIONS_ID;\n\n document.head.appendChild(predefinedAnimationsStyleTag);\n document.head.appendChild(customAnimationsStyleTag);\n}\n\nexport function insertWebAnimation(animationName: string, keyframe: string) {\n // Without this check SSR crashes because document is undefined (NextExample on CI)\n if (!isWindowAvailable()) {\n return;\n }\n\n const styleTag = document.getElementById(\n CUSTOM_WEB_ANIMATIONS_ID\n ) as HTMLStyleElement;\n\n if (!styleTag.sheet) {\n console.error(\n '[Reanimated] Failed to create layout animations stylesheet.'\n );\n return;\n }\n\n styleTag.sheet.insertRule(keyframe, 0);\n animationNameList.unshift(animationName);\n animationNameToIndex.set(animationName, 0);\n\n for (let i = 1; i < animationNameList.length; ++i) {\n const nextAnimationName = animationNameList[i];\n const nextAnimationIndex = animationNameToIndex.get(nextAnimationName);\n\n if (nextAnimationIndex === undefined) {\n throw new Error('[Reanimated] Failed to obtain animation index.');\n }\n\n animationNameToIndex.set(animationNameList[i], nextAnimationIndex + 1);\n }\n}\n\nfunction removeWebAnimation(animationName: string) {\n // Without this check SSR crashes because document is undefined (NextExample on CI)\n if (!isWindowAvailable()) {\n return;\n }\n\n const styleTag = document.getElementById(\n CUSTOM_WEB_ANIMATIONS_ID\n ) as HTMLStyleElement;\n\n const currentAnimationIndex = animationNameToIndex.get(animationName);\n\n if (currentAnimationIndex === undefined) {\n throw new Error('[Reanimated] Failed to obtain animation index.');\n }\n\n styleTag.sheet?.deleteRule(currentAnimationIndex);\n animationNameList.splice(currentAnimationIndex, 1);\n animationNameToIndex.delete(animationName);\n\n for (let i = currentAnimationIndex; i < animationNameList.length; ++i) {\n const nextAnimationName = animationNameList[i];\n const nextAnimationIndex = animationNameToIndex.get(nextAnimationName);\n\n if (nextAnimationIndex === undefined) {\n throw new Error('[Reanimated] Failed to obtain animation index.');\n }\n\n animationNameToIndex.set(animationNameList[i], nextAnimationIndex - 1);\n }\n}\n\nconst timeoutScale = 1.25; // We use this value to enlarge timeout duration. It can prove useful if animation lags.\nconst frameDurationMs = 16; // Just an approximation.\nconst minimumFrames = 10;\n\nexport function scheduleAnimationCleanup(\n animationName: string,\n animationDuration: number\n) {\n // If duration is very short, we want to keep remove delay to at least 10 frames\n // In our case it is exactly 160/1099 s, which is approximately 0.15s\n const timeoutValue = Math.max(\n animationDuration * timeoutScale * 1000,\n animationDuration + frameDurationMs * minimumFrames\n );\n\n setTimeout(() => removeWebAnimation(animationName), timeoutValue);\n}\n\nfunction reattachElementToAncestor(child: ReanimatedHTMLElement, parent: Node) {\n const childSnapshot = snapshots.get(child);\n\n if (!childSnapshot) {\n console.error('[Reanimated] Failed to obtain snapshot.');\n return;\n }\n\n // We use that so we don't end up in infinite loop\n child.removedAfterAnimation = true;\n parent.appendChild(child);\n\n setDummyPosition(child, childSnapshot);\n\n const originalOnAnimationEnd = child.onanimationend;\n\n child.onanimationend = function (event: AnimationEvent) {\n parent.removeChild(child);\n\n // Given that this function overrides onAnimationEnd, it won't be null\n originalOnAnimationEnd?.call(this, event);\n };\n}\n\nfunction findDescendantWithExitingAnimation(\n node: ReanimatedHTMLElement,\n root: Node\n) {\n if (node.reanimatedDummy && node.removedAfterAnimation === undefined) {\n reattachElementToAncestor(node, root);\n }\n\n const children = Array.from(node.children);\n\n for (let i = 0; i < children.length; ++i) {\n findDescendantWithExitingAnimation(\n children[i] as ReanimatedHTMLElement,\n root\n );\n }\n}\n\ntype FiberNodeKey = `__reactFiber${string}`;\n\ninterface FiberNode {\n memoizedProps?: {\n navigation?: unknown;\n };\n\n child?: FiberNode;\n}\n\ntype WithFiberNode = {\n [key: FiberNodeKey]: FiberNode;\n};\n\ntype MaybeWithFiberNode = Partial;\n\nfunction checkIfScreenWasChanged(\n mutationTarget: ReanimatedHTMLElement & MaybeWithFiberNode\n) {\n let reactFiberKey: FiberNodeKey = '__reactFiber';\n\n for (const key of Object.keys(mutationTarget)) {\n if (key.startsWith('__reactFiber')) {\n reactFiberKey = key as FiberNodeKey;\n break;\n }\n }\n\n return (\n mutationTarget[reactFiberKey]?.child?.memoizedProps?.navigation !==\n undefined\n );\n}\n\nexport function addHTMLMutationObserver() {\n if (isObserverSet || !isWindowAvailable()) {\n return;\n }\n\n isObserverSet = true;\n\n const observer = new MutationObserver((mutationsList) => {\n const rootMutation = mutationsList[mutationsList.length - 1];\n\n if (\n checkIfScreenWasChanged(\n rootMutation.target as ReanimatedHTMLElement & MaybeWithFiberNode\n )\n ) {\n return;\n }\n\n for (let i = 0; i < rootMutation.removedNodes.length; ++i) {\n findDescendantWithExitingAnimation(\n rootMutation.removedNodes[i] as ReanimatedHTMLElement,\n rootMutation.target\n );\n }\n });\n\n observer.observe(document.body, { childList: true, subtree: true });\n}\n\nexport function areDOMRectsEqual(r1: DOMRect, r2: DOMRect) {\n // There are 4 more fields, but checking these should suffice\n return (\n r1.x === r2.x &&\n r1.y === r2.y &&\n r1.width === r2.width &&\n r1.height === r2.height\n );\n}\n"],"mappings":"AAAA,YAAY;;AAGZ,SAASA,iBAAiB,QAAQ,uBAAuB;AACzD,SAASC,gBAAgB,EAAEC,SAAS,QAAQ,kBAAkB;AAC9D,SAASC,UAAU,QAAQ,UAAU;AAGrC,MAAMC,4BAA4B,GAAG,wCAAwC;AAC7E,MAAMC,wBAAwB,GAAG,oCAAoC;;AAErE;AACA,MAAMC,oBAAoB,GAAG,IA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