@momo-kits/carousel 0.151.1-beta.4 → 0.151.1-test.5
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.
- package/PROPS.md +188 -0
- package/index.tsx +465 -778
- package/package.json +20 -20
package/PROPS.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# Carousel Component Props
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
The Carousel component is a customizable, performant carousel/slider component built on top of React Native's `FlatList`. It supports features like looping, autoplay, animations, and custom layouts.
|
|
5
|
+
|
|
6
|
+
## Props
|
|
7
|
+
|
|
8
|
+
### Data & Rendering
|
|
9
|
+
|
|
10
|
+
| Prop | Type | Default | Required | Description |
|
|
11
|
+
|------|------|---------|----------|-------------|
|
|
12
|
+
| `data` | `any[]` | - | ✅ | Array of data items to be rendered in the carousel |
|
|
13
|
+
| `renderItem` | `(info: { item: any; index: number }) => React.ReactNode` | - | ✅ | Function that renders each carousel item |
|
|
14
|
+
| `keyExtractor` | `(item: any, index: number) => string` | Auto-generated | ❌ | Function to extract unique keys for list items |
|
|
15
|
+
| `firstItem` | `number` | `0` | ❌ | Index of the initial active item |
|
|
16
|
+
|
|
17
|
+
### Layout & Sizing
|
|
18
|
+
|
|
19
|
+
| Prop | Type | Default | Required | Description |
|
|
20
|
+
|------|------|---------|----------|-------------|
|
|
21
|
+
| `itemWidth` | `number` | Auto-calculated | ❌ | Width of each carousel item. If not specified, calculated based on `visibleItem` |
|
|
22
|
+
| `visibleItem` | `number` | `1` | ❌ | Number of items visible at once in the viewport |
|
|
23
|
+
| `full` | `boolean` | `false` | ❌ | If `true`, items will take full container width with no margins |
|
|
24
|
+
| `activeSlideOffset` | `number` | `20` | ❌ | Offset from center to determine when a slide is considered "active" |
|
|
25
|
+
|
|
26
|
+
### Scrolling & Snapping
|
|
27
|
+
|
|
28
|
+
| Prop | Type | Default | Required | Description |
|
|
29
|
+
|------|------|---------|----------|-------------|
|
|
30
|
+
| `scrollEnabled` | `boolean` | `true` | ❌ | Enable or disable scrolling |
|
|
31
|
+
| `enableSnap` | `boolean` | `true` | ❌ | Enable snapping to items after scroll |
|
|
32
|
+
| `snapToInterval` | `number \| Animated.Value \| Animated.AnimatedInterpolation` | Auto-calculated | ❌ | Custom snap interval. If not provided, calculated from item dimensions |
|
|
33
|
+
| `disableIntervalMomentum` | `boolean` | `true` (Android) / `false` (iOS) | ❌ | Disable momentum scrolling. Default varies by platform |
|
|
34
|
+
|
|
35
|
+
### Loop Mode
|
|
36
|
+
|
|
37
|
+
| Prop | Type | Default | Required | Description |
|
|
38
|
+
|------|------|---------|----------|-------------|
|
|
39
|
+
| `loop` | `boolean` | `false` | ❌ | Enable infinite loop mode |
|
|
40
|
+
| `loopClonesPerSide` | `number` | `3` | ❌ | Number of cloned items on each side when loop is enabled |
|
|
41
|
+
|
|
42
|
+
### Autoplay
|
|
43
|
+
|
|
44
|
+
| Prop | Type | Default | Required | Description |
|
|
45
|
+
|------|------|---------|----------|-------------|
|
|
46
|
+
| `autoplay` | `boolean` | `false` | ❌ | Enable autoplay |
|
|
47
|
+
| `autoplayDelay` | `number` | `1000` | ❌ | Delay in milliseconds before autoplay starts |
|
|
48
|
+
| `autoplayInterval` | `number` | `3000` | ❌ | Interval in milliseconds between automatic slides |
|
|
49
|
+
|
|
50
|
+
### Animations
|
|
51
|
+
|
|
52
|
+
| Prop | Type | Default | Required | Description |
|
|
53
|
+
|------|------|---------|----------|-------------|
|
|
54
|
+
| `inactiveSlideOpacity` | `number` | `1` | ❌ | Opacity value for inactive slides (0-1). Values < 1 enable opacity animation |
|
|
55
|
+
| `inactiveSlideScale` | `number` | `1` | ❌ | Scale value for inactive slides (0-1). Values < 1 enable scale animation |
|
|
56
|
+
| `apparitionDelay` | `number` | `0` | ❌ | Delay in milliseconds before carousel appears |
|
|
57
|
+
|
|
58
|
+
### Styling
|
|
59
|
+
|
|
60
|
+
| Prop | Type | Default | Required | Description |
|
|
61
|
+
|------|------|---------|----------|-------------|
|
|
62
|
+
| `style` | `ViewStyle` | `{}` | ❌ | Style for the carousel container |
|
|
63
|
+
| `slideStyle` | `StyleProp<ViewStyle>` | `{}` | ❌ | Style applied to each individual slide |
|
|
64
|
+
| `contentContainerStyle` | `StyleProp<ViewStyle>` | `undefined` | ❌ | Style for the inner content container |
|
|
65
|
+
|
|
66
|
+
### Callbacks
|
|
67
|
+
|
|
68
|
+
| Prop | Type | Default | Required | Description |
|
|
69
|
+
|------|------|---------|----------|-------------|
|
|
70
|
+
| `onSnapToItem` | `(index: number) => void` | - | ❌ | Callback fired when a slide snaps to position. Receives the data index (not the loop index) |
|
|
71
|
+
| `onScrollIndexChanged` | `(index: number) => void` | ❌ | ❌ | Callback fired when the active index changes during scroll |
|
|
72
|
+
| `onScroll` | `any` | - | ❌ | Scroll event handler (can be Animated.event) |
|
|
73
|
+
| `onMomentumScrollEnd` | `(event: NativeSyntheticEvent<NativeScrollEvent>) => void` | - | ❌ | Callback fired when momentum scroll ends |
|
|
74
|
+
| `onTouchStart` | `(event: any) => void` | - | ❌ | Callback fired when user touches the carousel |
|
|
75
|
+
| `onTouchEnd` | `(event: GestureResponderEvent) => void` | - | ❌ | Callback fired when user releases touch |
|
|
76
|
+
| `onLayout` | `(event: LayoutChangeEvent) => void` | - | ❌ | Callback fired when layout changes |
|
|
77
|
+
|
|
78
|
+
### Advanced
|
|
79
|
+
|
|
80
|
+
| Prop | Type | Default | Required | Description |
|
|
81
|
+
|------|------|---------|----------|-------------|
|
|
82
|
+
| `getItemLayout` | `(data: any, index: number) => { length: number; offset: number; index: number }` | Auto-calculated | ❌ | Custom item layout calculation for performance optimization |
|
|
83
|
+
|
|
84
|
+
## Ref Methods
|
|
85
|
+
|
|
86
|
+
When using a ref, the following methods are available:
|
|
87
|
+
|
|
88
|
+
| Method | Parameters | Description |
|
|
89
|
+
|--------|------------|-------------|
|
|
90
|
+
| `snapToItem` | `index: number, animated?: boolean, fireCallback?: boolean` | Snap to a specific item by index |
|
|
91
|
+
| `snapToNext` | `animated?: boolean, fireCallback?: boolean` | Snap to the next item |
|
|
92
|
+
| `snapToPrev` | `animated?: boolean, fireCallback?: boolean` | Snap to the previous item |
|
|
93
|
+
| `startAutoplay` | - | Start autoplay programmatically |
|
|
94
|
+
| `pauseAutoPlay` | - | Pause autoplay (can be resumed) |
|
|
95
|
+
| `stopAutoplay` | - | Stop autoplay completely |
|
|
96
|
+
|
|
97
|
+
## Usage Example
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
import { Carousel, CarouselRef } from '@momo-kits/carousel';
|
|
101
|
+
import { useRef } from 'react';
|
|
102
|
+
|
|
103
|
+
function MyCarousel() {
|
|
104
|
+
const carouselRef = useRef<CarouselRef>(null);
|
|
105
|
+
|
|
106
|
+
const data = [1, 2, 3, 4, 5];
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<Carousel
|
|
110
|
+
ref={carouselRef}
|
|
111
|
+
data={data}
|
|
112
|
+
renderItem={({ item, index }) => (
|
|
113
|
+
<View>
|
|
114
|
+
<Text>Item {item}</Text>
|
|
115
|
+
</View>
|
|
116
|
+
)}
|
|
117
|
+
// Layout
|
|
118
|
+
visibleItem={1}
|
|
119
|
+
full={false}
|
|
120
|
+
|
|
121
|
+
// Scrolling
|
|
122
|
+
enableSnap={true}
|
|
123
|
+
loop={true}
|
|
124
|
+
|
|
125
|
+
// Autoplay
|
|
126
|
+
autoplay={true}
|
|
127
|
+
autoplayDelay={1000}
|
|
128
|
+
autoplayInterval={3000}
|
|
129
|
+
|
|
130
|
+
// Animations
|
|
131
|
+
inactiveSlideOpacity={0.7}
|
|
132
|
+
inactiveSlideScale={0.9}
|
|
133
|
+
|
|
134
|
+
// Callbacks
|
|
135
|
+
onSnapToItem={(index) => console.log('Snapped to:', index)}
|
|
136
|
+
/>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Common Patterns
|
|
142
|
+
|
|
143
|
+
### Full-width Carousel with Loop
|
|
144
|
+
```tsx
|
|
145
|
+
<Carousel
|
|
146
|
+
data={items}
|
|
147
|
+
renderItem={renderItem}
|
|
148
|
+
full={true}
|
|
149
|
+
loop={true}
|
|
150
|
+
enableSnap={true}
|
|
151
|
+
/>
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Multi-item Carousel
|
|
155
|
+
```tsx
|
|
156
|
+
<Carousel
|
|
157
|
+
data={items}
|
|
158
|
+
renderItem={renderItem}
|
|
159
|
+
visibleItem={3}
|
|
160
|
+
enableSnap={false}
|
|
161
|
+
/>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Autoplay with Animations
|
|
165
|
+
```tsx
|
|
166
|
+
<Carousel
|
|
167
|
+
data={items}
|
|
168
|
+
renderItem={renderItem}
|
|
169
|
+
autoplay={true}
|
|
170
|
+
autoplayInterval={3000}
|
|
171
|
+
loop={true}
|
|
172
|
+
inactiveSlideOpacity={0.5}
|
|
173
|
+
inactiveSlideScale={0.85}
|
|
174
|
+
/>
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Notes
|
|
178
|
+
|
|
179
|
+
- **Loop Mode**: When `loop={true}`, the carousel creates clones of items to enable seamless infinite scrolling. The number of clones is controlled by `loopClonesPerSide`.
|
|
180
|
+
- **Index Handling**: Callback functions like `onSnapToItem` receive the data index (0 to data.length-1), not the internal loop index.
|
|
181
|
+
- **Performance**: The component uses `PureComponent` and optimized rendering. Set `itemWidth` and `visibleItem` appropriately for best performance.
|
|
182
|
+
- **Platform Differences**: Some default values differ between iOS and Android (e.g., `disableIntervalMomentum`) to provide optimal experience on each platform.
|
|
183
|
+
- **Autoplay Behavior**: Autoplay automatically pauses when user touches the carousel and resumes when touch ends (if it was active before).
|
|
184
|
+
- **Item Width Calculation**:
|
|
185
|
+
- If `full={true}`: items take full container width
|
|
186
|
+
- If `itemWidth` is provided: uses that value
|
|
187
|
+
- Otherwise: calculated based on `visibleItem` count with appropriate spacing
|
|
188
|
+
|