All files / src/components CampusSwitcher.tsx

100% Statements 28/28
93.75% Branches 15/16
100% Functions 8/8
100% Lines 27/27

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                                          8x 8x 8x   8x 8x 8x 8x 7x     8x 8x   3x 3x             3x 3x 1x 1x       3x 3x 3x 2x         8x 3x     8x   8x                                 1x                               1x                                     1x                                                                            
import React, { useEffect, useMemo, useRef, useState } from "react";
import {
  Animated,
  LayoutChangeEvent,
  Pressable,
  StyleSheet,
  Text,
  View,
} from "react-native";
import { useTheme } from "../hooks/useTheme";
import { useAccessibleTypography } from "../hooks/useAccessibilitySettings";
 
type CampusSwitcherProps = Readonly<{
  value: "SGW" | "LOYOLA";
  onChange: (v: "SGW" | "LOYOLA") => void;
}>;
 
export default function CampusSwitcher({
  value,
  onChange,
}: CampusSwitcherProps) {
  const { colors } = useTheme();
  const { textStyle } = useAccessibleTypography();
  const [containerWidth, setContainerWidth] = useState(0);
 
  const translateX = useRef(new Animated.Value(0)).current;
  const animationRef = useRef<Animated.CompositeAnimation | null>(null);
  const selectedIndex = value === "SGW" ? 0 : 1;
  const segmentWidth = useMemo(() => {
    return containerWidth > 0 ? containerWidth / 2 : 0;
  }, [containerWidth]);
 
  useEffect(() => {
    if (!segmentWidth) return;
 
    animationRef.current?.stop();
    const animation = Animated.spring(translateX, {
      toValue: selectedIndex * segmentWidth,
      useNativeDriver: false,
      damping: 18,
      stiffness: 180,
      mass: 0.7,
    });
    animationRef.current = animation;
    animation.start(() => {
      Eif (animationRef.current === animation) {
        animationRef.current = null;
      }
    });
 
    return () => {
      animation.stop();
      if (animationRef.current === animation) {
        animationRef.current = null;
      }
    };
  }, [selectedIndex, segmentWidth, translateX]);
 
  const onLayout = (e: LayoutChangeEvent) => {
    setContainerWidth(e.nativeEvent.layout.width);
  };
 
  const segmentLabelStyle = textStyle(13);
 
  return (
    <View
      style={[styles.wrapper, { backgroundColor: colors.surface }]}
      onLayout={onLayout}
    >
      <Animated.View
        pointerEvents="none"
        style={[
          styles.selector,
          {
            width: segmentWidth ? segmentWidth - 8 : 0,
            transform: [{ translateX }],
            borderColor: colors.primary,
          },
        ]}
      />
 
      <Pressable style={styles.item} onPress={() => onChange("SGW")}>
        <Text
          style={[
            styles.text,
            segmentLabelStyle,
            { color: colors.textMuted },
            value === "SGW" && [
              styles.textActive,
              { color: colors.primary, opacity: 1 },
            ],
          ]}
        >
          SGW Campus
        </Text>
      </Pressable>
 
      <Pressable style={styles.item} onPress={() => onChange("LOYOLA")}>
        <Text
          style={[
            styles.text,
            segmentLabelStyle,
            { color: colors.textMuted },
            value === "LOYOLA" && [
              styles.textActive,
              { color: colors.primary, opacity: 1 },
            ],
          ]}
        >
          Loyola Campus
        </Text>
      </Pressable>
    </View>
  );
}
 
const styles = StyleSheet.create({
  wrapper: {
    width: 300,
    height: 42,
    flexDirection: "row",
    alignItems: "center",
    borderRadius: 999,
    padding: 4,
    position: "relative",
    shadowColor: "#000",
    shadowOpacity: 0.12,
    shadowRadius: 10,
    shadowOffset: { width: 0, height: 4 },
    elevation: 5,
  },
  selector: {
    position: "absolute",
    left: 4,
    top: 4,
    bottom: 4,
    borderRadius: 999,
    borderWidth: 2,
    backgroundColor: "transparent",
  },
  item: {
    flex: 1,
    height: "100%",
    borderRadius: 999,
    alignItems: "center",
    justifyContent: "center",
  },
  text: {
    opacity: 0.7,
  },
  textActive: {
    opacity: 1,
  },
});