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

IOS - zIndex ignored with nested views

Open sodik82 opened this issue 10 years ago • 13 comments

Following code does not work as expected on IOS simulator with RN 0.30.0. On android it seems to work correctly.

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

export default class Demo extends Component {
  render() {
    return (
      <View style={styles.container}>
        <View>
          <View style={[styles.blueItem, {zIndex: 100}]}>
            <Text>zIndex: 100</Text>
          </View>
        </View>
        <View style={[styles.greenItem, styles.absolute, {zIndex: 2}]}>
          <Text>zIndex: 2</Text>
        </View>
      </View>
    );
  }

}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 20,
  },
  blueItem: {
    backgroundColor: 'blue',
    height: 50,
  },
  greenItem: {
    backgroundColor: 'green',
    height: 50,
    width: 50,
    position: 'absolute',
    top: 45,
    left: 0,
  },
});

I would expect that blue item with zIndex 100 will be over green box (with zIndex 2).

image

Note that if I remove the "outer view" (around blue box), zIndex works fine.

sodik82 avatar Jul 22 '16 12:07 sodik82

This is how zindex works, every new view creates a zIndex stacking context. https://philipwalton.com/articles/what-no-one-told-you-about-z-index/

Edit: This is also because every view in react-native starts out at position: relative, as opposed to position: static on the web

JohnyDays avatar Jul 25 '16 15:07 JohnyDays

hm... it is trickier than I expected :) I have tried following example in Chrome and it seems to work as I would expect (D is over C): http://codepen.io/anon/pen/JKvZxX

sodik82 avatar Jul 25 '16 18:07 sodik82

Also every item starts at zIndex: 0 I believe, basically that means that stacking contexts as defined in WC3 isn't really implemented to spec (for performance and complexity reasons). What you get with this new zIndex system is resolving sibling view display order independent of their order in code. I'm not sure it's much more than that, though I might be wrong, would be good to get some opinion from a core member.

Tagging @vjeux because I know he cares about web spec compatibility,

JohnyDays avatar Jul 25 '16 22:07 JohnyDays

thanks for insights. more clarification about intended behaviour will be good anyway. compatibility between platforms (ios/android) is still the problem - one platform does not behave as expected.

sodik82 avatar Jul 26 '16 07:07 sodik82

The behavior of zIndex is strange. Is possible behave as browser's zIndex?

singno avatar Aug 09 '16 09:08 singno

The browser makes some performance tradeoffs, and has significant added layout complexity in every other part of the layout system, in order to be able to implement z-index to spec. I'm not sure this is something react-native wants/needs, especially since most problems related to z-index can be solved in another way.

What is your specific issue @singno?

JohnyDays avatar Aug 09 '16 11:08 JohnyDays

I don't think using zIndex is a good idea on react-native. It only works when Views are siblings of each other, which can change, when RN is collapsing views for performance reasons.

I used them, had issues and just gave them up. Reorder your JSX instead.

fab1an avatar Aug 20 '16 13:08 fab1an

It sounds like this is at least "working as intended" if not "working according to spec" so I am going to close this issue.

lacker avatar Nov 30 '16 00:11 lacker

@lacker - does it mean that Android it is not "working as intended" then? should I open issue for that android and IOS zIndex is working differently?

sodik82 avatar Nov 30 '16 07:11 sodik82

Ehh, hmm that is weird. It does seem like it should work the same across iOS and Android. Yeah, could you open a new issue for that? Ideal would be a rnplay.org link that shows different behavior in iOS + Android.

lacker avatar Nov 30 '16 18:11 lacker

for those interested demo is here - https://rnplay.org/apps/gLwHvg however it seems that the difference is caused by android view removal and can be "fixed" by collapsable={false}

sodik82 avatar Dec 01 '16 09:12 sodik82

I'm glad there's a workaround, at least.

lacker avatar Dec 01 '16 16:12 lacker

zIndex not working in IOS but its working in Android "https://rnplay.org/apps/Ji5rSQ", Can you please help

Kalyani518 avatar Mar 08 '17 15:03 Kalyani518