Skip to content

Commit

Permalink
feat: add content container minHeight
Browse files Browse the repository at this point in the history
related: #6, #8
  • Loading branch information
PedroBern committed Dec 4, 2020
1 parent e9905e1 commit a633910
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 49 deletions.
32 changes: 3 additions & 29 deletions example/src/CollapsibleTabViewSmallContentExample.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import * as React from 'react';
import {
StyleSheet,
View,
Text,
Platform,
Dimensions,
StatusBar,
} from 'react-native';
import { StyleSheet, View, Text } from 'react-native';
import { SceneMap, NavigationState } from 'react-native-tab-view';

import {
Expand All @@ -25,33 +18,14 @@ type Route = {
type State = NavigationState<Route>;

const HEADER_HEIGHT = 250;
const APP_HEADER_HEIGHT = 56; // from "./App"

const RN_DEVICE_HEIGHT = Dimensions.get('window').height;

// Fix for android devices with noch
const DEVICE_HEIGHT =
Platform.select({
ios: RN_DEVICE_HEIGHT,
android:
!StatusBar.currentHeight || StatusBar.currentHeight > 24
? RN_DEVICE_HEIGHT
: RN_DEVICE_HEIGHT - StatusBar.currentHeight,
}) || RN_DEVICE_HEIGHT;

export const AlbumsSmallScene = () => {
const { contentContainerStyle, ...rest } = useCollapsibleScene(
'albums-small'
);
const propsAndRef = useCollapsibleScene('albums-small');

return (
<AnimatedAlbums
nCovers={2} // only two covers
{...rest}
contentContainerStyle={{
...contentContainerStyle,
minHeight: DEVICE_HEIGHT + HEADER_HEIGHT - APP_HEADER_HEIGHT,
}}
{...propsAndRef}
/>
);
};
Expand Down
5 changes: 3 additions & 2 deletions example/src/Shared/Albums.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
StyleSheet,
ViewStyle,
Animated,
View,
} from 'react-native';

const COVERS = [
Expand Down Expand Up @@ -52,10 +53,10 @@ export const AnimatedAlbums = React.forwardRef<
<Animated.ScrollView
ref={ref}
style={styles.container}
contentContainerStyle={[styles.content, contentContainerStyle]}
contentContainerStyle={contentContainerStyle}
{...rest}
>
{albumsContent(nCovers)}
<View style={styles.content}>{albumsContent(nCovers)}</View>
</Animated.ScrollView>
);
});
Expand Down
52 changes: 34 additions & 18 deletions src/CollapsibleTabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Animated,
ViewStyle,
LayoutChangeEvent,
View,
} from 'react-native';
import {
TabView,
Expand Down Expand Up @@ -295,25 +296,36 @@ const CollapsibleTabView = <T extends Route>({
);
};

const [containerHeight, setContainerHeight] = React.useState<
number | undefined
>(undefined);

const onLayout = React.useCallback((e: LayoutChangeEvent) => {
setContainerHeight(e.nativeEvent.layout.height);
}, []);

return (
<CollapsibleContextProvider
value={{
activeRouteKey: routes[index][routeKeyProp as keyof Route] as string,
scrollY,
buildGetRef,
headerHeight,
tabBarHeight,
onMomentumScrollBegin,
onScrollEndDrag,
onMomentumScrollEnd,
}}
>
<TabView
{...tabViewProps}
navigationState={{ index, routes }}
renderTabBar={renderTabBar}
/>
</CollapsibleContextProvider>
<View style={styles.container} onLayout={onLayout}>
<CollapsibleContextProvider
value={{
activeRouteKey: routes[index][routeKeyProp as keyof Route] as string,
scrollY,
buildGetRef,
headerHeight,
tabBarHeight,
onMomentumScrollBegin,
onScrollEndDrag,
onMomentumScrollEnd,
containerHeight: containerHeight || 0,
}}
>
<TabView
{...tabViewProps}
navigationState={{ index, routes }}
renderTabBar={renderTabBar}
/>
</CollapsibleContextProvider>
</View>
);
};

Expand All @@ -324,6 +336,10 @@ const styles = StyleSheet.create({
position: 'absolute',
width: '100%',
},
container: {
flex: 1,
overflow: 'hidden',
},
});

export default CollapsibleTabView;
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type CollapsibleContext = {
buildGetRef: (routeKey: string) => GetRef;
headerHeight: number;
tabBarHeight: number;
containerHeight: number;
onMomentumScrollBegin: (
event: NativeSyntheticEvent<NativeScrollEvent>
) => void;
Expand Down Expand Up @@ -78,6 +79,7 @@ export type CollapsibleScenePropsAndRef = {
*/
contentContainerStyle: {
paddingTop: number;
minHeight: number;
};
/**
* Needed for the loading indicator to show correctly on android.
Expand Down
2 changes: 2 additions & 0 deletions src/useCollapsibleScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const useCollapsibleScene = <T extends Route>(
buildGetRef,
headerHeight,
tabBarHeight,
containerHeight,
...rest
} = context;

Expand All @@ -53,6 +54,7 @@ const useCollapsibleScene = <T extends Route>(
ref: buildGetRef(routeKey),
contentContainerStyle: {
paddingTop: headerHeight + tabBarHeight,
minHeight: containerHeight + headerHeight,
},
progressViewOffset: headerHeight + tabBarHeight,
...rest,
Expand Down

0 comments on commit a633910

Please sign in to comment.