import { useInsertionEffect } from 'react' import { useConstant } from '@tamagui/use-constant' import { useForceUpdate } from '@tamagui/use-force-update' import type { FunctionComponent, PropsWithChildren, ReactElement, ReactNode } from 'react' import { Children, isValidElement, useContext, useMemo, useRef, useState } from 'react' import { LayoutGroupContext } from './LayoutGroupContext' import { PresenceChild } from './PresenceChild' import type { AnimatePresenceProps } from './types' type ComponentKey = string | number const getChildKey = (child: ReactElement): ComponentKey => { return child.key ?? '' } function onlyElements(children: ReactNode): ReactElement[] { const filtered: ReactElement[] = [] // We use forEach here instead of map as map mutates the component key by preprending `.$` Children.forEach(children, (child) => { if (isValidElement(child)) filtered.push(child) }) return filtered } export const AnimatePresence: FunctionComponent< PropsWithChildren > = ({ children, initial = true, onExitComplete, mode = 'sync', presenceAffectsLayout = true, custom, passThrough, }) => { /** * Filter any children that aren't ReactElements. We can only track components * between renders with a props.key. * IMPORTANT: useMemo ensures reference stability for the comparison below */ const presentChildren = useMemo(() => onlyElements(children), [children]) /** * Track the keys of the currently rendered children. */ const presentKeys = presentChildren.map(getChildKey) /** * If `initial={false}` we only want to pass this to components in the first render. */ const isInitialRender = useRef(true) /** * Freeze custom prop for exiting children so direction doesn't reverse mid-exit. */ const frozenCustomRef = useRef(new Map()) /** * A ref containing the currently present children. When all exit animations * are complete, we use this to re-render with the latest children *committed* * rather than the latest children *rendered*. */ const pendingPresentChildren = useRef(presentChildren) /** * Track which exiting children have finished animating out. */ const exitComplete = useConstant(() => new Map()) /** * Save children to render as React state. To ensure this component is concurrent-safe, * we check for exiting children via an effect. */ const [diffedChildren, setDiffedChildren] = useState(presentChildren) const [renderedChildren, setRenderedChildren] = useState(presentChildren) /** * If we've been provided a forceRender function by the LayoutGroupContext, * we can use it to force a re-render amongst all surrounding components once * all components have finished animating out. */ const forceRender = useContext(LayoutGroupContext).forceRender ?? useForceUpdate() if (passThrough) { return <>{children} } // useInsertionEffect runs before ALL useLayoutEffects (including children's) // this ensures pendingPresentChildren and exitComplete are set before // animation drivers call sendExitComplete() in their layout effects // (critical for immediate completions like `transition="none"`) useInsertionEffect(() => { isInitialRender.current = false pendingPresentChildren.current = presentChildren /** * Update complete status of exiting children. */ for (let i = 0; i < renderedChildren.length; i++) { const key = getChildKey(renderedChildren[i]) if (!presentKeys.includes(key)) { if (exitComplete.get(key) !== true) { exitComplete.set(key, false) } } else { exitComplete.delete(key) frozenCustomRef.current.delete(key) } } }, [renderedChildren, presentKeys.length, presentKeys.join('-')]) if (presentChildren !== diffedChildren) { let nextChildren = [...presentChildren] /** * Loop through all the currently rendered components and decide which * are exiting. */ for (let i = 0; i < renderedChildren.length; i++) { const child = renderedChildren[i] const key = getChildKey(child) if (!presentKeys.includes(key)) { nextChildren.splice(i, 0, child) // freeze custom at the moment of exit so direction doesn't reverse if (!frozenCustomRef.current.has(key)) { frozenCustomRef.current.set(key, custom) } } } /** * If we're in "wait" mode, and we have exiting children, we want to * only render these until they've all exited. */ const exitingChildren = renderedChildren.filter( (child) => !presentKeys.includes(getChildKey(child)) ) if (mode === 'wait' && exitingChildren.length) { nextChildren = exitingChildren } setRenderedChildren(onlyElements(nextChildren)) setDiffedChildren(presentChildren) /** * Early return to ensure once we've set state with the latest diffed * children, we can immediately re-render. */ return null } return ( <> {renderedChildren.map((child) => { const key = getChildKey(child) const isPresent = presentChildren === renderedChildren || presentKeys.includes(key) const onExit = () => { if (exitComplete.has(key)) { exitComplete.set(key, true) } else { return } let isEveryExitComplete = true exitComplete.forEach((isExitComplete) => { if (!isExitComplete) isEveryExitComplete = false }) if (isEveryExitComplete) { forceRender?.() setRenderedChildren(pendingPresentChildren.current) onExitComplete?.() } } return ( {child} ) })} ) } AnimatePresence.displayName = 'AnimatePresence'