rn-snap-carousel-sg 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/Readme.md +15 -0
  2. package/index.tsx +158 -0
  3. package/package.json +43 -0
package/Readme.md ADDED
@@ -0,0 +1,15 @@
1
+ # react-native-sg-basic-carousel
2
+
3
+ An npm package to display basic carousel on react native applications
4
+
5
+ Example:
6
+
7
+ Android
8
+
9
+ <img width="347" alt="Screenshot 2024-10-11 at 12 36 06 AM" src="https://github.com/user-attachments/assets/303b35d4-425f-4910-992c-9df671a98dd1">
10
+
11
+ .
12
+
13
+ IOS
14
+
15
+ <img width="400" alt="Screenshot 2024-10-11 at 12 46 37 AM" src="https://github.com/user-attachments/assets/40d68bb1-2beb-44bb-93ca-a53a17d1caa9">
package/index.tsx ADDED
@@ -0,0 +1,158 @@
1
+ /* eslint-disable react-native/no-inline-styles */
2
+ import React, { useState, useRef, useEffect, JSX } from "react";
3
+ import {
4
+ View,
5
+ FlatList,
6
+ Dimensions,
7
+ StyleSheet,
8
+ ViewStyle,
9
+ StyleProp,
10
+ ImageBackground,
11
+ } from "react-native";
12
+
13
+ interface CarouselProps {
14
+ data: any[];
15
+ renderItem?: (item: any) => JSX.Element;
16
+ autoScroll?: boolean;
17
+ scrollInterval?: number;
18
+ showPagination?: boolean;
19
+ containerStyle?: StyleProp<ViewStyle>;
20
+ paginationContainerStyle?: StyleProp<ViewStyle>;
21
+ paginationDotStyle?: StyleProp<ViewStyle>;
22
+ }
23
+
24
+ const { width: screenWidth } = Dimensions.get("window");
25
+
26
+ const BasicCarousel = ({
27
+ data,
28
+ renderItem,
29
+ autoScroll = true,
30
+ scrollInterval = 3000,
31
+ showPagination = true,
32
+ containerStyle,
33
+ paginationContainerStyle,
34
+ paginationDotStyle,
35
+ }: CarouselProps) => {
36
+ const [activeIndex, setActiveIndex] = useState(0);
37
+ const flatListRef = useRef<FlatList<any>>(null);
38
+
39
+ useEffect(() => {
40
+ if (autoScroll) {
41
+ const timerId = setInterval(() => {
42
+ if (activeIndex === data.length - 1) {
43
+ setActiveIndex(0);
44
+ flatListRef.current?.scrollToIndex({ index: 0, animated: true });
45
+ } else {
46
+ setActiveIndex(activeIndex + 1);
47
+ flatListRef.current?.scrollToIndex({
48
+ index: activeIndex + 1,
49
+ animated: true,
50
+ });
51
+ }
52
+ }, scrollInterval);
53
+
54
+ return () => clearInterval(timerId);
55
+ }
56
+ }, [activeIndex, autoScroll, scrollInterval, data.length]);
57
+
58
+ const onViewableItemsChanged = useRef(
59
+ ({ viewableItems }: { viewableItems: any }) => {
60
+ if (viewableItems.length > 0) {
61
+ setActiveIndex(viewableItems[0].index);
62
+ }
63
+ },
64
+ ).current;
65
+
66
+ if (!data || data.length === 0) {
67
+ return null;
68
+ }
69
+
70
+ return (
71
+ <View style={containerStyle}>
72
+ <FlatList
73
+ ref={flatListRef}
74
+ data={data}
75
+ renderItem={({ item }) =>
76
+ renderItem ? renderItem(item) : <DefaultCarouselCard item={item} />
77
+ }
78
+ horizontal
79
+ pagingEnabled
80
+ showsHorizontalScrollIndicator={false}
81
+ onViewableItemsChanged={onViewableItemsChanged}
82
+ scrollEventThrottle={16}
83
+ viewabilityConfig={{ viewAreaCoveragePercentThreshold: 50 }}
84
+ keyExtractor={(_, index) => index.toString()}
85
+ />
86
+
87
+ {showPagination && (
88
+ <View style={[styles.paginationContainer, paginationContainerStyle]}>
89
+ {data.map((_, index) => (
90
+ <View
91
+ key={index}
92
+ style={[
93
+ styles.paginationDot,
94
+ paginationDotStyle,
95
+ {
96
+ width: index === activeIndex ? 20 : 8,
97
+ opacity: index === activeIndex ? 1 : 0.5,
98
+ },
99
+ ]}
100
+ />
101
+ ))}
102
+ </View>
103
+ )}
104
+ </View>
105
+ );
106
+ };
107
+
108
+ const DefaultCarouselCard = ({ item }: { item: any }) => {
109
+ const imageUri =
110
+ typeof item === "object" && item?.image
111
+ ? item.image
112
+ : "https://www.sciencealert.com/images/2026/01/ai_psychosis_header-3.jpg";
113
+
114
+ return (
115
+ <View style={styles.defaultCard}>
116
+ <ImageBackground
117
+ source={{ uri: imageUri }}
118
+ style={styles.imageBackground}
119
+ resizeMode="cover"
120
+ />
121
+ </View>
122
+ );
123
+ };
124
+
125
+ const styles = StyleSheet.create({
126
+ paginationContainer: {
127
+ flexDirection: "row",
128
+ justifyContent: "center",
129
+ alignItems: "center",
130
+ marginTop: 15,
131
+ },
132
+ paginationDot: {
133
+ height: 8,
134
+ backgroundColor: "#2196F3",
135
+ marginHorizontal: 4,
136
+ borderRadius: 4,
137
+ },
138
+ defaultCard: {
139
+ width: screenWidth * 0.9,
140
+ height: 200,
141
+ marginHorizontal: screenWidth * 0.05,
142
+ justifyContent: "center",
143
+ alignItems: "center",
144
+ borderRadius: 12,
145
+ padding: 1,
146
+ borderWidth: 0.5,
147
+ overflow: "hidden",
148
+ },
149
+ imageBackground: {
150
+ width: "100%",
151
+ borderRadius: 12,
152
+ overflow: "hidden",
153
+ height: "100%",
154
+ },
155
+ });
156
+
157
+ export default BasicCarousel;
158
+ export type { CarouselProps };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "rn-snap-carousel-sg",
3
+ "version": "1.0.0",
4
+ "description": "An easiest way to integrate carousels in your react native.",
5
+ "keywords": [
6
+ "npm",
7
+ "react",
8
+ "native",
9
+ "carousel",
10
+ "react",
11
+ "native",
12
+ "carousel",
13
+ "snap",
14
+ "carousel",
15
+ "best",
16
+ "carousel",
17
+ "for",
18
+ "react",
19
+ "native"
20
+ ],
21
+ "homepage": "https://github.com/siddhant124/rn-snap-carousel-sg#readme",
22
+ "bugs": {
23
+ "url": "https://github.com/siddhant124/rn-snap-carousel-sg/issues"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/siddhant124/rn-snap-carousel-sg.git"
28
+ },
29
+ "license": "Apache-2.0",
30
+ "author": "Siddhant Goyal",
31
+ "type": "commonjs",
32
+ "main": "index.tsx",
33
+ "scripts": {
34
+ "test": "npm run test"
35
+ },
36
+ "peerDependencies": {
37
+ "react": "19.2.0",
38
+ "react-native": "0.83.1"
39
+ },
40
+ "devDependencies": {
41
+ "@types/react": "^19.2.8"
42
+ }
43
+ }