@momo-kits/tab-view 0.169.3 → 0.169.4-beta.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/tab-view",
3
- "version": "0.169.3",
3
+ "version": "0.169.4-beta.1",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "peerDependencies": {
package/styles.ts CHANGED
@@ -68,7 +68,18 @@ export default StyleSheet.create({
68
68
  borderTopLeftRadius: Spacing.M,
69
69
  borderTopRightRadius: Spacing.M,
70
70
  },
71
+ leftPath: {
72
+ position: 'absolute',
73
+ top: 0,
74
+ right: '100%',
75
+ },
76
+ rightPath: {
77
+ position: 'absolute',
78
+ top: 0,
79
+ left: '100%',
80
+ },
71
81
  overlayTextWrapper: {
82
+ width: '100%',
72
83
  height: '100%',
73
84
  alignItems: 'center',
74
85
  justifyContent: 'center',
@@ -15,6 +15,15 @@ import {
15
15
  import CardTabItem from '../tabItem/CardTabItem';
16
16
  import PathSvg from '../assets/Path';
17
17
 
18
+ type PercentValue = `${number}%`;
19
+
20
+ /** Mirrors the curve drawn for the right side so it fits the left side. */
21
+ const FLIP = { rotateY: '180deg' } as const;
22
+
23
+ /**
24
+ * Card-style tab bar with a raised selected card; widths are percentages and only the
25
+ * left offset is measured, because measured lengths are already scaled.
26
+ */
18
27
  const CardTabBar: FC<TabBarProps> = ({
19
28
  tabs,
20
29
  onPressTabItem,
@@ -26,20 +35,21 @@ const CardTabBar: FC<TabBarProps> = ({
26
35
  }) => {
27
36
  const {theme} = useContext(ApplicationContext);
28
37
  const itemWidth = containerWidth / tabs.length;
38
+ const itemPercent: `${number}%` = `${100 / tabs.length}%`;
29
39
  const isFirst = selectedIndex === 0;
30
40
  const isLast = selectedIndex === tabs.length - 1;
31
41
  const pathWidth = tabs.length > 2 ? 22 : 28;
32
- const backSpace = isFirst ? 0 : pathWidth;
33
- const isWeb = Platform.OS === 'web';
34
42
  const overlayLeft = itemWidth * selectedIndex;
35
- const pathInset = isWeb ? 8 - pathWidth : 8;
36
- const pathPosition = isWeb ? { position: 'absolute' as const, top: 0 } : {};
37
- const leftPathStyle = {
38
- ...pathPosition,
39
- left: pathInset,
40
- transform: [{ rotateY: '180deg' }],
41
- };
42
- const rightPathStyle = { ...pathPosition, right: pathInset };
43
+ // tuck each curve in by the corner radius, as a % of its own width so it scales with it
44
+ const pathShift = (Radius.M / pathWidth) * 100;
45
+ const leftPathStyle = [
46
+ styles.leftPath,
47
+ { transform: [{ translateX: `${pathShift}%` as PercentValue }, FLIP] },
48
+ ];
49
+ const rightPathStyle = [
50
+ styles.rightPath,
51
+ { transform: [{ translateX: `${-pathShift}%` as PercentValue }] },
52
+ ];
43
53
 
44
54
  const renderIconTabBar = () => {
45
55
  return (
@@ -77,7 +87,7 @@ const CardTabBar: FC<TabBarProps> = ({
77
87
  onPressTabItem={() => {
78
88
  onPressTabItem?.(index);
79
89
  }}
80
- width={itemWidth}
90
+ width={itemPercent}
81
91
  selectedColor={selectedColor}
82
92
  unselectedColor={unselectedColor}
83
93
  />
@@ -88,11 +98,12 @@ const CardTabBar: FC<TabBarProps> = ({
88
98
  styles.cardTabOverlay,
89
99
  Platform.OS === 'ios' && Shadow.Light,
90
100
  {
91
- width: containerWidth / tabs.length,
92
- left: isWeb ? overlayLeft : overlayLeft - backSpace,
101
+ width: itemPercent,
102
+ left: overlayLeft,
93
103
  },
94
104
  ]}>
95
105
  {!isFirst && <PathSvg style={leftPathStyle} pathWidth={pathWidth} />}
106
+ {!isLast && <PathSvg style={rightPathStyle} pathWidth={pathWidth} />}
96
107
  <View
97
108
  accessibilityLabel={
98
109
  accessibilityLabelForSelectedCard ??
@@ -102,7 +113,6 @@ const CardTabBar: FC<TabBarProps> = ({
102
113
  styles.overlayTextWrapper,
103
114
  {
104
115
  backgroundColor: theme.colors.background.surface,
105
- width: containerWidth / tabs.length,
106
116
  borderTopRightRadius: Radius.M,
107
117
  borderTopLeftRadius: Radius.M,
108
118
  paddingHorizontal: tabs.length === 3 ? Spacing.S : Spacing.M,
@@ -133,7 +143,6 @@ const CardTabBar: FC<TabBarProps> = ({
133
143
  </View>
134
144
  )}
135
145
  </View>
136
- {!isLast && <PathSvg style={rightPathStyle} pathWidth={pathWidth} />}
137
146
  </View>
138
147
  </View>
139
148
  );
@@ -13,6 +13,10 @@ import styles from '../styles';
13
13
  import { ApplicationContext } from '@momo-kits/foundation';
14
14
  import TabItem from '../tabItem/TabItem';
15
15
 
16
+ /**
17
+ * Scrollable tab bar; the indicator is placed with left/right because measured lengths
18
+ * are already scaled and `width` would scale them again.
19
+ */
16
20
  const ScrollableTabBar: FC<TabBarProps> = ({
17
21
  tabs = [],
18
22
  onPressTabItem,
@@ -25,7 +29,8 @@ const ScrollableTabBar: FC<TabBarProps> = ({
25
29
  const [itemWidthMap, setItemWidthMap] = useState<
26
30
  { width: number; x: number }[]
27
31
  >([]);
28
- const animatedWidth = useRef(new Animated.Value(0)).current;
32
+ const [contentWidth, setContentWidth] = useState(0);
33
+ const animRight = useRef(new Animated.Value(0)).current;
29
34
  const animX = useRef(new Animated.Value(0)).current;
30
35
 
31
36
  const scrollViewRef = useRef<ScrollView>(null);
@@ -95,11 +100,12 @@ const ScrollableTabBar: FC<TabBarProps> = ({
95
100
  );
96
101
 
97
102
  const moveIndicator = useCallback(() => {
103
+ if (contentWidth === 0) return;
98
104
  const w = itemWidthMap[selectedIndex]?.width ?? 0;
99
105
  const x = itemWidthMap[selectedIndex]?.x ?? 0;
100
106
  Animated.parallel([
101
- Animated.timing(animatedWidth, {
102
- toValue: w,
107
+ Animated.timing(animRight, {
108
+ toValue: contentWidth - x - w,
103
109
  duration: 250,
104
110
  useNativeDriver: false,
105
111
  }),
@@ -109,7 +115,7 @@ const ScrollableTabBar: FC<TabBarProps> = ({
109
115
  useNativeDriver: false,
110
116
  }),
111
117
  ]).start();
112
- }, [animatedWidth, animX, itemWidthMap, selectedIndex]);
118
+ }, [animRight, animX, contentWidth, itemWidthMap, selectedIndex]);
113
119
 
114
120
  const onLayout = useCallback(
115
121
  (e: LayoutChangeEvent, index: number) => {
@@ -119,6 +125,15 @@ const ScrollableTabBar: FC<TabBarProps> = ({
119
125
  [setMeasured],
120
126
  );
121
127
 
128
+ const handleContentLayout = useCallback(
129
+ (e: LayoutChangeEvent) => {
130
+ const { width } = e.nativeEvent.layout;
131
+ if (contentWidth === 0) animRight.setValue(width);
132
+ setContentWidth(width);
133
+ },
134
+ [animRight, contentWidth],
135
+ );
136
+
122
137
  const scrollToIndex = useCallback(
123
138
  (index: number) => {
124
139
  const targetIndex = Math.max(0, index - 1);
@@ -198,18 +213,21 @@ const ScrollableTabBar: FC<TabBarProps> = ({
198
213
  ref={contentRef}
199
214
  collapsable={false}
200
215
  style={styles.relativeRow}
216
+ onLayout={handleContentLayout}
201
217
  >
202
218
  {tabs.map((item, index) => renderItem({ item, index }))}
203
- <Animated.View
204
- style={[
205
- styles.indicator,
206
- {
207
- left: animX,
208
- width: animatedWidth,
209
- backgroundColor: selectedColor,
210
- },
211
- ]}
212
- />
219
+ {contentWidth > 0 && (
220
+ <Animated.View
221
+ style={[
222
+ styles.indicator,
223
+ {
224
+ left: animX,
225
+ right: animRight,
226
+ backgroundColor: selectedColor,
227
+ },
228
+ ]}
229
+ />
230
+ )}
213
231
  </View>
214
232
  </ScrollView>
215
233
  );
package/tabBar/TabBar.tsx CHANGED
@@ -5,6 +5,10 @@ import TabItem from '../tabItem/TabItem';
5
5
  import styles from '../styles';
6
6
  import {ApplicationContext, Spacing} from '@momo-kits/foundation';
7
7
 
8
+ /**
9
+ * Fixed-width tab bar; widths are percentages and the indicator uses left/right because
10
+ * measured lengths are already scaled and `width` would scale them again.
11
+ */
8
12
  const TabBar: FC<TabBarProps> = ({
9
13
  tabs = [],
10
14
  selectedIndex,
@@ -17,6 +21,7 @@ const TabBar: FC<TabBarProps> = ({
17
21
  }) => {
18
22
  const {theme} = useContext(ApplicationContext);
19
23
  const itemWidth = containerWidth / tabs.length;
24
+ const itemPercent: `${number}%` = `${100 / tabs.length}%`;
20
25
 
21
26
  return (
22
27
  <View
@@ -31,7 +36,7 @@ const TabBar: FC<TabBarProps> = ({
31
36
  return (
32
37
  <TabItem
33
38
  key={`Tab View Item ${tab.title}-${index}`}
34
- width={itemWidth}
39
+ width={itemPercent}
35
40
  onPressTabItem={() => {
36
41
  onPressTabItem?.(index);
37
42
  }}
@@ -55,7 +60,14 @@ const TabBar: FC<TabBarProps> = ({
55
60
  ],
56
61
  extrapolate: 'clamp',
57
62
  }),
58
- width: itemWidth - Spacing.XS * 2,
63
+ right: scrollX.interpolate({
64
+ inputRange: [0, tabs.length - 1],
65
+ outputRange: [
66
+ (tabs.length - 1) * itemWidth + Spacing.XS,
67
+ Spacing.XS,
68
+ ],
69
+ extrapolate: 'clamp',
70
+ }),
59
71
  backgroundColor: selectedColor,
60
72
  },
61
73
  ]}
@@ -12,6 +12,10 @@ import {
12
12
  Text,
13
13
  } from '@momo-kits/foundation';
14
14
 
15
+ /**
16
+ * Card-style tab item; `width` sits on the wrapper because a percentage only resolves
17
+ * on a direct child of the row.
18
+ */
15
19
  const CardTabItem: FC<TabItemProps> = ({
16
20
  tab,
17
21
  active,
@@ -70,15 +74,10 @@ const CardTabItem: FC<TabItemProps> = ({
70
74
  };
71
75
 
72
76
  return (
73
- <View style={styles.cardTabItemWrapper}>
77
+ <View style={[styles.cardTabItemWrapper, { width }]}>
74
78
  <TouchableOpacity
75
79
  onPress={onPressTabItem}
76
- style={[
77
- styles.cardTabItem,
78
- {
79
- width,
80
- },
81
- ]}
80
+ style={styles.cardTabItem}
82
81
  accessibilityLabel={componentId}
83
82
  accessibilityState={{ checked: active }}
84
83
  >
package/types.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import {ReactElement} from 'react';
2
2
  import {PagerViewProps} from 'react-native-pager-view';
3
- import {Animated, ViewStyle} from 'react-native';
3
+ import {Animated, DimensionValue, ViewStyle} from 'react-native';
4
4
 
5
5
  export type Tab = {
6
6
  /**
@@ -54,7 +54,7 @@ export type TabItemProps = {
54
54
  /**
55
55
  * Optional. Specifies the width of the tab item. Useful for customizing layout and appearance.
56
56
  */
57
- width?: number;
57
+ width?: DimensionValue;
58
58
 
59
59
  /**
60
60
  * Optional. Specifies the layout direction for the tab's content, either in a row or column format.