@shoutem/ui 9.3.0 → 9.3.1-rc.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.
- package/components/ImageCarousel/ImageCarousel.js +50 -16
- package/package.json +1 -1
- package/theme.js +3 -0
|
@@ -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.
|
|
@@ -78,8 +100,11 @@ const defaultGetImageSource = item => item.source;
|
|
|
78
100
|
* slide changes (not on mount).
|
|
79
101
|
* @param {Object} style Resolved styles, injected by connectStyle. Geometry is
|
|
80
102
|
* read from the `cardWidth`, `cardSpacing`, `inactiveScale` and
|
|
81
|
-
* `inactiveOpacity` scalars.
|
|
82
|
-
* over
|
|
103
|
+
* `inactiveOpacity` scalars. `style.slide` is passed to every
|
|
104
|
+
* `ImageCarouselSlide` and merged over that component's theme, so callers
|
|
105
|
+
* can restyle the slide card (e.g. `slide.card` background, corner radius,
|
|
106
|
+
* border) from the carousel's style. A `style` prop passed by the caller is
|
|
107
|
+
* merged over the theme styles by connectStyle.
|
|
83
108
|
* @returns {JSX.Element|null}
|
|
84
109
|
*/
|
|
85
110
|
const ImageCarousel = ({
|
|
@@ -133,6 +158,7 @@ const ImageCarousel = ({
|
|
|
133
158
|
width: cardWidth,
|
|
134
159
|
marginHorizontal: cardSpacing / 2,
|
|
135
160
|
aspectRatio: cardAspectRatio,
|
|
161
|
+
...webSlotSnapStyle,
|
|
136
162
|
}),
|
|
137
163
|
[cardWidth, cardSpacing, cardAspectRatio],
|
|
138
164
|
);
|
|
@@ -295,17 +321,23 @@ const ImageCarousel = ({
|
|
|
295
321
|
],
|
|
296
322
|
};
|
|
297
323
|
|
|
324
|
+
// The slot stays untransformed so its layout box (which web scroll
|
|
325
|
+
// snapping aligns to) is identical for every slide; only the inner view
|
|
326
|
+
// scales and shifts.
|
|
298
327
|
return (
|
|
299
|
-
<
|
|
300
|
-
<
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
328
|
+
<RNView style={itemWrapperStyle}>
|
|
329
|
+
<Animated.View style={[slideFillStyle, animatedStyle]}>
|
|
330
|
+
<ImageCarouselSlide
|
|
331
|
+
item={item}
|
|
332
|
+
index={index}
|
|
333
|
+
isActive={index === activeIndex}
|
|
334
|
+
source={getImageSource(item)}
|
|
335
|
+
renderOverlay={renderOverlay}
|
|
336
|
+
renderImagePlaceholder={renderImagePlaceholder}
|
|
337
|
+
style={style.slide}
|
|
338
|
+
/>
|
|
339
|
+
</Animated.View>
|
|
340
|
+
</RNView>
|
|
309
341
|
);
|
|
310
342
|
},
|
|
311
343
|
[
|
|
@@ -320,6 +352,7 @@ const ImageCarousel = ({
|
|
|
320
352
|
inactiveScale,
|
|
321
353
|
inactiveOpacity,
|
|
322
354
|
activeIndex,
|
|
355
|
+
style.slide,
|
|
323
356
|
],
|
|
324
357
|
);
|
|
325
358
|
|
|
@@ -346,6 +379,7 @@ const ImageCarousel = ({
|
|
|
346
379
|
scrollEventThrottle={16}
|
|
347
380
|
removeClippedSubviews={false}
|
|
348
381
|
contentOffset={{ x: initialOffset, y: 0 }}
|
|
382
|
+
style={webListSnapStyle}
|
|
349
383
|
onScroll={handleScrollEvent}
|
|
350
384
|
onScrollBeginDrag={handleScrollBeginDrag}
|
|
351
385
|
onScrollEndDrag={handleScrollEndDrag}
|
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),
|