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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | 1x 24x 24x 24x 24x 24x 24x 24x 18x 13x 13x 11x 24x 16x 2x 14x 24x 24x 24x | import { cn } from '@sb/libs/utils';
import StripedOverlay from './ProgressBar.patterns';
import { FC, useEffect, useRef, useState } from 'react';
import { getProgressBarColor } from './ProgressBar.theme';
import { Animated, Easing, Text, View } from 'react-native';
import { useThemeColors } from '@sb/hooks/Utilities/useThemeColors';
import type { ProgressBarNativeProps, SizeClassProps } from './ProgressBar.native.types';
const ProgressBar: FC<ProgressBarNativeProps> = ({
id,
value: propValue,
defaultValue = 0,
max = 100,
min = 0,
size = 'medium',
color = 'primary',
backgroundColor,
striped = false,
variant = 'solid',
showLoadingLabel = true,
showPercentage = false,
indeterminate = false,
onChange,
direction = 'horizontal',
minColor = 'white',
maxColor = 'white',
labelPosition = 'outside',
minLabel,
maxLabel,
style,
animated = false,
minLabelClassName,
maxLabelClassName,
customProgressColorClass,
}) => {
const { theme, themedColors } = useThemeColors();
const isFirstRender = useRef(true);
const indeterminateAnim = useRef(new Animated.Value(0)).current;
const [value, setValue] = useState<number>(propValue ?? defaultValue);
const percentage = indeterminate ? 0 : Math.round(((value - min) / (max - min)) * 100);
const clampedPercentage = Math.min(Math.max(percentage, 0), 100);
useEffect(() => {
if (propValue !== undefined) {
setValue(propValue);
if (!isFirstRender.current) onChange?.(propValue);
else isFirstRender.current = false;
}
}, [propValue]);
useEffect(() => {
if (indeterminate) {
Animated.loop(
Animated.timing(indeterminateAnim, {
toValue: 1,
duration: 1500,
easing: Easing.linear,
useNativeDriver: true,
})
).start();
} else {
indeterminateAnim.stopAnimation();
}
}, [indeterminate]);
const translateStyle =
direction === 'horizontal'
? {
transform: [
{
translateX: indeterminateAnim.interpolate({
inputRange: [0, 1],
outputRange: [-300, 300],
}),
},
],
}
: {
transform: [
{
translateY: indeterminateAnim.interpolate({
inputRange: [0, 1],
outputRange: [350, -350],
}),
},
],
};
const sizeClasses: SizeClassProps = {
horizontal: {
small: 'h-2',
medium: 'h-3',
large: 'h-4',
},
vertical: {
small: 'w-2',
medium: 'w-3',
large: 'w-4',
},
};
return (
<View
testID={id}
accessibilityRole="progressbar"
accessibilityLabel="Progress bar"
className={cn(
'w-full',
direction === 'horizontal' ? 'flex flex-col' : 'flex h-full flex-row items-center',
style
)}
>
{direction === 'horizontal' && labelPosition === 'outside' && (
<View className="mb-1 flex w-full flex-row justify-between">
{minLabel && (
<Text
accessibilityLabel={`Min Label: ${minLabel}`}
className={cn('mb-2 text-xs font-medium', minLabelClassName)}
style={{ color: minColor }}
>
{minLabel}
</Text>
)}
{maxLabel && (
<Text
accessibilityLabel={`Max Label: ${maxLabel}`}
className={cn('text-xs font-medium', maxLabelClassName)}
style={{ color: maxColor }}
>
{maxLabel}
</Text>
)}
</View>
)}
<View className={cn('relative rounded-full', direction === 'vertical' && 'h-full')}>
<View
style={{
backgroundColor: themedColors.colorSecondary,
}}
accessibilityLabel="Progress Bar Container"
className={cn(
'overflow-hidden rounded-full',
direction === 'horizontal' ? 'w-full' : 'mx-auto h-full',
sizeClasses[direction][size],
backgroundColor
)}
>
{!indeterminate ? (
<View
className={cn(
'overflow-hidden rounded-full',
direction === 'horizontal' ? 'h-full' : 'absolute bottom-0 w-full',
variant === 'dashed' && 'progress-bar-dashed',
variant === 'dotted' && 'progress-bar-dotted',
customProgressColorClass
)}
style={{
width: direction === 'horizontal' ? `${clampedPercentage}%` : '100%',
height: direction === 'vertical' ? `${clampedPercentage}%` : '100%',
backgroundColor: getProgressBarColor(theme, color),
}}
>
{striped && (
<StripedOverlay
width={300}
height={300}
animated={animated}
direction={direction}
/>
)}
{showPercentage && labelPosition === 'inside' && (
<View
className={cn(
'absolute flex items-center justify-center whitespace-nowrap',
direction === 'horizontal' ? 'inset-0 right-2' : 'inset-0 top-2',
{
'w-full': direction === 'vertical',
'h-full': direction === 'horizontal',
}
)}
>
<Text
style={{
color: themedColors.colorText_Main,
}}
accessibilityLabel={`Current Progress: ${clampedPercentage}%`}
className={cn('text-[8px] font-semibold tracking-wide', {
'text-center': direction === 'vertical',
})}
>{`${clampedPercentage}%`}</Text>
</View>
)}
</View>
) : (
<Animated.View
className={cn('absolute rounded-full', {
'h-full w-[100%]': direction === 'horizontal',
'h-[100%] w-full': direction === 'vertical',
})}
style={[{ backgroundColor: getProgressBarColor(theme, color) }, translateStyle]}
/>
)}
</View>
{showPercentage && labelPosition === 'outside' && (
<View
className={cn('absolute h-full w-full flex-row', {
'top-full mt-1 h-[20px] justify-center': direction === 'horizontal',
'left-full ml-2 w-[100px] items-center': direction === 'vertical',
})}
>
<Text
style={{
color: themedColors.colorText_Main,
}}
className="text-xs font-semibold"
accessibilityLabel={`Current Progress: ${clampedPercentage}%`}
>
{indeterminate ? (showLoadingLabel ? 'Loading...' : '') : `${clampedPercentage}%`}
</Text>
</View>
)}
</View>
{direction === 'vertical' && labelPosition === 'outside' && (
<View className="relative ml-2 h-full">
{maxLabel && (
<View
className="absolute -top-[10px] left-0"
accessibilityLabel={`Max Label: ${maxLabel}`}
>
<Text
className={cn('text-xs font-medium', maxLabelClassName)}
style={{ color: maxColor }}
>
{maxLabel}
</Text>
</View>
)}
{minLabel && (
<View
className="absolute bottom-[-5px] left-0"
accessibilityLabel={`Min Label: ${minLabel}`}
>
<Text
className={cn('text-xs font-medium', minLabelClassName)}
style={{ color: minColor }}
>
{minLabel}
</Text>
</View>
)}
</View>
)}
</View>
);
};
export default ProgressBar;
|