1- import { forwardRef , ReactNode , useCallback , useImperativeHandle , useMemo , useState } from "react" ;
1+ import { noop } from "lodash" ;
2+ import { createContext , forwardRef , ReactNode , useCallback , useContext , useImperativeHandle , useMemo , useState } from "react" ;
23import { StyleProp , StyleSheet , View , ViewStyle } from "react-native" ;
34import { Gesture , GestureDetector , TouchableWithoutFeedback } from "react-native-gesture-handler" ;
45import Animated , { cancelAnimation , Easing , runOnJS , useAnimatedStyle , useSharedValue , withTiming } from "react-native-reanimated" ;
@@ -98,6 +99,21 @@ export type TabsRef = {
9899 closeImmediately ( ) : void ;
99100} ;
100101
102+ /**
103+ * Allow components in tabs to get access to the Tab properties.
104+ */
105+ const TabsContext = createContext < TabsRef & { isOpen : boolean } > ( {
106+ close : noop ,
107+ open : noop ,
108+ closeImmediately : noop ,
109+ isOpen : false ,
110+ } ) ;
111+
112+ /**
113+ * Expose the Tabs Context as a hook
114+ */
115+ export const useTabs = ( ) => useContext ( TabsContext ) ;
116+
101117/**
102118 * A row of tabs and a "more" button.
103119 *
@@ -203,7 +219,7 @@ export const Tabs = forwardRef<TabsRef, TabsProps>(({ style, tabs, textMore = "M
203219 // TODO: Integration with back button.
204220
205221 return (
206- < >
222+ < TabsContext . Provider value = { { close , open , closeImmediately , isOpen : offset . value > 0 } } >
207223 { /* Dismissal area. */ }
208224 < Animated . View style = { [ styles . dismiss , styleDismiss , dynamicDismiss ] } >
209225 < TouchableWithoutFeedback containerStyle = { StyleSheet . absoluteFill } style = { StyleSheet . absoluteFill } onPress = { close } />
@@ -227,7 +243,7 @@ export const Tabs = forwardRef<TabsRef, TabsProps>(({ style, tabs, textMore = "M
227243 </ View >
228244 </ Animated . View >
229245 </ GestureDetector >
230- </ >
246+ </ TabsContext . Provider >
231247 ) ;
232248} ) ;
233249
0 commit comments