All files / atoms/Loader/mobile Loader.native.tsx

100% Statements 19/19
93.93% Branches 31/33
100% Functions 3/3
100% Lines 18/18

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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222                              1x                               18x   18x                             18x           18x 18x   18x                     18x             18x                             18x                   18x                             18x   18x 9x               9x 9x     18x         18x                                                                                                                                                                            
import { FC, useEffect, useRef } from 'react';
import {
  AccessibilityProps,
  ActivityIndicator,
  Animated,
  Easing,
  StyleProp,
  Text,
  View,
  ViewStyle,
} from 'react-native';
import { cn } from '@sb/libs/utils';
import Svg, { Circle, Path } from 'react-native-svg';
import { LoaderNativeProps, LoaderSize } from './Loader.native.type';
 
const Loader: FC<LoaderNativeProps> = ({
  id,
  style,
  content,
  value = 0,
  textStyle,
  color = 'white',
  shadow = false,
  size = 'medium',
  animated = true,
  indeterminate = true,
  showPercentage = false,
  loaderPosition = 'top',
  useNativeIndeterminate = true,
  percentagePosition = 'center',
}) => {
  const isDeterminate = value || !indeterminate;
 
  const dimensionMap: Record<LoaderSize, { size: number; strokeWidth: number }> = {
    small: {
      size: 32,
      strokeWidth: 2,
    },
    medium: {
      size: 44,
      strokeWidth: 3,
    },
    large: {
      size: 52,
      strokeWidth: 4,
    },
  };
 
  const fontSizeMap: Record<LoaderSize, string> = {
    small: 'text-xs font-normal',
    medium: 'text-sm font-semibold',
    large: 'text-md font-Bold',
  };
 
  const fontSize = fontSizeMap[size];
  const dimension = dimensionMap[size];
 
  const containerClasses = cn(
    'flex flex-col items-center justify-center p-4',
    {
      'flex-row gap-2': loaderPosition === 'left',
      'flex-row-reverse gap-2': loaderPosition === 'right',
      'flex-col-reverse': loaderPosition === 'bottom',
      'flex-col': loaderPosition === 'top',
    },
    style
  );
 
  const wrapperClasses = cn('relative flex flex-col items-center justify-center', {
    'flex-row-reverse gap-2': percentagePosition === 'left',
    'flex-row gap-2': percentagePosition === 'right',
    'flex-col': percentagePosition === 'bottom',
    'flex-col-reverse': percentagePosition === 'top',
  });
 
  const loaderStyles: StyleProp<ViewStyle> = {
    borderColor: color,
    borderTopColor: 'transparent',
    borderStyle: 'solid',
    transform: animated && indeterminate ? [{ rotate: '360deg' }] : undefined,
    ...(shadow && {
      shadowColor: color,
      shadowOffset: { width: 0, height: 2 },
      shadowOpacity: 0.4,
      shadowRadius: 4,
      elevation: 3,
    }),
    ...(isDeterminate ? { borderWidth: 0 } : {}),
  };
 
  const percentageTextClasses = cn(
    'font-semibold m-2',
    {
      'absolute inset-0 flex items-center justify-center text-center':
        percentagePosition === 'center',
    },
    fontSize,
    textStyle
  );
 
  const accessibilityProps: AccessibilityProps = {
    accessibilityRole: 'progressbar',
    accessibilityLabel: `${id} loading indicator`,
    accessibilityValue: isDeterminate
      ? {
          min: 0,
          max: 100,
          now: value,
          text: `${value}% complete`,
        }
      : {
          text: 'Loading in progress',
        },
  };
 
  const rotation = useRef(new Animated.Value(0)).current;
 
  useEffect(() => {
    const animation = Animated.loop(
      Animated.timing(rotation, {
        toValue: 1,
        duration: 1000,
        easing: Easing.linear,
        useNativeDriver: true,
      })
    );
    animation.start();
    return () => animation.stop();
  }, [rotation]);
 
  const rotateInterpolate = rotation.interpolate({
    inputRange: [0, 1],
    outputRange: ['0deg', '360deg'],
  });
 
  return (
    <View
      className={containerClasses}
      accessibilityLiveRegion="polite"
      accessibilityLabel="Loader status"
    >
      <View className={wrapperClasses} accessibilityRole="progressbar">
        {isDeterminate ? (
          <View style={loaderStyles} {...accessibilityProps} testID="loader-svg">
            <Svg width={dimension.size} height={dimension.size} viewBox="0 0 36 36">
              <Circle
                cx="18"
                cy="18"
                r="16"
                stroke="white"
                strokeWidth={dimension.strokeWidth}
                fill="none"
                opacity={0.3}
              />
              <Circle
                cx="18"
                cy="18"
                r="16"
                stroke="white"
                strokeWidth={dimension.strokeWidth}
                fill="none"
                strokeDasharray={`${(value / 100) * 100}, 100`}
                strokeLinecap="round"
                transform="rotate(-90 18 18)"
              />
            </Svg>
          </View>
        ) : useNativeIndeterminate ? (
          <ActivityIndicator
            color={color}
            size={size === 'small' ? 'small' : 'large'}
            animating={animated}
            {...accessibilityProps}
            accessibilityLabel="Indeterminate loading indicator"
          />
        ) : (
          <Animated.View
            className="items-center justify-center"
            style={{
              transform: [{ rotate: rotateInterpolate }],
            }}
            accessibilityLabel="Indeterminate loading indicator"
          >
            <Svg width={dimension.size} height={dimension.size} viewBox="0 0 28 29" fill="none">
              <Path
                d="M14 27C7.09644 27 1.5 21.4036 1.5 14.5C1.5 7.59644 7.09644 2 14 2C20.9036 2 26.5 7.59644 26.5 14.5"
                stroke={color}
                strokeWidth={dimension.strokeWidth / 1.2}
              />
            </Svg>
          </Animated.View>
        )}
 
        {showPercentage && isDeterminate && (
          <Text
            accessibilityLabel={`Loading Percentage: ${value}`}
            className={percentageTextClasses}
            style={{ color }}
            accessible={false}
          >
            {value}%
          </Text>
        )}
      </View>
 
      {content && (
        <View className="my-2.5">
          <Text
            className={cn('text-center', fontSize, textStyle)}
            accessibilityLabel={`${id} content`}
            style={{ color }}
          >
            {content}
          </Text>
        </View>
      )}
    </View>
  );
};
 
export default Loader;