All files / apps/host/src/components/AnimatedPendingRequestList/animations useStackAnimation.ts

97.5% Statements 39/40
93.33% Branches 14/15
100% Functions 4/4
97.5% Lines 39/40

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                                              3x                 10x   10x 10x   10x                   10x 10x 10x 10x 10x   10x         10x   10x   10x 10x 10x   10x   2x 2x 2x 2x       4x 4x 4x 4x       1x 1x         1x 1x       2x 2x         2x 2x     1x 1x 1x 1x     10x                         10x    
import {
  interpolate,
  SharedValue,
  useAnimatedReaction,
  useAnimatedStyle,
  useSharedValue,
  withSequence,
  withSpring,
  withTiming,
} from 'react-native-reanimated';
 
import { BOOK_PAGE_PEEK_Y, BOOK_PAGE_SCALE_STEP, MAX_ROTATION, PULSE_SPRING } from '../constants';
 
interface UseStackAnimationProps {
  index: number;
  currentIndex: number;
  animatedValue: SharedValue<number>;
  translateX: SharedValue<number>;
  translateY: SharedValue<number>;
  itemHeight: SharedValue<number>;
  width: number;
}
 
export const useStackAnimation = ({
  index,
  currentIndex,
  animatedValue,
  translateX,
  translateY,
  itemHeight,
  width,
}: UseStackAnimationProps) => {
  const pulseScale = useSharedValue(1);
 
  useAnimatedReaction(
    () => currentIndex === index,
    (isTop, wasTop) => {
      Iif (isTop && wasTop === false) {
        pulseScale.value = withSequence(
          withTiming(0.96, { duration: 0 }),
          withSpring(1, PULSE_SPRING),
        );
      }
    },
    [currentIndex, index],
  );
 
  const animatedStyle = useAnimatedStyle(() => {
    const currentItem = currentIndex === index;
    const relativeIndex = index - currentIndex;
    const progress = animatedValue.value - currentIndex;
    const clampedProgress = Math.max(0, Math.min(1, progress));
    // No clamp — card tilts further as it flies off screen (natural throw feel)
    const rotateZ = interpolate(
      translateX.value,
      [-width * 0.5, 0, width * 0.5],
      [MAX_ROTATION * 2.5, 0, -MAX_ROTATION * 2.5],
    );
    const pivot = -itemHeight.value / 2;
 
    const peekY = BOOK_PAGE_PEEK_Y;
 
    let stackTranslateY = 0;
    let stackScale = 1;
    let opacity = 1;
 
    switch (relativeIndex) {
      case 0:
        stackTranslateY = 0;
        stackScale = 1;
        opacity = 1;
        break;
 
      case 1:
        // Layer 2: rests peekY below top, slides forward as top card is swiped
        stackTranslateY = interpolate(clampedProgress, [0, 1], [peekY, 0]);
        stackScale = interpolate(clampedProgress, [0, 1], [1 - BOOK_PAGE_SCALE_STEP, 1]);
        opacity = 1;
        break;
 
      case 2:
        // Layer 3: rests 2×peekY below top, slides to layer 2 position during drag
        stackTranslateY = interpolate(clampedProgress, [0, 1], [peekY * 2, peekY]);
        stackScale = interpolate(
          clampedProgress,
          [0, 1],
          [1 - BOOK_PAGE_SCALE_STEP * 2, 1 - BOOK_PAGE_SCALE_STEP],
        );
        opacity = 1;
        break;
 
      case 3:
        // Layer 4: rests 3×peekY below top, slides to layer 3 position during drag
        stackTranslateY = interpolate(clampedProgress, [0, 1], [peekY * 3, peekY * 2]);
        stackScale = interpolate(
          clampedProgress,
          [0, 1],
          [1 - BOOK_PAGE_SCALE_STEP * 3, 1 - BOOK_PAGE_SCALE_STEP * 2],
        );
        opacity = 1;
        break;
 
      default:
        stackTranslateY = 1000;
        stackScale = 0;
        opacity = 0;
        break;
    }
 
    return {
      opacity,
      transform: [
        { translateX: translateX.value },
        { translateY: translateY.value + stackTranslateY },
        { scale: stackScale * pulseScale.value },
        { translateY: currentItem ? pivot : 0 },
        { rotateZ: currentItem ? `${rotateZ}deg` : '0deg' },
        { translateY: currentItem ? -pivot : 0 },
      ],
    };
  });
 
  return animatedStyle;
};