react-navigation-collapsible
An extension of react-navigation that makes your header collapsible.
Try out the demo on Expo Snack
🏗 The Collapsible Tab-navigator is no longer supported due to the Android bug from react-native.
Usage
import { disableExpoTranslucentStatusBar } from 'react-navigation-collapsible';
disableExpoTranslucentStatusBar();
import { Animated } from 'react-native';
import { useCollapsibleHeader } from 'react-navigation-collapsible';
const MyScreen = ({ navigation, route }) => {
const options = {
navigationOptions: {
headerStyle: { backgroundColor: 'green', height: 150 } ,
headerBackground: <Image /> ,
},
config: {
collapsedColor: 'red' ,
useNativeDriver: true ,
elevation: 4 ,
disableOpacity: true ,
},
};
const {
onScroll ,
onScrollWithListener ,
containerPaddingTop ,
scrollIndicatorInsetTop ,
positionY ,
translateY ,
progress ,
opacity ,
} = useCollapsibleHeader(options);
return (
<Animated.FlatList
onScroll={onScroll}
contentContainerStyle={{ paddingTop: containerPaddingTop }}
scrollIndicatorInsets={{ top: scrollIndicatorInsetTop }}
/* rest of your stuff */
/>
);
};
See /example/src/DefaultHeaderScreen.tsx
See /example/src/StickyHeaderScreen.tsx
See /example/src/BackgroundHeaderScreen.tsx
import { Animated } from 'react-native';
import {
useCollapsibleSubHeader,
CollapsibleSubHeaderAnimator,
} from 'react-navigation-collapsible';
const MySearchBar = () => (
<View style={{ padding: 15, width: '100%', height: 60 }}>
<TextInput placeholder="search here" />
</View>
);
const MyScreen = ({ navigation, route }) => {
const {
onScroll ,
containerPaddingTop ,
scrollIndicatorInsetTop ,
translateY,
} = useCollapsibleSubHeader();
return (
<>
<Animated.FlatList
onScroll={onScroll}
contentContainerStyle={{ paddingTop: containerPaddingTop }}
scrollIndicatorInsets={{ top: scrollIndicatorInsetTop }}
/* rest of your stuff */
/>
{/* Wrap your component with `CollapsibleSubHeaderAnimator` */}
<CollapsibleSubHeaderAnimator translateY={translateY}>
<MySearchBar />
</CollapsibleSubHeaderAnimator>
</>
);
};
See /example/src/SubHeaderScreen.tsx
import { Animated } from 'react-native';
import { useCollapsibleHeader } from 'react-navigation-collapsible';
const MyScreen = ({ navigation, route }) => {
const options = {
navigationOptions: {
header: ({ scene, previous, navigation }) => {
const { options } = scene.descriptor;
const title =
options.headerTitle !== undefined
? options.headerTitle
: options.title !== undefined
? options.title
: scene.route.name;
return (
<MyHeader
title={title}
leftButton={
previous ? (
<MyBackButton onPress={navigation.goBack} />
) : (
undefined
)
}
style={options.headerStyle}
/>
);
},
},
};
const {
onScroll ,
containerPaddingTop ,
scrollIndicatorInsetTop ,
} = useCollapsibleHeader(options);
return (
<Animated.FlatList
onScroll={onScroll}
contentContainerStyle={{ paddingTop: containerPaddingTop }}
scrollIndicatorInsets={{ top: scrollIndicatorInsetTop }}
/* rest of your stuff */
/>
);
};
See /example/src/CustomHeaderScreen.tsx
react-navigation
recommends to use headerMode='screen'
when you use the custom header. [Set headerMode
to screen
]
Install
yarn add react-navigation-collapsible
Contribution
PR is welcome!
How to test changes with the example?
/example imports the library directly from the root folder, configured with babel-plugin-module-resolver.
So, just build the library with the watch
option and run the example.
yarn tsc -w
cd example && yarn ios