@zoverdoser/react-native-tab-view 1.0.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.
Files changed (85) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -0
  3. package/lib/module/Pager.android.js +4 -0
  4. package/lib/module/Pager.android.js.map +1 -0
  5. package/lib/module/Pager.ios.js +4 -0
  6. package/lib/module/Pager.ios.js.map +1 -0
  7. package/lib/module/Pager.js +4 -0
  8. package/lib/module/Pager.js.map +1 -0
  9. package/lib/module/PagerViewAdapter.js +131 -0
  10. package/lib/module/PagerViewAdapter.js.map +1 -0
  11. package/lib/module/PanResponderAdapter.js +209 -0
  12. package/lib/module/PanResponderAdapter.js.map +1 -0
  13. package/lib/module/PlatformPressable.js +61 -0
  14. package/lib/module/PlatformPressable.js.map +1 -0
  15. package/lib/module/SceneMap.js +24 -0
  16. package/lib/module/SceneMap.js.map +1 -0
  17. package/lib/module/SceneView.js +72 -0
  18. package/lib/module/SceneView.js.map +1 -0
  19. package/lib/module/TabBar.js +470 -0
  20. package/lib/module/TabBar.js.map +1 -0
  21. package/lib/module/TabBarIndicator.js +122 -0
  22. package/lib/module/TabBarIndicator.js.map +1 -0
  23. package/lib/module/TabBarItem.js +209 -0
  24. package/lib/module/TabBarItem.js.map +1 -0
  25. package/lib/module/TabBarItemLabel.js +34 -0
  26. package/lib/module/TabBarItemLabel.js.map +1 -0
  27. package/lib/module/TabView.js +141 -0
  28. package/lib/module/TabView.js.map +1 -0
  29. package/lib/module/index.js +8 -0
  30. package/lib/module/index.js.map +1 -0
  31. package/lib/module/package.json +1 -0
  32. package/lib/module/types.js +4 -0
  33. package/lib/module/types.js.map +1 -0
  34. package/lib/module/useAnimatedValue.js +12 -0
  35. package/lib/module/useAnimatedValue.js.map +1 -0
  36. package/lib/typescript/package.json +1 -0
  37. package/lib/typescript/src/Pager.android.d.ts +2 -0
  38. package/lib/typescript/src/Pager.android.d.ts.map +1 -0
  39. package/lib/typescript/src/Pager.d.ts +2 -0
  40. package/lib/typescript/src/Pager.d.ts.map +1 -0
  41. package/lib/typescript/src/Pager.ios.d.ts +2 -0
  42. package/lib/typescript/src/Pager.ios.d.ts.map +1 -0
  43. package/lib/typescript/src/PagerViewAdapter.d.ts +18 -0
  44. package/lib/typescript/src/PagerViewAdapter.d.ts.map +1 -0
  45. package/lib/typescript/src/PanResponderAdapter.d.ts +19 -0
  46. package/lib/typescript/src/PanResponderAdapter.d.ts.map +1 -0
  47. package/lib/typescript/src/PlatformPressable.d.ts +14 -0
  48. package/lib/typescript/src/PlatformPressable.d.ts.map +1 -0
  49. package/lib/typescript/src/SceneMap.d.ts +10 -0
  50. package/lib/typescript/src/SceneMap.d.ts.map +1 -0
  51. package/lib/typescript/src/SceneView.d.ts +16 -0
  52. package/lib/typescript/src/SceneView.d.ts.map +1 -0
  53. package/lib/typescript/src/TabBar.d.ts +31 -0
  54. package/lib/typescript/src/TabBar.d.ts.map +1 -0
  55. package/lib/typescript/src/TabBarIndicator.d.ts +15 -0
  56. package/lib/typescript/src/TabBarIndicator.d.ts.map +1 -0
  57. package/lib/typescript/src/TabBarItem.d.ts +18 -0
  58. package/lib/typescript/src/TabBarItem.d.ts.map +1 -0
  59. package/lib/typescript/src/TabBarItemLabel.d.ts +13 -0
  60. package/lib/typescript/src/TabBarItemLabel.d.ts.map +1 -0
  61. package/lib/typescript/src/TabView.d.ts +33 -0
  62. package/lib/typescript/src/TabView.d.ts.map +1 -0
  63. package/lib/typescript/src/index.d.ts +11 -0
  64. package/lib/typescript/src/index.d.ts.map +1 -0
  65. package/lib/typescript/src/types.d.ts +76 -0
  66. package/lib/typescript/src/types.d.ts.map +1 -0
  67. package/lib/typescript/src/useAnimatedValue.d.ts +3 -0
  68. package/lib/typescript/src/useAnimatedValue.d.ts.map +1 -0
  69. package/package.json +84 -0
  70. package/src/Pager.android.tsx +1 -0
  71. package/src/Pager.ios.tsx +1 -0
  72. package/src/Pager.tsx +1 -0
  73. package/src/PagerViewAdapter.tsx +186 -0
  74. package/src/PanResponderAdapter.tsx +344 -0
  75. package/src/PlatformPressable.tsx +91 -0
  76. package/src/SceneMap.tsx +30 -0
  77. package/src/SceneView.tsx +106 -0
  78. package/src/TabBar.tsx +725 -0
  79. package/src/TabBarIndicator.tsx +190 -0
  80. package/src/TabBarItem.tsx +301 -0
  81. package/src/TabBarItemLabel.tsx +50 -0
  82. package/src/TabView.tsx +198 -0
  83. package/src/index.tsx +16 -0
  84. package/src/types.tsx +94 -0
  85. package/src/useAnimatedValue.tsx +12 -0
@@ -0,0 +1,198 @@
1
+ import * as React from 'react';
2
+ import {
3
+ I18nManager,
4
+ type LayoutChangeEvent,
5
+ Platform,
6
+ type StyleProp,
7
+ StyleSheet,
8
+ View,
9
+ type ViewStyle,
10
+ } from 'react-native';
11
+
12
+ import { Pager } from './Pager';
13
+ import { SceneView } from './SceneView';
14
+ import { TabBar } from './TabBar';
15
+ import type {
16
+ Layout,
17
+ LocaleDirection,
18
+ NavigationState,
19
+ PagerProps,
20
+ Route,
21
+ SceneRendererProps,
22
+ TabDescriptor,
23
+ } from './types';
24
+
25
+ export type Props<T extends Route> = Omit<PagerProps, 'layoutDirection'> & {
26
+ onIndexChange: (index: number) => void;
27
+ onTabSelect?: (props: { index: number }) => void;
28
+ navigationState: NavigationState<T>;
29
+ renderLazyPlaceholder?: (props: { route: T }) => React.ReactNode;
30
+ renderTabBar?: (
31
+ props: SceneRendererProps & {
32
+ navigationState: NavigationState<T>;
33
+ options: Record<string, TabDescriptor<T>> | undefined;
34
+ }
35
+ ) => React.ReactNode;
36
+ tabBarPosition?: 'top' | 'bottom';
37
+ initialLayout?: Partial<Layout>;
38
+ lazy?: ((props: { route: T }) => boolean) | boolean;
39
+ lazyPreloadDistance?: number;
40
+ direction?: LocaleDirection;
41
+ pagerStyle?: StyleProp<ViewStyle>;
42
+ style?: StyleProp<ViewStyle>;
43
+ renderScene: (props: SceneRendererProps & { route: T }) => React.ReactNode;
44
+ options?: Record<string, TabDescriptor<T>>;
45
+ commonOptions?: TabDescriptor<T>;
46
+ };
47
+
48
+ const renderLazyPlaceholderDefault = () => null;
49
+
50
+ export function TabView<T extends Route>({
51
+ onIndexChange,
52
+ onTabSelect,
53
+ navigationState,
54
+ renderScene,
55
+ initialLayout,
56
+ keyboardDismissMode = 'auto',
57
+ lazy = false,
58
+ lazyPreloadDistance = 0,
59
+ onSwipeStart,
60
+ onSwipeEnd,
61
+ renderLazyPlaceholder = renderLazyPlaceholderDefault,
62
+ // eslint-disable-next-line @eslint-react/no-unstable-default-props
63
+ renderTabBar = (props) => <TabBar {...props} />,
64
+ pagerStyle,
65
+ style,
66
+ direction = I18nManager.getConstants().isRTL ? 'rtl' : 'ltr',
67
+ swipeEnabled = true,
68
+ tabBarPosition = 'top',
69
+ animationEnabled = true,
70
+ overScrollMode,
71
+ options: sceneOptions,
72
+ commonOptions,
73
+ }: Props<T>) {
74
+ if (
75
+ Platform.OS !== 'web' &&
76
+ direction !== (I18nManager.getConstants().isRTL ? 'rtl' : 'ltr')
77
+ ) {
78
+ console.warn(
79
+ `The 'direction' prop is set to '${direction}' but the effective value is '${
80
+ I18nManager.getConstants().isRTL ? 'rtl' : 'ltr'
81
+ }'. This is not supported. Make sure to match the 'direction' prop with the writing direction of the app.`
82
+ );
83
+ }
84
+
85
+ const [layout, setLayout] = React.useState({
86
+ width: 0,
87
+ height: 0,
88
+ ...initialLayout,
89
+ });
90
+
91
+ const jumpToIndex = (index: number) => {
92
+ if (index !== navigationState.index) {
93
+ onIndexChange(index);
94
+ }
95
+ };
96
+
97
+ const handleLayout = (e: LayoutChangeEvent) => {
98
+ const { height, width } = e.nativeEvent.layout;
99
+
100
+ setLayout((prevLayout) => {
101
+ if (prevLayout.width === width && prevLayout.height === height) {
102
+ return prevLayout;
103
+ }
104
+
105
+ return { height, width };
106
+ });
107
+ };
108
+
109
+ const options = Object.fromEntries(
110
+ navigationState.routes.map((route) => [
111
+ route.key,
112
+ {
113
+ ...commonOptions,
114
+ ...sceneOptions?.[route.key],
115
+ },
116
+ ])
117
+ );
118
+
119
+ return (
120
+ <View onLayout={handleLayout} style={[styles.pager, style]}>
121
+ <Pager
122
+ layout={layout}
123
+ navigationState={navigationState}
124
+ keyboardDismissMode={keyboardDismissMode}
125
+ swipeEnabled={swipeEnabled}
126
+ onSwipeStart={onSwipeStart}
127
+ onSwipeEnd={onSwipeEnd}
128
+ onIndexChange={jumpToIndex}
129
+ onTabSelect={onTabSelect}
130
+ animationEnabled={animationEnabled}
131
+ overScrollMode={overScrollMode}
132
+ style={pagerStyle}
133
+ layoutDirection={direction}
134
+ >
135
+ {({ position, render, addEnterListener, jumpTo }) => {
136
+ // All the props here must not change between re-renders
137
+ // This is crucial to optimizing the routes with PureComponent
138
+ const sceneRendererProps = {
139
+ position,
140
+ layout,
141
+ jumpTo,
142
+ };
143
+
144
+ return (
145
+ <React.Fragment>
146
+ {tabBarPosition === 'top' &&
147
+ renderTabBar({
148
+ ...sceneRendererProps,
149
+ options,
150
+ navigationState,
151
+ })}
152
+ {render(
153
+ navigationState.routes.map((route, i) => {
154
+ const { sceneStyle } = options?.[route.key] ?? {};
155
+
156
+ return (
157
+ <SceneView
158
+ key={route.key}
159
+ {...sceneRendererProps}
160
+ addEnterListener={addEnterListener}
161
+ index={i}
162
+ lazy={typeof lazy === 'function' ? lazy({ route }) : lazy}
163
+ lazyPreloadDistance={lazyPreloadDistance}
164
+ navigationState={navigationState}
165
+ style={sceneStyle}
166
+ >
167
+ {({ loading }) =>
168
+ loading
169
+ ? renderLazyPlaceholder({ route })
170
+ : renderScene({
171
+ ...sceneRendererProps,
172
+ route,
173
+ })
174
+ }
175
+ </SceneView>
176
+ );
177
+ })
178
+ )}
179
+ {tabBarPosition === 'bottom' &&
180
+ renderTabBar({
181
+ ...sceneRendererProps,
182
+ options,
183
+ navigationState,
184
+ })}
185
+ </React.Fragment>
186
+ );
187
+ }}
188
+ </Pager>
189
+ </View>
190
+ );
191
+ }
192
+
193
+ const styles = StyleSheet.create({
194
+ pager: {
195
+ flex: 1,
196
+ overflow: 'hidden',
197
+ },
198
+ });
package/src/index.tsx ADDED
@@ -0,0 +1,16 @@
1
+ export { SceneMap } from './SceneMap';
2
+ export type { Props as TabBarProps } from './TabBar';
3
+ export { TabBar } from './TabBar';
4
+ export type { Props as TabBarIndicatorProps } from './TabBarIndicator';
5
+ export { TabBarIndicator } from './TabBarIndicator';
6
+ export type { Props as TabBarItemProps } from './TabBarItem';
7
+ export { TabBarItem } from './TabBarItem';
8
+ export type { Props as TabViewProps } from './TabView';
9
+ export { TabView } from './TabView';
10
+ export type {
11
+ AnimatedStyles,
12
+ NavigationState,
13
+ Route,
14
+ SceneRendererProps,
15
+ TabDescriptor,
16
+ } from './types';
package/src/types.tsx ADDED
@@ -0,0 +1,94 @@
1
+ import type { Animated, StyleProp, TextStyle, ViewStyle } from 'react-native';
2
+ import type { PagerViewProps } from 'react-native-pager-view';
3
+ import type { AnimatedStyle } from 'react-native-reanimated';
4
+
5
+ export type TabDescriptor<T extends Route> = {
6
+ accessibilityLabel?: string;
7
+ accessible?: boolean;
8
+ testID?: string;
9
+ labelText?: string;
10
+ labelAllowFontScaling?: boolean;
11
+ href?: string;
12
+ label?: (props: {
13
+ route: T;
14
+ labelText?: string;
15
+ focused: boolean;
16
+ allowFontScaling?: boolean;
17
+ style?: StyleProp<TextStyle>;
18
+ animatedStyles?: AnimatedStyle<TextStyle>;
19
+ }) => React.ReactNode;
20
+ labelStyle?: StyleProp<TextStyle>;
21
+ icon?: (props: {
22
+ route: T;
23
+ focused: boolean;
24
+ color: string;
25
+ size: number;
26
+ }) => React.ReactNode;
27
+ badge?: (props: { route: T }) => React.ReactElement;
28
+ sceneStyle?: StyleProp<ViewStyle>;
29
+ };
30
+
31
+ export type LocaleDirection = 'ltr' | 'rtl';
32
+
33
+ export type Route = {
34
+ key: string;
35
+ icon?: string;
36
+ title?: string;
37
+ accessible?: boolean;
38
+ accessibilityLabel?: string;
39
+ testID?: string;
40
+ };
41
+
42
+ export type Event = {
43
+ defaultPrevented: boolean;
44
+ preventDefault(): void;
45
+ };
46
+
47
+ export type Scene<T extends Route> = {
48
+ route: T;
49
+ };
50
+
51
+ export type NavigationState<T extends Route> = {
52
+ index: number;
53
+ routes: T[];
54
+ };
55
+
56
+ export type Layout = {
57
+ width: number;
58
+ height: number;
59
+ };
60
+
61
+ export type Listener = (value: number) => void;
62
+
63
+ export type SceneRendererProps = {
64
+ layout: Layout;
65
+ position: Animated.AnimatedInterpolation<number>;
66
+ jumpTo: (key: string) => void;
67
+ };
68
+
69
+ export type EventEmitterProps = {
70
+ addEnterListener: (listener: Listener) => () => void;
71
+ };
72
+
73
+ export type PagerProps = Omit<
74
+ PagerViewProps,
75
+ | 'initialPage'
76
+ | 'scrollEnabled'
77
+ | 'onPageScroll'
78
+ | 'onPageSelected'
79
+ | 'onPageScrollStateChanged'
80
+ | 'keyboardDismissMode'
81
+ | 'children'
82
+ > & {
83
+ keyboardDismissMode?: 'none' | 'on-drag' | 'auto';
84
+ swipeEnabled?: boolean;
85
+ animationEnabled?: boolean;
86
+ onSwipeStart?: () => void;
87
+ onSwipeEnd?: () => void;
88
+ };
89
+
90
+ export type AnimatedStyles = {
91
+ color?: [string, string];
92
+ opacity?: [number, number];
93
+ scale?: [number, number];
94
+ };
@@ -0,0 +1,12 @@
1
+ import * as React from 'react';
2
+ import { Animated } from 'react-native';
3
+
4
+ export function useAnimatedValue(initialValue: number) {
5
+ const lazyRef = React.useRef<Animated.Value>(undefined);
6
+
7
+ if (lazyRef.current === undefined) {
8
+ lazyRef.current = new Animated.Value(initialValue);
9
+ }
10
+
11
+ return lazyRef.current as Animated.Value;
12
+ }