Skip to content

Commit c37f0be

Browse files
committed
Implement custom component render
1 parent 6d0364f commit c37f0be

File tree

3 files changed

+62
-40
lines changed

3 files changed

+62
-40
lines changed

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ function LoadingScreen() {
2828
<LoadingDots />
2929
</View>
3030
</View>
31-
)
31+
);
3232
}
3333

3434
const styles = StyleSheet.create({
35-
loadingScreen: {
35+
loadingScreen: {
3636
flex: 1,
3737
display: "flex",
3838
alignItems: "center",
39-
justifyContent: "center"
39+
justifyContent: "center",
4040
},
4141
dotsWrapper: {
42-
width: 100
43-
}
44-
})
42+
width: 100,
43+
},
44+
});
4545
```
4646

4747
The above code will produce the same outcome as the demo screen capture.
@@ -77,7 +77,6 @@ default 20
7777

7878
This prop will control the size of each dot that will be displayed for the animation.
7979

80-
8180
### borderRadius
8281

8382
```
@@ -93,4 +92,13 @@ This prop will control the border radius of the dots in case you want a specific
9392
default 20
9493
```
9594

96-
This prop will control the height of the bouncing for the loading dots. The higher the value the higher the will bounce up and down. From `0` to `bounceHeight` and from `0` to `-bounceHeight`.
95+
This prop will control the height of the bouncing for the loading dots. The higher the value the higher the will bounce up and down. From `0` to `bounceHeight` and from `0` to `-bounceHeight`.
96+
97+
### components
98+
99+
```js
100+
@type {React.ReactNode}
101+
default null
102+
```
103+
104+
This prop will allow you to pass an array of the elements that you'd like to be rendered instead of the colored dots. If you pass `components` the `dots`, `colors`, `size` and `borderRadius` props will be ignored.

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{
22
"name": "react-native-loading-dots",
3-
"version": "1.2.0",
4-
"description": "A React Native loading component with dots and smooth animation",
3+
"version": "1.3.0",
4+
"description": "A React Native loading component with dots or custom components and smooth animation",
55
"repository": "https://github.com/alexvcasillas/react-native-loading-dots",
66
"author": "Alex Casillas",
77
"license": "MIT",
88
"main": "src/react-native-loading-dots.js",
99
"dependencies": {},
1010
"peerDependencies": {
1111
"react": "^16.8.6",
12-
"react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz"
13-
},
14-
"devDependencies": {}
12+
"react-native": "^0.68.2"
13+
}
1514
}

src/react-native-loading-dots.js

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import React, { useState, useEffect, useRef } from "react";
22
import { StyleSheet, Animated, Easing } from "react-native";
33

4-
const defaultColors = [
5-
"#4dabf7",
6-
"#3bc9db",
7-
"#38d9a9",
8-
"#69db7c"
9-
];
4+
const defaultColors = ["#4dabf7", "#3bc9db", "#38d9a9", "#69db7c"];
105

11-
function LoadingDots({ dots = 4, colors = defaultColors, size = 20, bounceHeight = 20, borderRadius }) {
6+
function LoadingDots({
7+
dots = 4,
8+
colors = defaultColors,
9+
size = 20,
10+
bounceHeight = 20,
11+
borderRadius,
12+
components = null,
13+
}) {
1214
const [animations, setAnimations] = useState([]);
1315
const [reverse, setReverse] = useState(false);
1416

1517
const opacity = useRef(new Animated.Value(0)).current;
1618

1719
useEffect(() => {
1820
const dotAnimations = [];
19-
// eslint-disable-next-line no-plusplus
20-
for (let i = 0; i < dots; i++) {
21+
let animationsAmount =
22+
!!components && Array.isArray(components) ? components.length : dots;
23+
for (let i = 0; i < animationsAmount; i++) {
2124
dotAnimations.push(new Animated.Value(0));
2225
}
2326
setAnimations(dotAnimations);
@@ -33,7 +36,7 @@ function LoadingDots({ dots = 4, colors = defaultColors, size = 20, bounceHeight
3336
Animated.timing(opacity, {
3437
toValue: 1,
3538
easing: Easing.ease,
36-
useNativeDriver: true
39+
useNativeDriver: true,
3740
}).start();
3841
}
3942

@@ -43,19 +46,19 @@ function LoadingDots({ dots = 4, colors = defaultColors, size = 20, bounceHeight
4346
toValue: reverseY ? bounceHeight : -bounceHeight,
4447
easing: Easing.bezier(0.41, -0.15, 0.56, 1.21),
4548
delay,
46-
useNativeDriver: true
49+
useNativeDriver: true,
4750
}),
4851
Animated.timing(node, {
4952
toValue: reverseY ? -bounceHeight : bounceHeight,
5053
easing: Easing.bezier(0.41, -0.15, 0.56, 1.21),
5154
delay,
52-
useNativeDriver: true
55+
useNativeDriver: true,
5356
}),
5457
Animated.timing(node, {
5558
toValue: 0,
5659
delay,
57-
useNativeDriver: true
58-
})
60+
useNativeDriver: true,
61+
}),
5962
]);
6063
return floatSequence;
6164
}
@@ -75,17 +78,29 @@ function LoadingDots({ dots = 4, colors = defaultColors, size = 20, bounceHeight
7578

7679
return (
7780
<Animated.View style={[styles.loading, { opacity }]}>
78-
{animations.map((animation, index) => (
79-
<Animated.View
80-
// eslint-disable-next-line react/no-array-index-key
81-
key={`loading-anim-${index}`}
82-
style={[
83-
{ width: size, height: size, borderRadius: borderRadius || size / 2 },
84-
{ backgroundColor: colors[index] || "#4dabf7" },
85-
{ transform: [{ translateY: animation }] }
86-
]}
87-
/>
88-
))}
81+
{animations.map((animation, index) =>
82+
components ? (
83+
<Animated.View
84+
key={`loading-anim-${index}`}
85+
style={[{ transform: [{ translateY: animation }] }]}
86+
>
87+
{components[index]}
88+
</Animated.View>
89+
) : (
90+
<Animated.View
91+
key={`loading-anim-${index}`}
92+
style={[
93+
{
94+
width: size,
95+
height: size,
96+
borderRadius: borderRadius || size / 2,
97+
},
98+
{ backgroundColor: colors[index] || "#4dabf7" },
99+
{ transform: [{ translateY: animation }] },
100+
]}
101+
/>
102+
)
103+
)}
89104
</Animated.View>
90105
);
91106
}
@@ -95,8 +110,8 @@ const styles = StyleSheet.create({
95110
display: "flex",
96111
flexDirection: "row",
97112
alignItems: "center",
98-
justifyContent: "space-between"
99-
}
113+
justifyContent: "space-between",
114+
},
100115
});
101116

102117
export default LoadingDots;

0 commit comments

Comments
 (0)