diff --git a/src/ModalView.web.tsx b/src/ModalView.web.tsx index ea72833..9553acf 100644 --- a/src/ModalView.web.tsx +++ b/src/ModalView.web.tsx @@ -44,7 +44,10 @@ export const ModalView: FC = ({ const { isTopmost } = useModalStack(currentModalId); const contentRef = useFocusTrap(isTopmost); - const { setContainerRef, animatedStyle } = useModalAnimation(animationType); + const { setContainerRef, animatedStyle } = useModalAnimation( + animationType, + contentRef, + ); useEffect(() => { if (!isTopmost || !onRequestDismiss) { diff --git a/src/helpers/animationHelpers.ts b/src/helpers/animationHelpers.ts index 75710a8..1c5a20e 100644 --- a/src/helpers/animationHelpers.ts +++ b/src/helpers/animationHelpers.ts @@ -23,14 +23,15 @@ export function runAfterGuaranteedRender(callback: () => void) { } export function playExitAnimation( - node: HTMLElement | null, + container: HTMLElement | null, + content: HTMLElement | null, animationType: AnimationType, ) { - if (!node || animationType === 'none') { + if (!container || animationType === 'none') { return; } - const clone = maybeGetElement(node.cloneNode(true)); + const clone = maybeGetElement(container.cloneNode(true)); if (!clone) { return; @@ -44,10 +45,11 @@ export function playExitAnimation( EXIT_ANIMATION_OPTIONS, ); - if (animationType === 'slide') { - const content = maybeGetElement(clone.lastElementChild); + if (animationType === 'slide' && content) { + const contentIndex = Array.from(container.children).indexOf(content); + const contentClone = clone.children.item(contentIndex); - content?.animate( + contentClone?.animate( { transform: ['translateY(0)', 'translateY(100%)'] }, EXIT_ANIMATION_OPTIONS, ); diff --git a/src/hooks/useModalAnimation.ts b/src/hooks/useModalAnimation.ts index d542afa..877911d 100644 --- a/src/hooks/useModalAnimation.ts +++ b/src/hooks/useModalAnimation.ts @@ -1,4 +1,6 @@ +import type { RefObject } from 'react'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import type { View } from 'react-native'; import { playExitAnimation, @@ -7,7 +9,10 @@ import { import { maybeGetElement } from '../helpers/focusHelpers'; import type { AnimationType } from '../types'; -export function useModalAnimation(animationType: AnimationType) { +export function useModalAnimation( + animationType: AnimationType, + contentRef: RefObject, +) { const [isVisible, setVisibility] = useState(animationType === 'none'); const containerRef = useRef(null); @@ -23,13 +28,14 @@ export function useModalAnimation(animationType: AnimationType) { setVisibility(true), ); - const modalContent = containerRef.current; + const container = containerRef.current; + const content = maybeGetElement(contentRef.current); return () => { cancelEnteringAnimation(); - playExitAnimation(modalContent, latestAnimationType.current); + playExitAnimation(container, content, latestAnimationType.current); }; - }, []); + }, [contentRef]); const animatedStyle = useMemo(() => { switch (animationType) {