@shoutem/ui 9.3.0 → 9.3.1-rc.1
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,12 @@ import React, {
|
|
|
5
5
|
useRef,
|
|
6
6
|
useState,
|
|
7
7
|
} from 'react';
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
Animated,
|
|
10
|
+
Platform,
|
|
11
|
+
useWindowDimensions,
|
|
12
|
+
View as RNView,
|
|
13
|
+
} from 'react-native';
|
|
9
14
|
import _ from 'lodash';
|
|
10
15
|
import PropTypes from 'prop-types';
|
|
11
16
|
import { connectStyle } from '@shoutem/theme';
|
|
@@ -39,14 +44,31 @@ const defaultKeyExtractor = (item, index) => item.id ?? String(index);
|
|
|
39
44
|
*/
|
|
40
45
|
const defaultGetImageSource = item => item.source;
|
|
41
46
|
|
|
47
|
+
const isWeb = Platform.OS === 'web';
|
|
48
|
+
|
|
49
|
+
// React Native Web ignores `snapToOffsets`, so on web the browser's CSS scroll
|
|
50
|
+
// snapping does it instead. Centering every slot reproduces
|
|
51
|
+
// `resolveSnapOffsets`: the browser clamps snap positions to the scrollable
|
|
52
|
+
// range, so the first and last slides rest against the list edges.
|
|
53
|
+
// `scrollSnapStop` keeps a fling to one slide at a time, like
|
|
54
|
+
// `disableIntervalMomentum` on native.
|
|
55
|
+
const webListSnapStyle = isWeb ? { scrollSnapType: 'x mandatory' } : null;
|
|
56
|
+
const webSlotSnapStyle = isWeb
|
|
57
|
+
? { scrollSnapAlign: 'center', scrollSnapStop: 'always' }
|
|
58
|
+
: null;
|
|
59
|
+
|
|
60
|
+
// Fills the slot, which owns the slide's size and spacing.
|
|
61
|
+
const slideFillStyle = { flex: 1 };
|
|
62
|
+
|
|
42
63
|
/**
|
|
43
64
|
* A horizontally-swipeable image carousel: pages through `data` as fixed-width
|
|
44
65
|
* image cards that snap one by one, with neighbouring cards peeking in and
|
|
45
66
|
* scaling/fading with the scroll position. The first and last slides rest
|
|
46
67
|
* against the list edges; every other slide rests centered. Also owns page
|
|
47
|
-
* indicator dots, arrows, and autoplay.
|
|
48
|
-
*
|
|
49
|
-
*
|
|
68
|
+
* indicator dots, arrows, and autoplay. On web, where `snapToOffsets` isn't
|
|
69
|
+
* supported, slides snap through CSS scroll snapping. The list renders on the
|
|
70
|
+
* first frame using an estimated width and already rests on `initialIndex`; it
|
|
71
|
+
* renders nothing at all when `data` is empty. Callers customize slides through
|
|
50
72
|
* `renderOverlay` and `renderImagePlaceholder`.
|
|
51
73
|
*
|
|
52
74
|
* @param {Array} [data] The slides to render.
|
|
@@ -76,10 +98,19 @@ const defaultGetImageSource = item => item.source;
|
|
|
76
98
|
* rendered over a slide's image until it has loaded.
|
|
77
99
|
* @param {Function} [onIndexChange] `(index) => void`, called when the active
|
|
78
100
|
* slide changes (not on mount).
|
|
101
|
+
* @param {Function} [onImageLoadEnd] `(item, { index }) => void`, called when a
|
|
102
|
+
* slide's image finishes loading, whether it succeeded or failed.
|
|
103
|
+
* @param {boolean} [scrollEnabled] Whether the user can swipe between slides.
|
|
104
|
+
* Defaults to true. When false, autoplay does not advance and arrows are
|
|
105
|
+
* hidden; page indicators are unaffected. Swiping is also disabled when
|
|
106
|
+
* there's only one slide.
|
|
79
107
|
* @param {Object} style Resolved styles, injected by connectStyle. Geometry is
|
|
80
108
|
* read from the `cardWidth`, `cardSpacing`, `inactiveScale` and
|
|
81
|
-
* `inactiveOpacity` scalars.
|
|
82
|
-
* over
|
|
109
|
+
* `inactiveOpacity` scalars. `style.slide` is passed to every
|
|
110
|
+
* `ImageCarouselSlide` and merged over that component's theme, so callers
|
|
111
|
+
* can restyle the slide card (e.g. `slide.card` background, corner radius,
|
|
112
|
+
* border) from the carousel's style. A `style` prop passed by the caller is
|
|
113
|
+
* merged over the theme styles by connectStyle.
|
|
83
114
|
* @returns {JSX.Element|null}
|
|
84
115
|
*/
|
|
85
116
|
const ImageCarousel = ({
|
|
@@ -97,6 +128,8 @@ const ImageCarousel = ({
|
|
|
97
128
|
renderOverlay,
|
|
98
129
|
renderImagePlaceholder,
|
|
99
130
|
onIndexChange,
|
|
131
|
+
onImageLoadEnd,
|
|
132
|
+
scrollEnabled = true,
|
|
100
133
|
style = {},
|
|
101
134
|
}) => {
|
|
102
135
|
const { width: windowWidth } = useWindowDimensions();
|
|
@@ -133,6 +166,7 @@ const ImageCarousel = ({
|
|
|
133
166
|
width: cardWidth,
|
|
134
167
|
marginHorizontal: cardSpacing / 2,
|
|
135
168
|
aspectRatio: cardAspectRatio,
|
|
169
|
+
...webSlotSnapStyle,
|
|
136
170
|
}),
|
|
137
171
|
[cardWidth, cardSpacing, cardAspectRatio],
|
|
138
172
|
);
|
|
@@ -148,7 +182,13 @@ const ImageCarousel = ({
|
|
|
148
182
|
);
|
|
149
183
|
|
|
150
184
|
useEffect(() => {
|
|
151
|
-
if (
|
|
185
|
+
if (
|
|
186
|
+
!autoplay ||
|
|
187
|
+
!scrollEnabled ||
|
|
188
|
+
data.length < 2 ||
|
|
189
|
+
!snapInterval ||
|
|
190
|
+
isInteracting
|
|
191
|
+
) {
|
|
152
192
|
return undefined;
|
|
153
193
|
}
|
|
154
194
|
|
|
@@ -160,6 +200,7 @@ const ImageCarousel = ({
|
|
|
160
200
|
}, [
|
|
161
201
|
autoplay,
|
|
162
202
|
autoplayIntervalMs,
|
|
203
|
+
scrollEnabled,
|
|
163
204
|
activeIndex,
|
|
164
205
|
isInteracting,
|
|
165
206
|
data.length,
|
|
@@ -295,23 +336,31 @@ const ImageCarousel = ({
|
|
|
295
336
|
],
|
|
296
337
|
};
|
|
297
338
|
|
|
339
|
+
// The slot stays untransformed so its layout box (which web scroll
|
|
340
|
+
// snapping aligns to) is identical for every slide; only the inner view
|
|
341
|
+
// scales and shifts.
|
|
298
342
|
return (
|
|
299
|
-
<
|
|
300
|
-
<
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
343
|
+
<RNView style={itemWrapperStyle}>
|
|
344
|
+
<Animated.View style={[slideFillStyle, animatedStyle]}>
|
|
345
|
+
<ImageCarouselSlide
|
|
346
|
+
item={item}
|
|
347
|
+
index={index}
|
|
348
|
+
isActive={index === activeIndex}
|
|
349
|
+
source={getImageSource(item)}
|
|
350
|
+
renderOverlay={renderOverlay}
|
|
351
|
+
renderImagePlaceholder={renderImagePlaceholder}
|
|
352
|
+
onImageLoadEnd={onImageLoadEnd}
|
|
353
|
+
style={style.slide}
|
|
354
|
+
/>
|
|
355
|
+
</Animated.View>
|
|
356
|
+
</RNView>
|
|
309
357
|
);
|
|
310
358
|
},
|
|
311
359
|
[
|
|
312
360
|
getImageSource,
|
|
313
361
|
renderOverlay,
|
|
314
362
|
renderImagePlaceholder,
|
|
363
|
+
onImageLoadEnd,
|
|
315
364
|
itemWrapperStyle,
|
|
316
365
|
scrollX,
|
|
317
366
|
snapOffsets,
|
|
@@ -320,6 +369,7 @@ const ImageCarousel = ({
|
|
|
320
369
|
inactiveScale,
|
|
321
370
|
inactiveOpacity,
|
|
322
371
|
activeIndex,
|
|
372
|
+
style.slide,
|
|
323
373
|
],
|
|
324
374
|
);
|
|
325
375
|
|
|
@@ -341,11 +391,12 @@ const ImageCarousel = ({
|
|
|
341
391
|
decelerationRate="fast"
|
|
342
392
|
disableIntervalMomentum
|
|
343
393
|
getItemLayout={getItemLayout}
|
|
344
|
-
scrollEnabled={data.length > 1}
|
|
394
|
+
scrollEnabled={scrollEnabled && data.length > 1}
|
|
345
395
|
bounces={false}
|
|
346
396
|
scrollEventThrottle={16}
|
|
347
397
|
removeClippedSubviews={false}
|
|
348
398
|
contentOffset={{ x: initialOffset, y: 0 }}
|
|
399
|
+
style={webListSnapStyle}
|
|
349
400
|
onScroll={handleScrollEvent}
|
|
350
401
|
onScrollBeginDrag={handleScrollBeginDrag}
|
|
351
402
|
onScrollEndDrag={handleScrollEndDrag}
|
|
@@ -360,7 +411,7 @@ const ImageCarousel = ({
|
|
|
360
411
|
style={style.pageIndicators}
|
|
361
412
|
/>
|
|
362
413
|
)}
|
|
363
|
-
{showArrows && activeIndex > 0 && (
|
|
414
|
+
{showArrows && scrollEnabled && activeIndex > 0 && (
|
|
364
415
|
<TouchableOpacity
|
|
365
416
|
style={style.leftArrow}
|
|
366
417
|
onPress={handlePrevArrowPress}
|
|
@@ -368,7 +419,7 @@ const ImageCarousel = ({
|
|
|
368
419
|
<Icon name="left-arrow" style={style.arrowIcon} />
|
|
369
420
|
</TouchableOpacity>
|
|
370
421
|
)}
|
|
371
|
-
{showArrows && activeIndex < data.length - 1 && (
|
|
422
|
+
{showArrows && scrollEnabled && activeIndex < data.length - 1 && (
|
|
372
423
|
<TouchableOpacity
|
|
373
424
|
style={style.rightArrow}
|
|
374
425
|
onPress={handleNextArrowPress}
|
|
@@ -392,9 +443,11 @@ ImageCarousel.propTypes = {
|
|
|
392
443
|
maxPageIndicators: PropTypes.number,
|
|
393
444
|
renderImagePlaceholder: PropTypes.func,
|
|
394
445
|
renderOverlay: PropTypes.func,
|
|
446
|
+
scrollEnabled: PropTypes.bool,
|
|
395
447
|
showArrows: PropTypes.bool,
|
|
396
448
|
showPageIndicators: PropTypes.bool,
|
|
397
449
|
style: PropTypes.object,
|
|
450
|
+
onImageLoadEnd: PropTypes.func,
|
|
398
451
|
onIndexChange: PropTypes.func,
|
|
399
452
|
};
|
|
400
453
|
|
|
@@ -17,6 +17,8 @@ import { View } from '../View';
|
|
|
17
17
|
* rendered on top of the image.
|
|
18
18
|
* @param {Function} [renderImagePlaceholder] `(item, { index }) => JSX.Element`
|
|
19
19
|
* rendered over the image until it finishes loading.
|
|
20
|
+
* @param {Function} [onImageLoadEnd] `(item, { index }) => void`, called once
|
|
21
|
+
* the image finishes loading, whether it succeeded or failed.
|
|
20
22
|
* @param {Object} style Resolved styles, injected by connectStyle.
|
|
21
23
|
* @returns {JSX.Element}
|
|
22
24
|
*/
|
|
@@ -27,13 +29,18 @@ const ImageCarouselSlide = ({
|
|
|
27
29
|
source,
|
|
28
30
|
renderOverlay,
|
|
29
31
|
renderImagePlaceholder,
|
|
32
|
+
onImageLoadEnd,
|
|
30
33
|
style = {},
|
|
31
34
|
}) => {
|
|
32
35
|
const [isImageLoaded, setImageLoaded] = useState(false);
|
|
33
36
|
|
|
34
37
|
const handleImageLoadEnd = useCallback(() => {
|
|
35
38
|
setImageLoaded(true);
|
|
36
|
-
|
|
39
|
+
|
|
40
|
+
if (onImageLoadEnd) {
|
|
41
|
+
onImageLoadEnd(item, { index });
|
|
42
|
+
}
|
|
43
|
+
}, [onImageLoadEnd, item, index]);
|
|
37
44
|
|
|
38
45
|
const placeholder =
|
|
39
46
|
!isImageLoaded && renderImagePlaceholder
|
|
@@ -65,6 +72,7 @@ ImageCarouselSlide.propTypes = {
|
|
|
65
72
|
renderOverlay: PropTypes.func,
|
|
66
73
|
source: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
|
|
67
74
|
style: PropTypes.object,
|
|
75
|
+
onImageLoadEnd: PropTypes.func,
|
|
68
76
|
};
|
|
69
77
|
|
|
70
78
|
const StyledImageCarouselSlide = connectStyle('shoutem.ui.ImageCarouselSlide')(
|
package/package.json
CHANGED
package/theme.js
CHANGED
|
@@ -2169,6 +2169,9 @@ export default () => {
|
|
|
2169
2169
|
backgroundColor: resolveVariable('paperColor'),
|
|
2170
2170
|
},
|
|
2171
2171
|
listContent: {},
|
|
2172
|
+
// Passed to every `shoutem.ui.ImageCarouselSlide` and merged over its own
|
|
2173
|
+
// theme (e.g. `slide.card`), so a carousel's style can restyle its slides.
|
|
2174
|
+
slide: {},
|
|
2172
2175
|
// Geometry / animation scalars read by the component, not applied as styles.
|
|
2173
2176
|
cardWidth: responsiveWidth(236),
|
|
2174
2177
|
cardSpacing: responsiveWidth(10),
|