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 | 3x 135x 135x 135x 135x 135x 239x 239x 239x 135x 135x 135x 135x 133x 132x 132x 132x 135x 132x 3x 100x 100x 100x 3x 135x 270x 135x 4x 135x 135x 43x 43x 43x 3x 1x 1x 2x 40x 9x 31x 135x 140x 1x 139x 139x 139x 135x 140x 3x | import React, { useEffect, useState, useRef } from 'react';
import { View, Text, Animated, StyleSheet, ViewStyle, TextStyle } from 'react-native';
import { SkeletonProps } from '../Skeleton.types';
import { cn } from '@sb/libs/utils';
import { useTheme } from '@sb/ui/components/Themes/ThemeProvider';
import { colors } from '@sb/styles/colors';
export const Skeleton: React.FC<SkeletonProps> = ({
id,
style,
width = 100,
height = 50,
duration = 1,
shape = 'rectangular',
count = 1,
animated = true,
color,
backgroundColor,
isVisible = true,
showPercentage = false,
containerStyle,
className = '',
}) => {
const [percentage, setPercentage] = useState(0);
const [layoutSize, setLayoutSize] = useState<{
width: number;
height: number;
}>({
width: 0,
height: 0,
});
const pulseAnim = useRef(new Animated.Value(1)).current;
const token = className.split(/\s+/);
const hasBgClass = token.some((t) => t.startsWith('bg-'));
const hasHeightClass = token.some((t) => t.startsWith('h-'));
const hasWidthClass = token.some((t) => t.startsWith('w-'));
const hasShapeClass = token.some((t) => t === 'rounded' || t.startsWith('rounded-'));
const { theme } = useTheme();
const defaultBackgroundColor = backgroundColor || colors[theme].colorSkeletonFallback;
const defaultTextColor = color || colors[theme].bgBlack;
useEffect(() => {
if (!animated) return;
const loop = Animated.loop(
Animated.sequence([
Animated.timing(pulseAnim, {
toValue: 0.5,
duration: (duration * 1000) / 2,
useNativeDriver: true,
}),
Animated.timing(pulseAnim, {
toValue: 1,
duration: (duration * 1000) / 2,
useNativeDriver: true,
}),
])
);
loop.start();
return () => loop.stop();
}, [animated, duration, pulseAnim]);
useEffect(() => {
if (!showPercentage) return;
const interval = setInterval(
() => {
setPercentage((prev) => {
Iif (prev >= 100) {
clearInterval(interval);
return 100;
}
return prev + 1;
});
},
(duration * 1000) / 100
);
return () => clearInterval(interval);
}, [showPercentage, duration]);
const normalizeSize = (value: number | string): number | string =>
typeof value === 'string' && value.includes('%') ? value : Number(value);
const isNumeric = (value: number | string): value is number =>
typeof value === 'number' || !isNaN(Number(value));
const baseSize = {
width: normalizeSize(width),
height: normalizeSize(height),
};
const ShapeStyle = (): object => {
const w = baseSize.width;
const h = baseSize.height;
if (shape === 'circle') {
if (isNumeric(w) && isNumeric(h)) {
const minSize =
layoutSize.width && layoutSize.height
? Math.min(layoutSize.width, layoutSize.height)
: Math.min(Number(w), Number(h));
return {
width: minSize,
height: minSize,
borderRadius: minSize / 2,
};
}
return {
width: width,
height: height,
};
}
if (shape === 'rounded') {
return { ...baseSize, borderRadius: 8 };
}
return { ...baseSize, borderRadius: 0 };
};
const RenderSkeleton = (count: number) => {
if (!isVisible) {
return null;
}
const flatStyle = StyleSheet.flatten(style);
const styles: object = {
...(hasBgClass
? {}
: { backgroundColor: flatStyle?.backgroundColor || defaultBackgroundColor }),
...(hasHeightClass ? {} : { height: flatStyle?.height || height }),
...(hasWidthClass ? {} : { width: flatStyle?.width || width }),
...(hasShapeClass ? {} : { ...ShapeStyle() }),
};
return (
<Animated.View
testID={`${id}-skeleton-${count}`}
id={`${id}-skeleton-${count}`}
accessibilityLabel={'skeleton-item'}
key={count}
className={cn(className)}
style={[
styleSkeleton.skeleton,
{ opacity: animated ? pulseAnim : 1 },
style as ViewStyle,
styles,
]}
onLayout={(event) => {
const { width, height } = event.nativeEvent.layout;
setLayoutSize({ width, height });
}}
>
{showPercentage && (
<Text style={[styleSkeleton.percentageText, { color: defaultTextColor }]}>
{percentage}%
</Text>
)}
</Animated.View>
);
};
return (
<View
style={[styleSkeleton.skeletonContainer, containerStyle as ViewStyle]}
accessibilityLabel="loading"
testID={id}
id={id}
>
{Array.from({ length: count }, (_, index) => RenderSkeleton(index))}
</View>
);
};
const styleSkeleton = StyleSheet.create({
skeletonContainer: {
flexDirection: 'column',
gap: 10,
} as ViewStyle,
skeleton: {
overflow: 'hidden',
position: 'relative',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
},
percentageText: {
textAlign: 'center',
fontWeight: 'bold',
alignSelf: 'center',
} as TextStyle,
});
|