StaticBone.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React, { memo } from 'react';
  2. import Animated, {
  3. SharedValue,
  4. useAnimatedStyle,
  5. interpolateColor,
  6. } from 'react-native-reanimated';
  7. import { useGetBoneStyles } from './hooks';
  8. import type {
  9. ICustomViewStyle,
  10. ISkeletonProps,
  11. IComponentSize,
  12. } from './constants';
  13. type StaticBoneProps = Pick<
  14. ISkeletonProps,
  15. 'boneColor' | 'highlightColor' | 'animationType' | 'animationDirection'
  16. > & {
  17. componentSize: IComponentSize;
  18. boneLayout: ICustomViewStyle;
  19. animationValue: SharedValue<number>;
  20. index?: number | string;
  21. };
  22. /**
  23. * StaticBone is a component that renders a rectangle with a static color.
  24. * @boneLayout is the layout of the bone.
  25. * @boneColor is the color of the bone.
  26. * @highlightColor is the color of the highlight.
  27. * @animationDirection is the direction of the animation.
  28. * @animationValue is the value of the animation.
  29. * @animationType is the type of the animation.
  30. * @index is the index of the bone.
  31. * @componentSize is the size of the component.
  32. */
  33. const StaticBone: React.FC<StaticBoneProps> = ({
  34. boneLayout,
  35. index,
  36. componentSize,
  37. highlightColor,
  38. animationValue,
  39. animationDirection,
  40. animationType,
  41. boneColor,
  42. }) => {
  43. const getBoneStyles = useGetBoneStyles(componentSize);
  44. const boneStyle = getBoneStyles({
  45. animationDirection,
  46. animationType,
  47. boneLayout,
  48. boneColor,
  49. });
  50. const animatedStyle = useAnimatedStyle(() => ({
  51. backgroundColor: interpolateColor(
  52. animationValue.value,
  53. [0, 1],
  54. [boneColor as string, highlightColor as string],
  55. ),
  56. }));
  57. return (
  58. <Animated.View
  59. key={index}
  60. style={[boneStyle, animationType === 'none' ? {} : animatedStyle]}
  61. />
  62. );
  63. };
  64. export default memo(StaticBone);