@shoutem/ui 9.3.0-rc.0 → 9.3.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.
|
@@ -5,7 +5,7 @@ import React, {
|
|
|
5
5
|
useRef,
|
|
6
6
|
useState,
|
|
7
7
|
} from 'react';
|
|
8
|
-
import { Animated } from 'react-native';
|
|
8
|
+
import { Animated, useWindowDimensions } from 'react-native';
|
|
9
9
|
import _ from 'lodash';
|
|
10
10
|
import PropTypes from 'prop-types';
|
|
11
11
|
import { connectStyle } from '@shoutem/theme';
|
|
@@ -44,9 +44,10 @@ const defaultGetImageSource = item => item.source;
|
|
|
44
44
|
* image cards that snap one by one, with neighbouring cards peeking in and
|
|
45
45
|
* scaling/fading with the scroll position. The first and last slides rest
|
|
46
46
|
* against the list edges; every other slide rests centered. Also owns page
|
|
47
|
-
* indicator dots, arrows, and autoplay.
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* indicator dots, arrows, and autoplay. The list renders on the first frame
|
|
48
|
+
* using an estimated width and already rests on `initialIndex`; it renders
|
|
49
|
+
* nothing at all when `data` is empty. Callers customize slides through
|
|
50
|
+
* `renderOverlay` and `renderImagePlaceholder`.
|
|
50
51
|
*
|
|
51
52
|
* @param {Array} [data] The slides to render.
|
|
52
53
|
* @param {Function} [getImageSource] `(item) => imageSource`. Defaults to the
|
|
@@ -54,8 +55,13 @@ const defaultGetImageSource = item => item.source;
|
|
|
54
55
|
* @param {Function} [keyExtractor] `(item, index) => string`. Defaults to the
|
|
55
56
|
* item's `id`, falling back to its index.
|
|
56
57
|
* @param {number} [cardAspectRatio] The shape of every slide as width / height.
|
|
57
|
-
* @param {number} [initialIndex] The 0-based slide index to open on
|
|
58
|
-
*
|
|
58
|
+
* @param {number} [initialIndex] The 0-based slide index to open on. The first
|
|
59
|
+
* frame already rests on it.
|
|
60
|
+
* @param {number} [initialWidth] The estimated carousel width used until the
|
|
61
|
+
* real width is measured. Defaults to the window width, which is exact for a
|
|
62
|
+
* full-width carousel; pass it when the carousel is narrower than the window.
|
|
63
|
+
* `onLayout` corrects any difference, which can re-snap neighbouring slides
|
|
64
|
+
* by a frame.
|
|
59
65
|
* @param {boolean} [autoplay] Whether to automatically advance slides on a
|
|
60
66
|
* timer. Paused while the user is dragging.
|
|
61
67
|
* @param {number} [autoplayIntervalMs] The autoplay interval, in milliseconds.
|
|
@@ -68,9 +74,6 @@ const defaultGetImageSource = item => item.source;
|
|
|
68
74
|
* rendered inside the slide card, on top of the image.
|
|
69
75
|
* @param {Function} [renderImagePlaceholder] `(item, { index }) => JSX.Element`
|
|
70
76
|
* rendered over a slide's image until it has loaded.
|
|
71
|
-
* @param {Function} [renderPlaceholder] `({ cardAspectRatio }) => JSX.Element`
|
|
72
|
-
* rendered instead of the list until the width is measured. Defaults to an
|
|
73
|
-
* empty view with the height the loaded carousel will have.
|
|
74
77
|
* @param {Function} [onIndexChange] `(index) => void`, called when the active
|
|
75
78
|
* slide changes (not on mount).
|
|
76
79
|
* @param {Object} style Resolved styles, injected by connectStyle. Geometry is
|
|
@@ -80,28 +83,30 @@ const defaultGetImageSource = item => item.source;
|
|
|
80
83
|
* @returns {JSX.Element|null}
|
|
81
84
|
*/
|
|
82
85
|
const ImageCarousel = ({
|
|
83
|
-
data,
|
|
84
|
-
getImageSource,
|
|
85
|
-
keyExtractor,
|
|
86
|
-
cardAspectRatio,
|
|
87
|
-
initialIndex,
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
86
|
+
data = [],
|
|
87
|
+
getImageSource = defaultGetImageSource,
|
|
88
|
+
keyExtractor = defaultKeyExtractor,
|
|
89
|
+
cardAspectRatio = 16 / 9,
|
|
90
|
+
initialIndex = 0,
|
|
91
|
+
initialWidth,
|
|
92
|
+
autoplay = false,
|
|
93
|
+
autoplayIntervalMs = 5000,
|
|
94
|
+
showPageIndicators = true,
|
|
95
|
+
maxPageIndicators = 10,
|
|
96
|
+
showArrows = false,
|
|
93
97
|
renderOverlay,
|
|
94
98
|
renderImagePlaceholder,
|
|
95
|
-
renderPlaceholder,
|
|
96
99
|
onIndexChange,
|
|
97
|
-
style,
|
|
100
|
+
style = {},
|
|
98
101
|
}) => {
|
|
102
|
+
const { width: windowWidth } = useWindowDimensions();
|
|
99
103
|
const listRef = useRef(null);
|
|
100
104
|
const isFirstSnapRender = useRef(true);
|
|
101
105
|
const isFirstIndexRender = useRef(true);
|
|
102
|
-
const scrollX = useRef(new Animated.Value(0)).current;
|
|
103
106
|
|
|
104
|
-
const [containerWidth, setContainerWidth] = useState(
|
|
107
|
+
const [containerWidth, setContainerWidth] = useState(
|
|
108
|
+
initialWidth ?? windowWidth,
|
|
109
|
+
);
|
|
105
110
|
const [activeIndex, setActiveIndex] = useState(initialIndex);
|
|
106
111
|
const [isInteracting, setIsInteracting] = useState(false);
|
|
107
112
|
|
|
@@ -118,6 +123,11 @@ const ImageCarousel = ({
|
|
|
118
123
|
[data.length, containerWidth, cardWidth, cardSpacing],
|
|
119
124
|
);
|
|
120
125
|
|
|
126
|
+
// Offset of `initialIndex` computed from the estimated width, so the very
|
|
127
|
+
// first frame already rests on the right slide.
|
|
128
|
+
const initialOffset = useRef(snapOffsets[initialIndex] ?? 0).current;
|
|
129
|
+
const scrollX = useRef(new Animated.Value(initialOffset)).current;
|
|
130
|
+
|
|
121
131
|
const itemWrapperStyle = useMemo(
|
|
122
132
|
() => ({
|
|
123
133
|
width: cardWidth,
|
|
@@ -179,12 +189,13 @@ const ImageCarousel = ({
|
|
|
179
189
|
return;
|
|
180
190
|
}
|
|
181
191
|
|
|
182
|
-
//
|
|
183
|
-
// changes:
|
|
184
|
-
//
|
|
185
|
-
//
|
|
186
|
-
//
|
|
187
|
-
//
|
|
192
|
+
// Re-positions the list on the active slide whenever the measured width
|
|
193
|
+
// changes: a corrected width estimate or a device rotation re-snaps to the
|
|
194
|
+
// slide that was already active, read at fire time (as are the snap
|
|
195
|
+
// offsets). The list deliberately has no `initialScrollIndex` - the list's
|
|
196
|
+
// own mount-time scroll pass renders a frame with the content shifted a
|
|
197
|
+
// full card before it self-corrects - so `contentOffset` positions the
|
|
198
|
+
// first frame instead.
|
|
188
199
|
listRef.current?.scrollToOffset({
|
|
189
200
|
offset: snapOffsets[activeIndex] ?? 0,
|
|
190
201
|
animated: false,
|
|
@@ -316,43 +327,31 @@ const ImageCarousel = ({
|
|
|
316
327
|
return null;
|
|
317
328
|
}
|
|
318
329
|
|
|
319
|
-
const placeholderStyle = [
|
|
320
|
-
style.placeholder,
|
|
321
|
-
{ height: cardWidth / cardAspectRatio },
|
|
322
|
-
];
|
|
323
|
-
const placeholder = renderPlaceholder ? (
|
|
324
|
-
renderPlaceholder({ cardAspectRatio })
|
|
325
|
-
) : (
|
|
326
|
-
<View style={placeholderStyle} />
|
|
327
|
-
);
|
|
328
|
-
|
|
329
330
|
return (
|
|
330
331
|
<View style={style.container} onLayout={handleLayout}>
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
/>
|
|
355
|
-
)}
|
|
332
|
+
<Animated.FlatList
|
|
333
|
+
ref={listRef}
|
|
334
|
+
horizontal
|
|
335
|
+
data={data}
|
|
336
|
+
renderItem={renderSlide}
|
|
337
|
+
keyExtractor={keyExtractor}
|
|
338
|
+
showsHorizontalScrollIndicator={false}
|
|
339
|
+
snapToOffsets={snapOffsets}
|
|
340
|
+
snapToAlignment="start"
|
|
341
|
+
decelerationRate="fast"
|
|
342
|
+
disableIntervalMomentum
|
|
343
|
+
getItemLayout={getItemLayout}
|
|
344
|
+
scrollEnabled={data.length > 1}
|
|
345
|
+
bounces={false}
|
|
346
|
+
scrollEventThrottle={16}
|
|
347
|
+
removeClippedSubviews={false}
|
|
348
|
+
contentOffset={{ x: initialOffset, y: 0 }}
|
|
349
|
+
onScroll={handleScrollEvent}
|
|
350
|
+
onScrollBeginDrag={handleScrollBeginDrag}
|
|
351
|
+
onScrollEndDrag={handleScrollEndDrag}
|
|
352
|
+
onMomentumScrollEnd={handleMomentumScrollEnd}
|
|
353
|
+
contentContainerStyle={style.listContent}
|
|
354
|
+
/>
|
|
356
355
|
{showPageIndicators && data.length > 1 && (
|
|
357
356
|
<PageIndicators
|
|
358
357
|
activeIndex={activeIndex}
|
|
@@ -388,35 +387,17 @@ ImageCarousel.propTypes = {
|
|
|
388
387
|
data: PropTypes.array,
|
|
389
388
|
getImageSource: PropTypes.func,
|
|
390
389
|
initialIndex: PropTypes.number,
|
|
390
|
+
initialWidth: PropTypes.number,
|
|
391
391
|
keyExtractor: PropTypes.func,
|
|
392
392
|
maxPageIndicators: PropTypes.number,
|
|
393
393
|
renderImagePlaceholder: PropTypes.func,
|
|
394
394
|
renderOverlay: PropTypes.func,
|
|
395
|
-
renderPlaceholder: PropTypes.func,
|
|
396
395
|
showArrows: PropTypes.bool,
|
|
397
396
|
showPageIndicators: PropTypes.bool,
|
|
398
397
|
style: PropTypes.object,
|
|
399
398
|
onIndexChange: PropTypes.func,
|
|
400
399
|
};
|
|
401
400
|
|
|
402
|
-
ImageCarousel.defaultProps = {
|
|
403
|
-
autoplay: false,
|
|
404
|
-
autoplayIntervalMs: 5000,
|
|
405
|
-
cardAspectRatio: 16 / 9,
|
|
406
|
-
data: [],
|
|
407
|
-
getImageSource: defaultGetImageSource,
|
|
408
|
-
initialIndex: 0,
|
|
409
|
-
keyExtractor: defaultKeyExtractor,
|
|
410
|
-
maxPageIndicators: 10,
|
|
411
|
-
renderImagePlaceholder: undefined,
|
|
412
|
-
renderOverlay: undefined,
|
|
413
|
-
renderPlaceholder: undefined,
|
|
414
|
-
showArrows: false,
|
|
415
|
-
showPageIndicators: true,
|
|
416
|
-
style: {},
|
|
417
|
-
onIndexChange: undefined,
|
|
418
|
-
};
|
|
419
|
-
|
|
420
401
|
const StyledImageCarousel = connectStyle('shoutem.ui.ImageCarousel')(
|
|
421
402
|
ImageCarousel,
|
|
422
403
|
);
|
|
@@ -22,12 +22,12 @@ import { View } from '../View';
|
|
|
22
22
|
*/
|
|
23
23
|
const ImageCarouselSlide = ({
|
|
24
24
|
item,
|
|
25
|
-
index,
|
|
26
|
-
isActive,
|
|
25
|
+
index = 0,
|
|
26
|
+
isActive = false,
|
|
27
27
|
source,
|
|
28
28
|
renderOverlay,
|
|
29
29
|
renderImagePlaceholder,
|
|
30
|
-
style,
|
|
30
|
+
style = {},
|
|
31
31
|
}) => {
|
|
32
32
|
const [isImageLoaded, setImageLoaded] = useState(false);
|
|
33
33
|
|
|
@@ -67,16 +67,6 @@ ImageCarouselSlide.propTypes = {
|
|
|
67
67
|
style: PropTypes.object,
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
-
ImageCarouselSlide.defaultProps = {
|
|
71
|
-
index: 0,
|
|
72
|
-
isActive: false,
|
|
73
|
-
item: undefined,
|
|
74
|
-
renderImagePlaceholder: undefined,
|
|
75
|
-
renderOverlay: undefined,
|
|
76
|
-
source: undefined,
|
|
77
|
-
style: {},
|
|
78
|
-
};
|
|
79
|
-
|
|
80
70
|
const StyledImageCarouselSlide = connectStyle('shoutem.ui.ImageCarouselSlide')(
|
|
81
71
|
ImageCarouselSlide,
|
|
82
72
|
);
|
package/package.json
CHANGED