Skip to content

Commit

Permalink
feat: expose hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
andreialecu committed Feb 11, 2021
1 parent 8263536 commit 06e693c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,17 @@ const Container = React.forwardRef<CollapsibleRef, CollapsibleProps>(
[children, lazy, tabNames.value, cancelLazyFadeIn]
)

const headerTranslateY = useDerivedValue(() => {
return revealHeaderOnScroll
? -accDiffClamp.value
: -Math.min(scrollYCurrent.value, headerScrollDistance.value)
}, [revealHeaderOnScroll])

const stylez = useAnimatedStyle(() => {
return {
transform: [
{
translateY: revealHeaderOnScroll
? -accDiffClamp.value
: -Math.min(scrollYCurrent.value, headerScrollDistance.value),
translateY: headerTranslateY.value,
},
],
}
Expand Down Expand Up @@ -454,6 +458,7 @@ const Container = React.forwardRef<CollapsibleRef, CollapsibleProps>(
snappingTo,
contentHeights,
setContentHeights,
headerTranslateY,
}}
>
<Animated.View
Expand Down
61 changes: 59 additions & 2 deletions src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'react'
import { useWindowDimensions } from 'react-native'
import { ContainerRef, RefComponent } from 'react-native-collapsible-tab-view'
import {
import Animated, {
cancelAnimation,
useAnimatedReaction,
useAnimatedRef,
Expand All @@ -19,6 +19,7 @@ import {
withTiming,
interpolate,
Extrapolate,
runOnJS,
} from 'react-native-reanimated'
import { useDeepCompareMemo } from 'use-deep-compare'

Expand Down Expand Up @@ -122,7 +123,7 @@ export function useTabNameContext(): TabName {
}

/**
* Hook to access some key styles that make the whole think work.
* Hook to access some key styles that make the whole thing work.
*
* You can use this to get the progessViewOffset and pass to the refresh control of scroll view.
*/
Expand Down Expand Up @@ -546,3 +547,59 @@ export function useAfterMountEffect(effect: React.EffectCallback) {
}, [didExecute, effect])
return result
}

export function useConvertAnimatedToValue<T>(
animatedValue: Animated.SharedValue<T>
) {
const [value, setValue] = useState(animatedValue.value)

useAnimatedReaction(
() => {
return animatedValue.value
},
(animValue) => {
if (animValue !== value) {
runOnJS(setValue)(animValue)
}
},
[value]
)

return value
}

interface HeaderMeasurements {
/**
* Animated value that represents the current Y translation of the header
*/
top: Animated.SharedValue<number>
/**
* The height of the header
*/
height: number
}

export function useHeaderMeasurements(): HeaderMeasurements {
const { headerTranslateY, headerHeight } = useTabsContext()
return {
top: headerTranslateY,
height: headerHeight,
}
}

/**
* Returns the currently focused tab name
*/
export function useFocusedTab() {
const { focusedTab } = useTabsContext()
const focusedTabValue = useConvertAnimatedToValue(focusedTab)
return focusedTabValue
}

/**
* Returns an animated value representing the current tab index, as a floating point number
*/
export function useAnimatedTabIndex() {
const { indexDecimal } = useTabsContext()
return indexDecimal
}
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export const Tabs = {
}

export { Container, Tab, Lazy, FlatList, ScrollView }
export { useTabsContext, useTabNameContext, useCollapsibleStyle } from './hooks'
export {
useHeaderMeasurements,
useFocusedTab,
useAnimatedTabIndex,
useCollapsibleStyle,
} from './hooks'
export { MaterialTabBar } from './MaterialTabBar/TabBar'
export { MaterialTabItem } from './MaterialTabBar/TabItem'
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ export type ContextType<T extends TabName = TabName> = {
>

contentInset: number

headerTranslateY: Animated.SharedValue<number>
}

export type ScrollViewProps = ComponentProps<typeof Animated.ScrollView>
Expand Down

0 comments on commit 06e693c

Please sign in to comment.