react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

React navigation's collapsible TabNavigator issue

Open alexstoyanov opened this issue 9 years ago • 1 comments

I want to achieve collapsible TabNavigator

Current Behavior

  • I have used position : 'absolute' style for the tabContainer and paddingTop for the FlatList in order to make the collapsible effect. Using absolute position and padding makes the tabs NOT clickable.
  • When scrolling down and then back to the top the tabs appear BEHIND the FlatList. Is it possible to make the tabs appear in front of the FlatList.

Expected Behavior

  • Tabs are clickable
  • Tabs appear in front of FlatList when scrolling upwards ` // COMPONENT CONTAINING FlatList

class ListScreen extends Component {

constructor(props) {
    super(props);

    this.props.setScrollY(new Animated.Value(0));
}

keyExtractor = (item, index) => index;

render() {
    return (
        <View style={{flex:1}}>
            {this.renderLayout()}
        </View>
    );
}

renderLayout() {
    let data = removeNulls(this.props.data);

        return (
                <FlatList
                    removeClippedSubviews={false}
                    keyExtractor={this.keyExtractor}
                    data={data}
                    onScroll={Animated.event(
                        [{nativeEvent: {contentOffset: {y: this.props.scrollY}}}],
                        {useNativeDriver: false}
                    )}
                    scrollEventThrottle={16}
                    overScrollMode="never"
                    style={globalStyles.containerStyle}
                    contentContainerStyle={{marginTop: 80}}
                    renderItem={({item}) => <ListItem absence={item}/>}
                />
        );
}

}

const mapStateToProps = state => ({ scrollY: state.classBook.scrollY data: state.classBook.data });

export default connect(mapStateToProps, { setScrollY })(ListScreen); // COMPONENT CONTAINING CustomTabBar

class CustomTabBar extends Component {

render() {
    const navigation = this.props.navigation;
    const {routes, index} = this.props.navigationState;

    const {
        navigation: {state}
    } = this.props;

    let theValue = 0;
    let position = {};
    let scrollY = this.props.scrollY;
    if (scrollY) {
        theValue = scrollY;
        let positionStyle = Animated.diffClamp(Animated.add(
            scrollY.interpolate({
                inputRange: [0, 48],
                outputRange: [0, 48],
                extrapolateLeft: 'clamp',
            }),
            0,
        ), 0, 48).interpolate({
            inputRange: [0, 48],
            outputRange: [0, -48],
            extrapolate: 'clamp',
        });
        position = {transform: [{translateY: positionStyle}]};

    }

    return (
        <Animated.View style={[styles.tabContainer, position]}>
            {routes.map((route, idx) => {
                let tabIcon = "fiber-new";
                let tabLabel = "";
                let selected = index === idx;
                switch (this.props.navigation.state.routes[idx].key) {
                    case "LatestGradesAbsences" :
                        tabIcon = selected ? "ios-notifications-outline" : "md-notifications";
                        tabLabel = "Най-ново";
                        break;
                    case "BadgesNavigator" :
                        tabIcon = selected ? "ios-happy-outline" : "md-happy";
                        tabLabel = "Отзиви";
                        break;
                    case "Absences" :
                        tabIcon = selected ? "ios-alarm-outline" : "md-alarm";
                        tabLabel = "Отсъствия";
                        break;
                    case "Grades" :
                        tabIcon = selected ? "ios-bookmarks-outline" : "md-bookmarks";
                        tabLabel = "Оценки";
                        break;
                }
                const color = selected ? '#ffffff' : 'white';
                const size = selected ? 30 : 30;
                return (
                    <TouchableOpacity
                        onPress={() => {
                            if (index !== idx) {
                                navigation.navigate(route.routeName);
                            }
                        }}
                        style={styles.tab}
                        key={route.routeName}
                    >
                        <View style={index === idx ? [styles.selectedTabShadow, {height: scrollY === 0 ? 20 : 48} ]
                            : [styles.nonSelectedTabShadow, {height: scrollY === 0 ? 20 : 48}]}>
                            <Icon name={tabIcon}
                                  size={size}
                                  color={color}/>
                            {
                                selected ?
                                    <Text style={{backgroundColor: 'rgba(0,0,0,0)', fontSize: 10, color: color}}>
                                        {tabLabel}
                                    </Text>
                                    : <View/>
                            }
                        </View>
                    </TouchableOpacity>
                );
            })}
        </Animated.View>
    );
}
static navigationOptions = {
    tabBarOptions: {
        scrollEnabled: true,
        indicatorStyle: {
            backgroundColor: globalColors.tabIndicatorColor
        },labelStyle: {
            fontSize: 16,
        },
    }
}

}

const styles = StyleSheet.create({ tabContainer: { flexDirection: 'row', backgroundColor: '#4276A4', top: 0, left: 0, right: 0, position: 'absolute' }, title: { backgroundColor: 'transparent', color: 'white', fontSize: 18, }, tab: { flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: '#4276A4', }, selectedTabShadow: { width: 64, backgroundColor: '#d64635', alignItems: 'center', justifyContent: 'center', }, nonSelectedTabShadow: { width: 64, alignItems: 'center', justifyContent: 'center', } });

const mapStateToProps = state => ({ scrollY: state.classBook.scrollY });

export default connect(mapStateToProps)(CustomTabBar); `

ezgif com-optimize

software version
react-navigation ^1.0.0-beta.12
react-native ^0.48.1
node 6.11.0
npm or yarn 5.5.1

alexstoyanov avatar Nov 30 '17 10:11 alexstoyanov

Found a solution with putting zIndex on the CustomTabBar

alexstoyanov avatar Nov 30 '17 10:11 alexstoyanov