@momo-kits/tab-view 0.169.4-beta.3 → 0.169.5

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/assets/Path.tsx CHANGED
@@ -1,12 +1,13 @@
1
- import React, {FC} from 'react';
2
- import {ViewProps} from 'react-native';
3
- import Svg, {Path} from 'react-native-svg';
1
+ import React, { FC } from 'react';
2
+ import { ViewProps } from 'react-native';
3
+ import Svg, { Path } from 'react-native-svg';
4
4
 
5
5
  interface PathSvgProps extends ViewProps {
6
6
  pathWidth: number;
7
7
  }
8
8
 
9
- const PathSvg: FC<PathSvgProps> = ({style, pathWidth}) => {
9
+ /** Curved edge linking the raised card tab to the bar, `pathWidth` wide. */
10
+ const PathSvg: FC<PathSvgProps> = ({ style, pathWidth }) => {
10
11
  const height = 44;
11
12
 
12
13
  return (
@@ -16,7 +17,8 @@ const PathSvg: FC<PathSvgProps> = ({style, pathWidth}) => {
16
17
  viewBox={`0 0 28 ${height}`}
17
18
  preserveAspectRatio="none"
18
19
  fill="none"
19
- style={style}>
20
+ style={style}
21
+ >
20
22
  <Path
21
23
  d="M0 0H0.60765C6.47582 0 11.4819 4.23831 12.4602 10.0244C13.9248 18.6872 15.9294 30.37 16.6925 34C17.9536 40 22.259 44 27.9996 44H0V0Z"
22
24
  fill="white"
package/index.tsx CHANGED
@@ -67,7 +67,7 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
67
67
 
68
68
  const _onPressTabItem = (index: number) => {
69
69
  onPressTabItem?.(index);
70
-
70
+
71
71
  if (!fitContent) {
72
72
  pagerRef.current?.setPage(index);
73
73
  }
@@ -159,9 +159,7 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
159
159
  </Animated.View>
160
160
 
161
161
  {fitContent ? (
162
- <View>
163
- {tabs[selectedIndex]?.component}
164
- </View>
162
+ <View>{tabs[selectedIndex]?.component}</View>
165
163
  ) : (
166
164
  <PagerView
167
165
  {...pagerProps}
@@ -183,6 +181,10 @@ const TabComponent: ForwardRefRenderFunction<TabViewRef, TabViewProps> = (
183
181
  );
184
182
  };
185
183
 
184
+ /**
185
+ * Tab bar (default, scrollable or card) over pages mounted on first visit.
186
+ * `fitContent` swaps the PagerView for the selected tab only, sized to content.
187
+ */
186
188
  const TabView = forwardRef(TabComponent);
187
189
 
188
190
  export { CardTabBar, ScrollableTabBar, TabBar, TabView };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/tab-view",
3
- "version": "0.169.4-beta.3",
3
+ "version": "0.169.5",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "peerDependencies": {
package/styles.ts CHANGED
@@ -68,18 +68,7 @@ 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
- },
81
71
  overlayTextWrapper: {
82
- width: '100%',
83
72
  height: '100%',
84
73
  alignItems: 'center',
85
74
  justifyContent: 'center',
@@ -1,6 +1,6 @@
1
- import React, {FC, useContext} from 'react';
2
- import {Platform, View} from 'react-native';
3
- import {TabBarProps} from '../types';
1
+ import React, { FC, useContext } from 'react';
2
+ import { Platform, View } from 'react-native';
3
+ import { TabBarProps } from '../types';
4
4
  import styles from '../styles';
5
5
  import {
6
6
  ApplicationContext,
@@ -15,14 +15,9 @@ 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
18
  /**
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.
19
+ * Card-style tabs: the selected tab is redrawn as a raised card on an overlay,
20
+ * joined to its neighbours by curved path edges.
26
21
  */
27
22
  const CardTabBar: FC<TabBarProps> = ({
28
23
  tabs,
@@ -33,43 +28,46 @@ const CardTabBar: FC<TabBarProps> = ({
33
28
  unselectedColor,
34
29
  accessibilityLabelForSelectedCard,
35
30
  }) => {
36
- const {theme} = useContext(ApplicationContext);
31
+ const { theme } = useContext(ApplicationContext);
32
+ const selectedTab = tabs[selectedIndex];
33
+ const tabLabel = selectedTab?.accessibilityLabel;
34
+ const selectedCardLabel = accessibilityLabelForSelectedCard ?? tabLabel;
37
35
  const itemWidth = containerWidth / tabs.length;
38
- const itemPercent: `${number}%` = `${100 / tabs.length}%`;
39
36
  const isFirst = selectedIndex === 0;
40
37
  const isLast = selectedIndex === tabs.length - 1;
41
38
  const pathWidth = tabs.length > 2 ? 22 : 28;
39
+ const backSpace = isFirst ? 0 : pathWidth;
40
+ const isWeb = Platform.OS === 'web';
42
41
  const overlayLeft = itemWidth * selectedIndex;
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
- ];
42
+ const pathInset = isWeb ? 8 - pathWidth : 8;
43
+ const pathPosition = isWeb ? { position: 'absolute' as const, top: 0 } : {};
44
+ const leftPathStyle = {
45
+ ...pathPosition,
46
+ left: pathInset,
47
+ transform: [{ rotateY: '180deg' }],
48
+ };
49
+ const rightPathStyle = { ...pathPosition, right: pathInset };
53
50
 
54
51
  const renderIconTabBar = () => {
52
+ const hasRenderIcon = typeof selectedTab?.renderIcon === 'function';
55
53
  return (
56
54
  <>
57
- {!!tabs[selectedIndex]?.renderIcon &&
58
- typeof tabs[selectedIndex]?.renderIcon === 'function' && (
59
- <View
60
- style={[
61
- styles.icon,
62
- styles.iconHolder,
63
- {marginRight: Spacing.XS},
64
- ]}>
65
- {tabs[selectedIndex]?.renderIcon?.(true)}
66
- </View>
67
- )}
68
- {!tabs[selectedIndex]?.renderIcon && !!tabs[selectedIndex]?.icon && (
55
+ {hasRenderIcon && (
56
+ <View
57
+ style={[
58
+ styles.icon,
59
+ styles.iconHolder,
60
+ { marginRight: Spacing.XS },
61
+ ]}
62
+ >
63
+ {selectedTab?.renderIcon?.(true)}
64
+ </View>
65
+ )}
66
+ {!selectedTab?.renderIcon && !!selectedTab?.icon && (
69
67
  <Icon
70
68
  color={selectedColor}
71
- source={tabs[selectedIndex]?.icon as string}
72
- style={[styles.icon, {marginRight: Spacing.XS}]}
69
+ source={selectedTab?.icon as string}
70
+ style={[styles.icon, { marginRight: Spacing.XS }]}
73
71
  />
74
72
  )}
75
73
  </>
@@ -87,7 +85,7 @@ const CardTabBar: FC<TabBarProps> = ({
87
85
  onPressTabItem={() => {
88
86
  onPressTabItem?.(index);
89
87
  }}
90
- width={itemPercent}
88
+ width={itemWidth}
91
89
  selectedColor={selectedColor}
92
90
  unselectedColor={unselectedColor}
93
91
  />
@@ -98,51 +96,52 @@ const CardTabBar: FC<TabBarProps> = ({
98
96
  styles.cardTabOverlay,
99
97
  Platform.OS === 'ios' && Shadow.Light,
100
98
  {
101
- width: itemPercent,
102
- left: overlayLeft,
99
+ width: containerWidth / tabs.length,
100
+ left: isWeb ? overlayLeft : overlayLeft - backSpace,
103
101
  },
104
- ]}>
102
+ ]}
103
+ >
105
104
  {!isFirst && <PathSvg style={leftPathStyle} pathWidth={pathWidth} />}
106
- {!isLast && <PathSvg style={rightPathStyle} pathWidth={pathWidth} />}
107
105
  <View
108
- accessibilityLabel={
109
- accessibilityLabelForSelectedCard ??
110
- tabs[selectedIndex]?.accessibilityLabel
111
- }
106
+ accessibilityLabel={selectedCardLabel}
112
107
  style={[
113
108
  styles.overlayTextWrapper,
114
109
  {
115
110
  backgroundColor: theme.colors.background.surface,
111
+ width: containerWidth / tabs.length,
116
112
  borderTopRightRadius: Radius.M,
117
113
  borderTopLeftRadius: Radius.M,
118
114
  paddingHorizontal: tabs.length === 3 ? Spacing.S : Spacing.M,
119
115
  },
120
- ]}>
116
+ ]}
117
+ >
121
118
  {renderIconTabBar()}
122
119
  <Text
123
120
  color={selectedColor}
124
121
  numberOfLines={1}
125
122
  style={[styles.textCenter, styles.shrink]}
126
- typography={'header_s_semibold'}>
127
- {tabs[selectedIndex]?.title || ''}
123
+ typography={'header_s_semibold'}
124
+ >
125
+ {selectedTab?.title || ''}
128
126
  </Text>
129
- {tabs[selectedIndex]?.showDot && (
127
+ {selectedTab?.showDot && (
130
128
  <BadgeDot
131
- size={tabs[selectedIndex]?.dotSize ?? 'small'}
129
+ size={selectedTab?.dotSize ?? 'small'}
132
130
  style={{
133
131
  marginLeft: Spacing.XS,
134
132
  }}
135
133
  />
136
134
  )}
137
- {!tabs[selectedIndex]?.showDot && tabs[selectedIndex]?.badgeValue && (
135
+ {!selectedTab?.showDot && selectedTab?.badgeValue && (
138
136
  <View>
139
137
  <Badge
140
- label={tabs[selectedIndex]?.badgeValue}
141
- style={{marginLeft: Spacing.XS}}
138
+ label={selectedTab?.badgeValue}
139
+ style={{ marginLeft: Spacing.XS }}
142
140
  />
143
141
  </View>
144
142
  )}
145
143
  </View>
144
+ {!isLast && <PathSvg style={rightPathStyle} pathWidth={pathWidth} />}
146
145
  </View>
147
146
  </View>
148
147
  );
@@ -14,8 +14,8 @@ import { ApplicationContext } from '@momo-kits/foundation';
14
14
  import TabItem from '../tabItem/TabItem';
15
15
 
16
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.
17
+ * Scrollable tabs sized to their titles. Measures each item so the indicator
18
+ * animates to the selected tab and the bar scrolls it into view.
19
19
  */
20
20
  const ScrollableTabBar: FC<TabBarProps> = ({
21
21
  tabs = [],
@@ -29,8 +29,7 @@ const ScrollableTabBar: FC<TabBarProps> = ({
29
29
  const [itemWidthMap, setItemWidthMap] = useState<
30
30
  { width: number; x: number }[]
31
31
  >([]);
32
- const [contentWidth, setContentWidth] = useState(0);
33
- const animRight = useRef(new Animated.Value(0)).current;
32
+ const animatedWidth = useRef(new Animated.Value(0)).current;
34
33
  const animX = useRef(new Animated.Value(0)).current;
35
34
 
36
35
  const scrollViewRef = useRef<ScrollView>(null);
@@ -100,12 +99,11 @@ const ScrollableTabBar: FC<TabBarProps> = ({
100
99
  );
101
100
 
102
101
  const moveIndicator = useCallback(() => {
103
- if (contentWidth === 0) return;
104
102
  const w = itemWidthMap[selectedIndex]?.width ?? 0;
105
103
  const x = itemWidthMap[selectedIndex]?.x ?? 0;
106
104
  Animated.parallel([
107
- Animated.timing(animRight, {
108
- toValue: contentWidth - x - w,
105
+ Animated.timing(animatedWidth, {
106
+ toValue: w,
109
107
  duration: 250,
110
108
  useNativeDriver: false,
111
109
  }),
@@ -115,7 +113,7 @@ const ScrollableTabBar: FC<TabBarProps> = ({
115
113
  useNativeDriver: false,
116
114
  }),
117
115
  ]).start();
118
- }, [animRight, animX, contentWidth, itemWidthMap, selectedIndex]);
116
+ }, [animatedWidth, animX, itemWidthMap, selectedIndex]);
119
117
 
120
118
  const onLayout = useCallback(
121
119
  (e: LayoutChangeEvent, index: number) => {
@@ -125,15 +123,6 @@ const ScrollableTabBar: FC<TabBarProps> = ({
125
123
  [setMeasured],
126
124
  );
127
125
 
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
-
137
126
  const scrollToIndex = useCallback(
138
127
  (index: number) => {
139
128
  const targetIndex = Math.max(0, index - 1);
@@ -209,25 +198,18 @@ const ScrollableTabBar: FC<TabBarProps> = ({
209
198
  },
210
199
  ]}
211
200
  >
212
- <View
213
- ref={contentRef}
214
- collapsable={false}
215
- style={styles.relativeRow}
216
- onLayout={handleContentLayout}
217
- >
201
+ <View ref={contentRef} collapsable={false} style={styles.relativeRow}>
218
202
  {tabs.map((item, index) => renderItem({ item, index }))}
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
- )}
203
+ <Animated.View
204
+ style={[
205
+ styles.indicator,
206
+ {
207
+ left: animX,
208
+ width: animatedWidth,
209
+ backgroundColor: selectedColor,
210
+ },
211
+ ]}
212
+ />
231
213
  </View>
232
214
  </ScrollView>
233
215
  );
package/tabBar/TabBar.tsx CHANGED
@@ -1,14 +1,11 @@
1
- import React, {FC, useContext} from 'react';
2
- import {Animated, View} from 'react-native';
3
- import {TabBarProps} from '../types';
1
+ import React, { FC, useContext } from 'react';
2
+ import { Animated, View } from 'react-native';
3
+ import { TabBarProps } from '../types';
4
4
  import TabItem from '../tabItem/TabItem';
5
5
  import styles from '../styles';
6
- import {ApplicationContext, Spacing} from '@momo-kits/foundation';
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
+ /** Equal-width tabs with an underline that follows the pager via `scrollX`. */
12
9
  const TabBar: FC<TabBarProps> = ({
13
10
  tabs = [],
14
11
  selectedIndex,
@@ -19,9 +16,8 @@ const TabBar: FC<TabBarProps> = ({
19
16
  selectedColor,
20
17
  unselectedColor,
21
18
  }) => {
22
- const {theme} = useContext(ApplicationContext);
19
+ const { theme } = useContext(ApplicationContext);
23
20
  const itemWidth = containerWidth / tabs.length;
24
- const itemPercent: `${number}%` = `${100 / tabs.length}%`;
25
21
 
26
22
  return (
27
23
  <View
@@ -31,12 +27,13 @@ const TabBar: FC<TabBarProps> = ({
31
27
  borderColor: theme.colors.border.default,
32
28
  backgroundColor: theme.colors.background.surface,
33
29
  },
34
- ]}>
30
+ ]}
31
+ >
35
32
  {tabs.map((tab, index) => {
36
33
  return (
37
34
  <TabItem
38
35
  key={`Tab View Item ${tab.title}-${index}`}
39
- width={itemPercent}
36
+ width={itemWidth}
40
37
  onPressTabItem={() => {
41
38
  onPressTabItem?.(index);
42
39
  }}
@@ -60,14 +57,7 @@ const TabBar: FC<TabBarProps> = ({
60
57
  ],
61
58
  extrapolate: 'clamp',
62
59
  }),
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
- }),
60
+ width: itemWidth - Spacing.XS * 2,
71
61
  backgroundColor: selectedColor,
72
62
  },
73
63
  ]}
@@ -12,10 +12,7 @@ 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
+ /** Plain card tab cell; the selected look is drawn by the bar's overlay. */
19
16
  const CardTabItem: FC<TabItemProps> = ({
20
17
  tab,
21
18
  active,
@@ -74,10 +71,15 @@ const CardTabItem: FC<TabItemProps> = ({
74
71
  };
75
72
 
76
73
  return (
77
- <View style={[styles.cardTabItemWrapper, { width }]}>
74
+ <View style={styles.cardTabItemWrapper}>
78
75
  <TouchableOpacity
79
76
  onPress={onPressTabItem}
80
- style={styles.cardTabItem}
77
+ style={[
78
+ styles.cardTabItem,
79
+ {
80
+ width,
81
+ },
82
+ ]}
81
83
  accessibilityLabel={componentId}
82
84
  accessibilityState={{ checked: active }}
83
85
  >
@@ -14,6 +14,10 @@ import {
14
14
  } from '@momo-kits/foundation';
15
15
  import styles from '../styles';
16
16
 
17
+ /**
18
+ * One tab of the default and scrollable bars: icon beside (`direction='row'`)
19
+ * or above the title, plus a dot or badge.
20
+ */
17
21
  const TabItem: FC<TabItemProps> = ({
18
22
  tab,
19
23
  active,
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, DimensionValue, ViewStyle} from 'react-native';
3
+ import {Animated, 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?: DimensionValue;
57
+ width?: number;
58
58
 
59
59
  /**
60
60
  * Optional. Specifies the layout direction for the tab's content, either in a row or column format.