import { useEffect, useState } from 'react';
import {
Dimensions,
SafeAreaView,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
import {
BUILTIN_INDICATOR_NAMES,
LoaderKitProgress,
LoaderKitView,
defineIndicator,
param,
type BuiltinIndicatorName,
} from 'react-native-loader-kit';
const { width: screenWidth } = Dimensions.get('window');
const Blink = defineIndicator({
name: 'Blink',
duration: 0.9,
params: { count: 4, low: 0.15 },
layout: { type: 'row', count: param('count'), gap: 0.08 },
shape: { type: 'rect', cornerRadius: 0.25 },
stagger: { each: 0.15 },
tracks: [
{
property: 'opacity',
keyTimes: [0, 0.5, 1],
values: [1, param('low'), 1],
easing: 'easeInOut',
},
{
property: 'scaleY',
keyTimes: [0, 0.5, 1],
values: [1, 0.5, 1],
easing: 'easeInOut',
},
],
});
const displayName = (name: string) => name.replace(/([A-Z])/g, ' $1').trim();
/** Progress of a fake download: uneven steps, then a pause before it starts again. */
function useDownload(running: boolean) {
const [value, setValue] = useState(0);
useEffect(() => {
if (!running) return;
const timer = setInterval(() => {
setValue((v) => (v >= 1 ? 0 : Math.min(1, v + Math.random() * 0.12)));
}, 400);
return () => clearInterval(timer);
}, [running]);
return value;
}
function Button({ label, onPress }: { label: string; onPress: () => void }) {
return (
{label}
);
}
export default function App() {
const [speed, setSpeed] = useState(1);
const [animating, setAnimating] = useState(true);
const [cycleProgress, setCycleProgress] = useState(
undefined
);
const [multicolor, setMulticolor] = useState(false);
const [count, setCount] = useState(4);
const [indeterminate, setIndeterminate] = useState(false);
const download = useDownload(!indeterminate);
const progressValue = indeterminate ? null : download;
const columns = 3;
const spacing = 10;
const itemWidth = (screenWidth - spacing * (columns + 1)) / columns;
const common = {
speed,
animating,
cycleProgress,
color: 'white',
colors: multicolor ? ['#ffce54', '#a0d468', '#4fc1e9'] : undefined,
style: styles.indicator,
};
const renderBuiltin = (name: BuiltinIndicatorName, index: number) => (
{index + 1}. {displayName(name)}
);
return (
Speed {speed.toFixed(1)}x
Cycle progress{' '}
{cycleProgress === undefined ? 'off' : cycleProgress.toFixed(2)}
setCycleProgress(undefined)} />
{[0, 0.25, 0.5, 0.75].map((value) => (
setCycleProgress(value)}
/>
))}
{BUILTIN_INDICATOR_NAMES.map(renderBuiltin)}
Progress {indeterminate ? 'indeterminate' : download.toFixed(2)}
setIndeterminate((i) => !i)}
/>
setIndeterminate(true)}
/>
setIndeterminate(false)} />
Custom spec: Blink, count {count}
setCount((c) => Math.max(1, c - 1))}
/>
setCount((c) => Math.min(8, c + 1))}
/>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ed5565',
},
controls: {
backgroundColor: 'rgba(204, 43, 63, 0.9)',
marginHorizontal: 16,
marginTop: 10,
padding: 12,
borderRadius: 8,
alignItems: 'center',
},
controlLabel: {
color: 'white',
fontSize: 14,
fontWeight: 'bold',
marginVertical: 6,
},
row: {
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
},
button: {
backgroundColor: 'rgba(255, 255, 255, 0.2)',
paddingHorizontal: 12,
height: 36,
borderRadius: 18,
alignItems: 'center',
justifyContent: 'center',
margin: 4,
},
buttonText: {
color: 'white',
fontSize: 14,
fontWeight: 'bold',
},
scrollContent: {
padding: 10,
},
grid: {
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-between',
},
cell: {
alignItems: 'center',
marginBottom: 16,
},
indicator: {
width: 50,
height: 50,
marginVertical: 8,
},
label: {
color: 'white',
fontSize: 11,
textAlign: 'center',
},
linear: {
alignSelf: 'stretch',
marginVertical: 8,
},
stop: {
width: 14,
height: 14,
borderRadius: 3,
backgroundColor: 'white',
},
custom: {
alignItems: 'center',
backgroundColor: 'rgba(255, 255, 255, 0.1)',
borderRadius: 8,
padding: 12,
marginTop: 8,
},
});