Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zIndex broken when containers have a background color #23615

Closed
acb opened this issue Feb 23, 2019 · 15 comments
Closed

zIndex broken when containers have a background color #23615

acb opened this issue Feb 23, 2019 · 15 comments
Labels
Bug Newer Patch Available Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@acb
Copy link

acb commented Feb 23, 2019

🐛 Bug Report

zIndex is broken when you have a component that has a container that has a background color. For example, given code like

render() {
    return <View style={styles.background}>
        <View style={styles.redBox}></View>
        <View>
            <View style={styles.greenBox}></View>
        </View>
    </View>
}

let styles = StyleSheet.create({
    background: {
        backgroundColor: "blue",
        height: "100%"
    },
    greenBox: {
        backgroundColor: "green",
        width: 100,
        height: 100,
        zIndex: 2
    },
    redBox: {
        height: 300,
        width: 300,
        backgroundColor: "red",
        position: "absolute",
        top: 0,
        left: 0,
        zIndex: 1
    },
});

you get a view that makes sense, blue background, then red box, then green box on top.

However, if you add a background color to the green box's container the zIndex breaks:

<View style={styles.background}>
   <View style={styles.redBox}></View>
   <View style={{backgroundColor: "yellow"}}>
        <View style={styles.greenBox}></View>
    </View>
</View>

Expected Behavior

Components inside of a container with a background color should still respect zIndex.

Code Example

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

export default class App extends Component {
    render() {
        return (
            <View style={styles.background}>
                <View style={styles.redBox}></View>
                <View style={{backgroundColor: "yellow"}}>
                    <View style={styles.greenBox}></View>
                </View>
            </View>
        );
    }
}

let styles = StyleSheet.create({
    background: {
        backgroundColor: "blue",
        height: "100%"
    },
    greenBox: {
        backgroundColor: "green",
        width: 100,
        height: 100,
        zIndex: 2
    },
    redBox: {
        height: 300,
        width: 300,
        backgroundColor: "red",
        position: "absolute",
        top: 0,
        left: 0,
        zIndex: 1
    },
});

Environment

Environment:
OS: Windows 10
Node: 9.3.0
Yarn: 1.3.2
npm: 4.6.1
Watchman: Not Found
Xcode: N/A
Android Studio: Version 3.2.0.0 AI-181.5540.7.32.5056338

Packages: (wanted => installed)
react: 16.3.2 => 16.3.2
react-native: ^0.55.4 => 0.55.4

It's worth noting that on iOS not even the working example I give here works, it doesn't show the green box above the red one ever.

@react-native-bot
Copy link
Collaborator

Can you run react-native info and edit your issue to include these results under the Environment section?

If you believe this information is irrelevant to the reported issue, you may write [skip envinfo] under Environment to let us know.

@ericlewis
Copy link
Contributor

zIndex is only relative to the parent element, so in this case, the view you are setting to yellow is the parent (the background color is irrelevant), therefore, it is working correctly. If you set the zIndex of the parent element, it will work properly.

@acb
Copy link
Author

acb commented Feb 23, 2019

@ericlewis then why does it work in the first example? The green box shows on top in the first example, but then when you change the background color in the second example (literally the only code change) the green box no longer renders on top of the red box.

I can toggle back and forth between the green box rendering on top of the red one or not just by adding or removing the backgroundColor: "yellow" on the green box's parent so it certainly seems relevant.

@react-native-bot
Copy link
Collaborator

It looks like you are using an older version of React Native. Please update to the latest release, v0.58 and verify if the issue still exists.

The "Resolution: Old Version" label will be removed automatically once you edit your original post with the results of running react-native info on a project using the latest release.

@ericlewis
Copy link
Contributor

@acb I tried the first example on iOS and it definitely does not work. There is an optimization for collapse layout components, so my guess is that perhaps that is occurring here, and accidentally allowing zIndex to look correct.

@JKCooper2
Copy link

JKCooper2 commented Feb 24, 2019

I believe it's working correctly and the behaviour is due to the stacking context
Repro: https://snack.expo.io/@jkcooper/zindex-stacking-context
Details on zIndex stacking context: https://philipwalton.com/articles/what-no-one-told-you-about-z-index/

Edit: Have updated the snack, looks like the issue is a platform disparity between android and ios. Toggle between the two platforms and you'll see on android zIndex pushes the green over. I don't understand zIndex stacking well enough to know which is the correct behaviour

@acb
Copy link
Author

acb commented Feb 25, 2019

Yeah I think the fact that green renders on top at all is the bug, unfortunately...

@acb acb closed this as completed Feb 25, 2019
@acb acb reopened this Dec 6, 2019
@ngdlmk
Copy link

ngdlmk commented Dec 17, 2019

zIndex is not working for RN 0.61.5 on android but working well on IOS

@stale
Copy link

stale bot commented Apr 17, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Apr 17, 2020
@nickarora
Copy link

I'm similarly seeing unexpected behavior with zindex on android with 0.61.5 as well

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Apr 17, 2020
@jonybar9
Copy link

Having a similar problem.
Really wierd

@akshgods
Copy link

same issue

1 similar comment
@bittu1028
Copy link

same issue

@stale
Copy link

stale bot commented Sep 20, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Sep 20, 2020
@stale
Copy link

stale bot commented Oct 4, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Oct 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Newer Patch Available Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

10 participants