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

zIndex does not work with dynamic components on Android

Open madox2 opened this issue 10 years ago • 84 comments

I am trying to render elements conditionally where each element has different zIndex style property. Using folowing code in Android emulator with react-native 0.30.0.

import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';

export default class Demo extends Component {
  constructor(props, ctx) {
    super(props, ctx);
    this.state = {
      showGreen: true,
    };
  }
  render() {
    return (
      <View style={{flex: 1, padding: 20}}>
        <View style={[styles.item, {zIndex: 3, backgroundColor: 'red'}]}>
          <Text>zIndex: 3</Text>
        </View>
        {this.state.showGreen &&
        <View style={[styles.item, {zIndex: 2, backgroundColor: 'green'}]}>
          <Text>zIndex: 2</Text>
        </View>
        }
        <View style={[styles.item, {zIndex: 1, backgroundColor: 'blue'}]}>
          <Text>zIndex: 1</Text>
        </View>
        <View style={styles.button}>
          <Text onPress={() => this.setState({ showGreen: !this.state.showGreen })}>
            Toggle green
          </Text>
        </View>
      </View>
    );
  }

}

const styles = StyleSheet.create({
  item: {
    marginTop: -20,
    height: 50,
    paddingTop: 22,
  },
  button: {
    backgroundColor: 'gray',
    marginTop: 30,
  }
});

Initial screen looks like expected:

zindex1

When I click 'Toggle green' button to dynamically show/hide green element it hides blue element instead and furthermore element's text is missing:

zindex2

When I click the button again, red and green elements remains visible and the toggle button jumps down.

madox2 avatar Jul 22 '16 10:07 madox2

@nhuthuynh but elevation adds shadow

brkyldz avatar May 02 '17 12:05 brkyldz

@brkyldz : because it will affect z-order in android as it document mentioned : " Sets the elevation of a view, using Android's underlying elevation API. This adds a drop shadow to the item and affects z-order for overlapping views. Only supported on Android 5.0+, has no effect on earlier versions."

nhuthuynh avatar May 03 '17 08:05 nhuthuynh

Closing this since it is now fixed (sorry for not posting here I didn't know about this issue).

This will make it in RN 0.45 (May) which should be in RC in a few days.

janicduplessis avatar May 03 '17 18:05 janicduplessis

@janicduplessis I' working on an app that is currently using RN 0.45.1 and it has a View with 3 images inside. I need left and right images to be on top of the center image, so I set a zIndex to the left image so its placed on top of the center one. It does not work. It only works when instead of using zIndex, I use elevation in Android. This way it does work, but I get the warning error saying Warning: Failed prop type: Invalid props.style key `elevation` supplied to `RCTImageView`.

This is my code

<View
  style={{
    flex: 2,
    justifyContent: 'center',
    alignItems: 'center',
    flexDirection: 'row',
  }}
>
  <Image
     source={require('./image_left.png')}
     style={{ top: 60, left: 50, elevation: 1 }} // zIndex doesn't work
  />
  <Image source={require('./image_center.png')} />
  <Image
     source={require('./image_right.png')}
     style={{ top: 100, left: -65 }}
   />
</View>

BTW, this is what I'm trying to do

images

tafelito avatar Jun 29 '17 14:06 tafelito

@janicduplessis I am still having this issue. I uploaded a screencast for ios, android, and even uwp(windows). Android is funky. I was on 0.45.1 but then I ran react-native-git-upgrade and then react-native start then react-native run-android but I am having same issue, even though I am now at 0.46.1. I uploaded screencasts in a stackoverflow issue here please - https://stackoverflow.com/questions/45021849/zindex-not-working-on-android-0-46-but-does-on-ios-and-even-uwp-windows

Noitidart avatar Jul 11 '17 04:07 Noitidart

Can confirm what others have mentioned. I added the following to my style.alert and it works on both platforms now.

...Platform.select({
      ios: {zIndex: 9999},
      android: {elevation: 100}
    })

jaredmharris avatar Jul 18 '17 23:07 jaredmharris

Thanks for your case @jaredmharris. However using elevation is not the same as zIndex, as pointer-events pass through what is underneath. elevation also causes a shadow to be created.

Noitidart avatar Jul 18 '17 23:07 Noitidart

still no solution for the Zindex issue on android?

jaycee425 avatar Jul 25 '17 16:07 jaycee425

I think the issue is that zIndex was not updated properly after the view was mounted. #15203 should fix it, let me know if you can test it and it fixes your problem.

janicduplessis avatar Jul 25 '17 22:07 janicduplessis

A sincere thank you for trying your hand to fix it @janicduplessis - it is a blocker issue for the app I am working on.

Noitidart avatar Jul 26 '17 04:07 Noitidart

I simply solved this very quickly by creating two identical style objects and one of them doesn't have the zIndex , I chose what style to be served through the Platform checker.

<View style={platform({ ios: styles.overlayContaineriOs, android: styles.overlayContainerAndroid })} >

ali-sao avatar Aug 07 '17 01:08 ali-sao

still seeing this issue, I have a listview that when triggered overlays over some other components. Everytime the listview is activated and overlays over the components then is deactivated (removing the overlay) one component dissappears each time.

rtman avatar Aug 17 '17 11:08 rtman

@janicduplessis I am experiencing maybe a similar zIndex related issue:

zindex here is the code : code sample demonstrating the issue the image has zIndex:1 , i am unable to press the close button . If i remove zIndex from the image than i can press the close button .

shubhnik avatar Aug 20 '17 08:08 shubhnik

The fix has not been released yet, it will make it in the next RC release (no eta on when this will happen)

janicduplessis avatar Aug 23 '17 06:08 janicduplessis

After one year and this error is still exists. I'm using 0.44.0

If a view has zIndex, then a modal, after closing this modal, the view will be disappear. This is should be mark as critical error and need to be fixed asap.

dzpt avatar Aug 24 '17 18:08 dzpt

@jaredmharris @Noitidart Anyway to prevent pointer-events pass through while using elevation?

dzpt avatar Aug 28 '17 21:08 dzpt

Fortunately I did not have to deal with pointer-events with my code. I popup an alert on the screen that disappears after a few seconds.

Are you using a Modal component? You shouldn't need to set zIndex/elevation for that. Make sure the order of your components are ordered correctly. Children components appear on top of parent components so see if you can change things up to get it to work.

jaredmharris avatar Aug 28 '17 23:08 jaredmharris

@Tom29 I could not find a way to make pointer-events pass using elevation alone. I would have to include state senstive logic to reorganize the order of elements to make sure my element i need on top is rendered bottom most.

Noitidart avatar Aug 28 '17 23:08 Noitidart

@jaredmharris Yes, i'm using modal, then after closing it, view with zIndex will be disappeared. This is so annoying bug on Android :( Many thank, i've changed the order or view component instead of using zIndex until this bug fixed.

dzpt avatar Aug 29 '17 04:08 dzpt

For anyone still stuck on the old RN here's a function I use that can be spread into your styles. It's based on the ideas presented by others above:

function zIndexWorkaround(val: number): Object {
    return Platform.select({
        ios: {zIndex: val},
        android: {elevation: val}
    });
}

Use it like this in place of e.g: zIndex: 100

...zIndexWorkaround(100)

Remove the Flow type checking from the function if you don't need it

glenn-axsy avatar Nov 03 '17 14:11 glenn-axsy