react-native-sg-basic-carousel 1.0.6 → 1.0.7

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/index.tsx +158 -0
  2. package/package.json +26 -13
  3. package/tsconfig.json +12 -0
package/index.tsx ADDED
@@ -0,0 +1,158 @@
1
+ /* eslint-disable react-native/no-inline-styles */
2
+ import { 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[]; // Array of items with flexible structure but image is mandatory
15
+ renderItem?: (item: any) => JSX.Element; // Custom renderer
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
+ // Default Carousel Card: Used if no custom renderer is provided
109
+ const DefaultCarouselCard = ({ item }: { item: any }) => {
110
+ const imageUri =
111
+ typeof item === "object" && item?.image
112
+ ? item.image
113
+ : "https://www.sciencealert.com/images/2026/01/ai_psychosis_header-3.jpg";
114
+
115
+ return (
116
+ <View style={styles.defaultCard}>
117
+ <ImageBackground
118
+ source={{ uri: imageUri }}
119
+ style={styles.imageBackground}
120
+ resizeMode="cover"
121
+ />
122
+ </View>
123
+ );
124
+ };
125
+
126
+ const styles = StyleSheet.create({
127
+ paginationContainer: {
128
+ flexDirection: "row",
129
+ justifyContent: "center",
130
+ alignItems: "center",
131
+ marginTop: 15,
132
+ },
133
+ paginationDot: {
134
+ height: 8,
135
+ backgroundColor: "#2196F3",
136
+ marginHorizontal: 4,
137
+ borderRadius: 4,
138
+ },
139
+ defaultCard: {
140
+ width: screenWidth * 0.9,
141
+ height: 200,
142
+ marginHorizontal: screenWidth * 0.05,
143
+ justifyContent: "center",
144
+ alignItems: "center",
145
+ borderRadius: 12,
146
+ padding: 1,
147
+ borderWidth: 0.5,
148
+ overflow: "hidden",
149
+ },
150
+ imageBackground: {
151
+ width: "100%",
152
+ borderRadius: 12,
153
+ overflow: "hidden",
154
+ height: "100%",
155
+ },
156
+ });
157
+
158
+ export default BasicCarousel;
package/package.json CHANGED
@@ -1,19 +1,32 @@
1
1
  {
2
2
  "name": "react-native-sg-basic-carousel",
3
- "version": "1.0.6",
4
- "main": "dist/index.js",
5
- "types": "dist/index.d.ts",
6
- "files": [
7
- "dist"
3
+ "version": "1.0.7",
4
+ "description": "An npm package to display basic carousel on react native applications",
5
+ "main": "index.tsx",
6
+ "scripts": {
7
+ "test": "npm run test"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/siddhant124/react-native-sg-basic-carousel.git"
12
+ },
13
+ "keywords": [
14
+ "npm",
15
+ "carousel",
16
+ "mobile",
17
+ "react",
18
+ "native"
8
19
  ],
9
- "peerDependencies": {
10
- "react": ">=17.0.0",
11
- "react-native": ">=0.68.0"
20
+ "author": "Siddhant Goyal",
21
+ "license": "Apache-2.0",
22
+ "bugs": {
23
+ "url": "https://github.com/siddhant124/react-native-sg-basic-carousel/issues"
12
24
  },
13
- "devDependencies": {
14
- "react": "19.2.0",
15
- "react-native": "0.83.0",
16
- "@types/react": "^19.2.0",
17
- "typescript": "^5.0.0"
25
+ "homepage": "https://github.com/siddhant124/react-native-sg-basic-carousel#readme",
26
+ "dependencies": {
27
+ "@types/react": "^18.3.11",
28
+ "@types/react-native": "^0.72.8",
29
+ "react": "^18.3.1",
30
+ "react-native": "^0.75.4"
18
31
  }
19
32
  }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2017",
4
+ "jsx": "react-native",
5
+ "declaration": true,
6
+ "outDir": "dist",
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "moduleResolution": "node"
10
+ },
11
+ "include": ["index.tsx"]
12
+ }