All files / apps/host/src/components/LoadingSlider index.tsx

100% Statements 20/20
100% Branches 2/2
100% Functions 3/3
100% Lines 20/20

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 143 144 145 146 147 148 149 150 151          5x   5x 5x             5x       3x 3x 3x   3x 3x             3x                                 3x                             3x 3x   3x 3x 3x       3x           3x         3x                                                             5x                                                                        
import React, { useEffect, useRef } from 'react';
import { Animated, Dimensions, Easing, StyleSheet, View } from 'react-native';
 
import { ASSETS } from '@/constants';
 
const { width: screenWidth } = Dimensions.get('window');
 
const TRACK_WIDTH = screenWidth * 0.7;
const DEFAULT_SKATE_SIZE = 120;
 
interface LoadingSliderProps {
  width?: number;
  height?: number;
}
 
export const LoadingSlider = ({
  width = DEFAULT_SKATE_SIZE,
  height = DEFAULT_SKATE_SIZE,
}: LoadingSliderProps) => {
  const translateX = useRef(new Animated.Value(-width)).current;
  const bounce = useRef(new Animated.Value(0)).current;
  const tilt = useRef(new Animated.Value(0)).current;
 
  useEffect(() => {
    const move = Animated.timing(translateX, {
      toValue: TRACK_WIDTH - width / 2,
      duration: 3500,
      easing: Easing.linear,
      useNativeDriver: true,
    });
 
    const bounceAnim = Animated.loop(
      Animated.sequence([
        Animated.timing(bounce, {
          toValue: -4,
          duration: 270,
          easing: Easing.inOut(Easing.ease),
          useNativeDriver: true,
        }),
        Animated.timing(bounce, {
          toValue: 0,
          duration: 270,
          easing: Easing.inOut(Easing.ease),
          useNativeDriver: true,
        }),
      ]),
    );
 
    const tiltAnim = Animated.loop(
      Animated.sequence([
        Animated.timing(tilt, {
          toValue: 1,
          duration: 300,
          useNativeDriver: true,
        }),
        Animated.timing(tilt, {
          toValue: -1,
          duration: 300,
          useNativeDriver: true,
        }),
      ]),
    );
 
    bounceAnim.start();
    tiltAnim.start();
 
    move.start(() => {
      bounceAnim.stop();
      tiltAnim.stop();
    });
  }, [bounce, tilt, translateX, width]);
 
  const progressScale = translateX.interpolate({
    inputRange: [-width, TRACK_WIDTH - width / 2],
    outputRange: [0, 1],
    extrapolate: 'clamp',
  });
 
  const rotateDeg = tilt.interpolate({
    inputRange: [-1, 1],
    outputRange: ['-2deg', '2deg'],
  });
 
  return (
    <View style={styles.container}>
      <View style={styles.wrapper}>
        <View style={styles.track}>
          <Animated.View
            style={[
              styles.progress,
              {
                transform: [{ scaleX: progressScale }],
              },
            ]}
          />
        </View>
 
        <Animated.Image
          source={ASSETS.IMAGES.HOME.PATIN}
          style={[
            styles.mascot,
            {
              width,
              height,
              transform: [{ translateX }, { translateY: bounce }, { rotate: rotateDeg }],
            },
          ]}
          resizeMode="contain"
        />
      </View>
    </View>
  );
};
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
 
  wrapper: {
    width: TRACK_WIDTH,
    height: 140,
  },
 
  track: {
    position: 'absolute',
    bottom: 22,
    width: '100%',
    height: 8,
    backgroundColor: '#e0e0e0',
    borderRadius: 4,
    overflow: 'hidden',
  },
 
  progress: {
    height: '100%',
    width: '100%',
    backgroundColor: '#bdbdbd',
    transform: [{ scaleX: 0 }],
    transformOrigin: 'left',
  },
 
  mascot: {
    position: 'absolute',
    bottom: 30,
    left: 0,
  },
});