All files / apps/chatbot/src/components/Indicators ShimmerText.tsx

100% Statements 18/18
100% Branches 0/0
100% Functions 6/6
100% Lines 16/16

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                            2x 2x 2x   2x                 22x   22x               22x       2x 2x 2x 2x   2x 2x             2x     22x           24x                          
import React, { useEffect } from "react";
import Animated, {
  Easing,
  interpolateColor,
  SharedValue,
  useAnimatedStyle,
  useSharedValue,
  withRepeat,
  withTiming,
} from "react-native-reanimated";
import { View } from "react-native";
 
import { makeStyles } from "@repo/ui/themes/makeStyles";
 
const SHIMMER_LABEL = "Thinking...";
const SHIMMER_BASE_COLOR = "#5E6C84";
const SHIMMER_ACTIVE_COLOR = "#D0D5DD";
 
const ShimmerChar = ({
  char,
  index,
  progress,
}: {
  char: string;
  index: number;
  progress: SharedValue<number>;
}) => {
  const styles = useStyles();
 
  const animatedTextStyle = useAnimatedStyle(() => ({
    color: interpolateColor(
      progress.value,
      [index - 1.5, index, index + 1.5],
      [SHIMMER_BASE_COLOR, SHIMMER_ACTIVE_COLOR, SHIMMER_BASE_COLOR],
    ),
  }));
 
  return <Animated.Text style={[styles.shimmerCharText, animatedTextStyle]}>{char}</Animated.Text>;
};
 
/** "Thinking..." with a highlight sweeping one character at a time. */
export const ShimmerText = () => {
  const styles = useStyles();
  const chars = SHIMMER_LABEL.split("");
  const progress = useSharedValue(-2);
 
  useEffect(() => {
    progress.value = withRepeat(
      withTiming(chars.length + 2, { duration: 1800, easing: Easing.linear }),
      -1,
      false,
    );
  }, [progress, chars.length]);
 
  return (
    <View style={styles.shimmerTextContainer}>
      {chars.map((char, index) => (
        <ShimmerChar key={index} char={char} index={index} progress={progress} />
      ))}
    </View>
  );
};
 
const useStyles = makeStyles(() => ({
  shimmerTextContainer: {
    flexDirection: "row",
    alignItems: "center",
    paddingVertical: 6,
    paddingHorizontal: 4,
  },
  shimmerCharText: {
    fontSize: 14,
    fontWeight: "400",
    letterSpacing: 0.5,
  },
}));