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

zIndex not working according to CSS spec

Open alekhurst opened this issue 10 years ago • 2 comments

Based on the docs at https://developer.mozilla.org/en-US/docs/Web/CSS/z-index, the RN zIndex does not work properly.

The example below consists of a Web and an RN snippet where there are 2 nested siblings. The first nested sibling has a higher zIndex than the second nested sibling, so the first nested sibling should display on top of the second. On the web, this works fine, but in RN it does not work.

Here is the web code: http://codepen.io/pthrasher/pen/WxqKvo

Here is the RN code:

'use strict';

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

class Sample extends Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.sibling}>
          <View style={styles.cousinJane}>
                <Text>hi</Text>
            </View>
        </View>
        <View style={styles.sibling}>
          <View style={styles.cousinBill}>
                <Text>hi</Text>
            </View>
        </View>
      </View>
    );
  }
};

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  sibling: {
    width: 200,
    height: 200,
  },
  cousinJane: {
    position: 'relative',
    backgroundColor: 'blue',
    top: 190,
    zIndex: 20,
    width: 200,
    height: 200,
  },
  cousinBill: {
    backgroundColor: 'red',
    zIndex: 10,
    width: 200,
    height: 200,
  }
});

AppRegistry.registerComponent('Sample', () => Sample);

For reference, this RN code was run on iOS from a fresh project created with react-native init, and is running on 0.32.0.

alekhurst avatar Aug 25 '16 18:08 alekhurst

My understanding is that zIndex is not meant to follow the spec and it only applies to sibling nodes.

This issue has some discussion: https://github.com/facebook/react-native/issues/8970

ryankask avatar Aug 25 '16 22:08 ryankask

Yes, the z-index implementation only reorders sibling views. Having spec compliant z-index would add a lot of complexity especially because it is not supported on the native platforms.

I'll close this issue in favor of #8970 if we want to further discuss this.

janicduplessis avatar Aug 26 '16 21:08 janicduplessis