@proximus/lavender-carousel-native 0.1.0-alpha.10
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/dist/AppleSeed.d.ts +6 -0
- package/dist/AppleSeed.d.ts.map +1 -0
- package/dist/AppleSeed.js +81 -0
- package/dist/Carousel.d.ts +11 -0
- package/dist/Carousel.d.ts.map +1 -0
- package/dist/Carousel.js +62 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/package.json +14 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AppleSeed.d.ts","sourceRoot":"","sources":["../src/AppleSeed.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4B,MAAM,OAAO,CAAC;AASjD,wBAAgB,WAAW,CAAC,EAC1B,aAAa,EACb,iBAAiB,GAClB,EAAE;IACD,aAAa,EAAE,MAAM,CAAC;IACtB,iBAAiB,EAAE,MAAM,CAAC;CAC3B,qBAiGA"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
|
+
import { Animated, View } from 'react-native';
|
|
3
|
+
import { useTheme } from '@proximus/lavender-styling-native';
|
|
4
|
+
const appleSeedSizeL = 14;
|
|
5
|
+
const appleSeedSizeM = 10;
|
|
6
|
+
const appleSeedSizeS = 6;
|
|
7
|
+
const appleSeedSizeXS = 4;
|
|
8
|
+
export function PxAppleSeed({ numberOfItems, selectedItemIndex, }) {
|
|
9
|
+
const { theme } = useTheme();
|
|
10
|
+
const tx = useRef(new Animated.Value(0)).current;
|
|
11
|
+
const prevSelectedItemIndex = useRef(selectedItemIndex);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if ((selectedItemIndex <= 2 && prevSelectedItemIndex.current != 3) ||
|
|
14
|
+
numberOfItems - selectedItemIndex < 3) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const translateLeft = selectedItemIndex > prevSelectedItemIndex.current ? -1 : 1;
|
|
18
|
+
const toValue = translateLeft
|
|
19
|
+
? -1 * (selectedItemIndex - 2) * theme.t('SizeIconS').number
|
|
20
|
+
: (selectedItemIndex - 3) * theme.t('SizeIconS').number;
|
|
21
|
+
Animated.spring(tx, {
|
|
22
|
+
toValue,
|
|
23
|
+
useNativeDriver: true,
|
|
24
|
+
}).start();
|
|
25
|
+
prevSelectedItemIndex.current = selectedItemIndex;
|
|
26
|
+
}, [numberOfItems, selectedItemIndex, theme, tx]);
|
|
27
|
+
return (React.createElement(View, { style: {
|
|
28
|
+
flexDirection: 'row',
|
|
29
|
+
width: 5 * theme.t('SizeIconS').number,
|
|
30
|
+
justifyContent: numberOfItems < 5 ? 'center' : 'flex-start',
|
|
31
|
+
alignSelf: 'center',
|
|
32
|
+
overflow: 'hidden',
|
|
33
|
+
alignItems: 'center',
|
|
34
|
+
} }, Array.from({ length: numberOfItems }).map((_, index) => (React.createElement(Animated.View, { key: index, style: {
|
|
35
|
+
width: theme.t('SizeIconS').number,
|
|
36
|
+
height: theme.t('SizeIconS').number,
|
|
37
|
+
alignItems: 'center',
|
|
38
|
+
justifyContent: 'center',
|
|
39
|
+
transform: [
|
|
40
|
+
{
|
|
41
|
+
translateX: tx,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
} },
|
|
45
|
+
React.createElement(View, { key: index, style: {
|
|
46
|
+
borderWidth: theme.t('SizeBorderS').number,
|
|
47
|
+
height: numberOfItems > 5 ? appleSeedSizeXS : appleSeedSizeM,
|
|
48
|
+
width: numberOfItems > 5 ? appleSeedSizeXS : appleSeedSizeM,
|
|
49
|
+
borderRadius: theme.t('RadiusMain').number,
|
|
50
|
+
borderColor: theme.t('ColorBackgroundStateActiveDefault'),
|
|
51
|
+
...(index === numberOfItems - 2 &&
|
|
52
|
+
numberOfItems > 6 && {
|
|
53
|
+
height: appleSeedSizeM,
|
|
54
|
+
width: appleSeedSizeM,
|
|
55
|
+
}),
|
|
56
|
+
...(index === numberOfItems - 3 &&
|
|
57
|
+
numberOfItems > 5 && {
|
|
58
|
+
height: appleSeedSizeS,
|
|
59
|
+
width: appleSeedSizeS,
|
|
60
|
+
}),
|
|
61
|
+
...((index === selectedItemIndex - 2 ||
|
|
62
|
+
index === selectedItemIndex + 2) &&
|
|
63
|
+
numberOfItems > 5 && {
|
|
64
|
+
height: appleSeedSizeS,
|
|
65
|
+
width: appleSeedSizeS,
|
|
66
|
+
}),
|
|
67
|
+
...((index === selectedItemIndex - 1 ||
|
|
68
|
+
index === selectedItemIndex + 1) &&
|
|
69
|
+
numberOfItems > 5 && {
|
|
70
|
+
height: appleSeedSizeM,
|
|
71
|
+
width: appleSeedSizeM,
|
|
72
|
+
}),
|
|
73
|
+
...(index === selectedItemIndex && {
|
|
74
|
+
backgroundColor: theme.t('ColorBackgroundStateActiveDefault'),
|
|
75
|
+
...(numberOfItems > 5 && {
|
|
76
|
+
height: appleSeedSizeL,
|
|
77
|
+
width: appleSeedSizeL,
|
|
78
|
+
}),
|
|
79
|
+
}),
|
|
80
|
+
} }))))));
|
|
81
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { AccessibilityProps, StyleProp, ViewStyle } from 'react-native';
|
|
3
|
+
export declare function PxCarousel({ numberOfItemsVisible, children, withAppleSeed, containerStyle, testID, accessibilityProps, }: {
|
|
4
|
+
numberOfItemsVisible?: number;
|
|
5
|
+
children?: React.ReactNode;
|
|
6
|
+
withAppleSeed?: boolean;
|
|
7
|
+
containerStyle?: StyleProp<ViewStyle>;
|
|
8
|
+
testID?: string;
|
|
9
|
+
accessibilityProps?: AccessibilityProps;
|
|
10
|
+
}): React.JSX.Element;
|
|
11
|
+
//# sourceMappingURL=Carousel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Carousel.d.ts","sourceRoot":"","sources":["../src/Carousel.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA8B,MAAM,OAAO,CAAC;AACnD,OAAO,EACL,kBAAkB,EAGlB,SAAS,EAET,SAAS,EACV,MAAM,cAAc,CAAC;AAOtB,wBAAgB,UAAU,CAAC,EACzB,oBAAwB,EACxB,QAAQ,EACR,aAAoB,EACpB,cAAc,EACd,MAAM,EACN,kBAAkB,GACnB,EAAE;IACD,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,cAAc,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;IACtC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;CACzC,qBAmGA"}
|
package/dist/Carousel.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import React, { useEffect, useState } from 'react';
|
|
2
|
+
import { Dimensions, FlatList, View, } from 'react-native';
|
|
3
|
+
import { useTheme } from '@proximus/lavender-styling-native';
|
|
4
|
+
import { PxAppleSeed } from './AppleSeed';
|
|
5
|
+
import { a11y, e2eID } from '@proximus/lavender-common-native';
|
|
6
|
+
const inset = 24;
|
|
7
|
+
export function PxCarousel({ numberOfItemsVisible = 1, children, withAppleSeed = true, containerStyle, testID, accessibilityProps, }) {
|
|
8
|
+
const [width, setWidth] = useState(0);
|
|
9
|
+
const [computedGap, setComputedGap] = useState(0);
|
|
10
|
+
const [elementsCount, setElementsCount] = useState(0);
|
|
11
|
+
const [selectedItemIndex, setSelectedItemIndex] = useState(0);
|
|
12
|
+
const [offsets, setOffsets] = useState([]);
|
|
13
|
+
const { theme } = useTheme();
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
setWidth((Dimensions.get('window').width -
|
|
16
|
+
numberOfItemsVisible * computedGap -
|
|
17
|
+
2 * inset) /
|
|
18
|
+
numberOfItemsVisible);
|
|
19
|
+
}, [numberOfItemsVisible, computedGap, theme]);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
setElementsCount(React.Children.count(children));
|
|
22
|
+
}, [children]);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
setComputedGap(theme.t(`SpacingSMobile`).number);
|
|
25
|
+
}, [theme]);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
setOffsets(new Array(elementsCount).fill(0).reduce((prev, _, i) => {
|
|
28
|
+
return [...prev, prev[i] + width + computedGap];
|
|
29
|
+
}, [width]));
|
|
30
|
+
}, [computedGap, elementsCount, width]);
|
|
31
|
+
return (React.createElement(View, { style: [containerStyle, { flexDirection: 'column', gap: 27.15, flex: 1 }], ...e2eID(testID), ...a11y(accessibilityProps) },
|
|
32
|
+
React.createElement(FlatList, { data: new Array(React.Children.count(children)).fill(0), renderItem: ({ index }) => (React.createElement(View, { style: { width } }, [...children]?.[index])), contentContainerStyle: {
|
|
33
|
+
gap: computedGap,
|
|
34
|
+
alignItems: 'stretch',
|
|
35
|
+
paddingLeft: computedGap,
|
|
36
|
+
}, horizontal: true, snapToOffsets: offsets, decelerationRate: "fast", snapToAlignment: numberOfItemsVisible === 1 ? 'center' : 'start', showsHorizontalScrollIndicator: false, ...(children?.length === 2 && {
|
|
37
|
+
onEndReached: () => {
|
|
38
|
+
setSelectedItemIndex(1);
|
|
39
|
+
},
|
|
40
|
+
onStartReached: () => {
|
|
41
|
+
setSelectedItemIndex(0);
|
|
42
|
+
},
|
|
43
|
+
}), onViewableItemsChanged: (event) => {
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
if (children?.length === 2) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (event.viewableItems.length === 2 &&
|
|
49
|
+
event.viewableItems?.[0]?.index === 0) {
|
|
50
|
+
setSelectedItemIndex(0);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
// when keeping swiping left on ios on the last element,
|
|
54
|
+
// viewableItems contains only one element so we ignore that case
|
|
55
|
+
// because vent.viewableItems?.[1] is out of bounds
|
|
56
|
+
if (event.viewableItems.length === 1) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
setSelectedItemIndex(event.viewableItems?.[1]?.index ?? 0);
|
|
60
|
+
} }),
|
|
61
|
+
withAppleSeed && elementsCount > 1 && (React.createElement(PxAppleSeed, { numberOfItems: React.Children.count(children), selectedItemIndex: selectedItemIndex }))));
|
|
62
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@proximus/lavender-carousel-native",
|
|
3
|
+
"version": "0.1.0-alpha.10",
|
|
4
|
+
"description": "Carousel component",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"@proximus/lavender-common-native": "*",
|
|
9
|
+
"@proximus/lavender-styling-native": "*"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
}
|
|
14
|
+
}
|