Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/ModalView.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export const ModalView: FC<ModalViewWebProps> = ({
const { isTopmost } = useModalStack(currentModalId);

const contentRef = useFocusTrap(isTopmost);
const { setContainerRef, animatedStyle } = useModalAnimation(animationType);
const { setContainerRef, animatedStyle } = useModalAnimation(
animationType,
contentRef,
);

useEffect(() => {
if (!isTopmost || !onRequestDismiss) {
Expand Down
14 changes: 8 additions & 6 deletions src/helpers/animationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
);
Expand Down
14 changes: 10 additions & 4 deletions src/hooks/useModalAnimation.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<View | null>,
) {
const [isVisible, setVisibility] = useState(animationType === 'none');

const containerRef = useRef<HTMLElement | null>(null);
Expand All @@ -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) {
Expand Down