All files / react-native-web/src/exports/Modal ModalAnimation.js

96.77% Statements 30/31
82.35% Branches 28/34
100% Functions 4/4
96.77% Lines 30/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                            1x     42x 5x   37x     37x                       87x   87x 87x   87x   87x   35x     1x     34x 31x 31x     3x 3x 3x             87x 46x 32x   46x   32x   46x     87x                 1x                                                                                                     1x 1x 1x 1x      
/**
 * Copyright (c) Nicolas Gallagher.
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
 */
 
import * as React from 'react';
import StyleSheet from '../StyleSheet';
import createElement from '../createElement';
 
const ANIMATION_DURATION = 300;
 
function getAnimationStyle(animationType, visible) {
  if (animationType === 'slide') {
    return visible ? animatedSlideInStyles : animatedSlideOutStyles;
  }
  Iif (animationType === 'fade') {
    return visible ? animatedFadeInStyles : animatedFadeOutStyles;
  }
  return visible ? styles.container : styles.hidden;
}
 
export type ModalAnimationProps = {|
  animationType?: ?('none' | 'slide' | 'fade'),
  children?: any,
  onDismiss?: ?() => void,
  onShow?: ?() => void,
  visible?: ?boolean
|};
 
function ModalAnimation(props: ModalAnimationProps): React.Node {
  const { animationType, children, onDismiss, onShow, visible } = props;
 
  const [isRendering, setIsRendering] = React.useState(false);
  const wasVisible = React.useRef(false);
 
  const isAnimated = animationType && animationType !== 'none';
 
  const animationEndCallback = React.useCallback(
    (e: any) => {
      if (e && e.currentTarget !== e.target) {
        // If the event was generated for something NOT this element we
        // should ignore it as it's not relevant to us
        return;
      }
 
      if (visible) {
        Eif (onShow) {
          onShow();
        }
      } else {
        setIsRendering(false);
        Eif (onDismiss) {
          onDismiss();
        }
      }
    },
    [onDismiss, onShow, visible]
  );
 
  React.useEffect(() => {
    if (visible) {
      setIsRendering(true);
    }
    if (visible !== wasVisible.current && !isAnimated) {
      // Manually call `animationEndCallback` if no animation is used
      animationEndCallback();
    }
    wasVisible.current = visible;
  }, [isAnimated, visible, animationEndCallback]);
 
  return isRendering || visible
    ? createElement('div', {
        style: isRendering ? getAnimationStyle(animationType, visible) : styles.hidden,
        onAnimationEnd: animationEndCallback,
        children
      })
    : null;
}
 
const styles = StyleSheet.create({
  container: {
    position: 'fixed',
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
    zIndex: 9999
  },
  animatedIn: {
    animationDuration: `${ANIMATION_DURATION}ms`,
    animationTimingFunction: 'ease-in'
  },
  animatedOut: {
    pointerEvents: 'none',
    animationDuration: `${ANIMATION_DURATION}ms`,
    animationTimingFunction: 'ease-out'
  },
  fadeIn: {
    opacity: 1,
    animationKeyframes: {
      '0%': { opacity: 0 },
      '100%': { opacity: 1 }
    }
  },
  fadeOut: {
    opacity: 0,
    animationKeyframes: {
      '0%': { opacity: 1 },
      '100%': { opacity: 0 }
    }
  },
  slideIn: {
    transform: [{ translateY: '0%' }],
    animationKeyframes: {
      '0%': { transform: [{ translateY: '100%' }] },
      '100%': { transform: [{ translateY: '0%' }] }
    }
  },
  slideOut: {
    transform: [{ translateY: '100%' }],
    animationKeyframes: {
      '0%': { transform: [{ translateY: '0%' }] },
      '100%': { transform: [{ translateY: '100%' }] }
    }
  },
  hidden: {
    opacity: 0
  }
});
 
const animatedSlideInStyles = [styles.container, styles.animatedIn, styles.slideIn];
const animatedSlideOutStyles = [styles.container, styles.animatedOut, styles.slideOut];
const animatedFadeInStyles = [styles.container, styles.animatedIn, styles.fadeIn];
const animatedFadeOutStyles = [styles.container, styles.animatedOut, styles.fadeOut];
 
export default ModalAnimation;