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 in a List #18616

Open
3 tasks done
TheNoim opened this issue Mar 29, 2018 · 30 comments
Open
3 tasks done

zIndex in a List #18616

TheNoim opened this issue Mar 29, 2018 · 30 comments
Labels
Bug Component: FlatList Help Wanted :octocat: Issues ideal for external contributors. Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project.

Comments

@TheNoim
Copy link

TheNoim commented Mar 29, 2018

This is actually a reopen of my #16878 issue. I've got some thumbs up, so I think I am not the only one with this issue. This time I updated my example project to a pure rn project with the latest release (rn Branche)

Environment

info
  React Native Environment Info:
    System:
      OS: macOS High Sierra 10.13.6
      CPU: (8) x64 Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
      Memory: 94.53 MB / 16.00 GB
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 10.12.0 - ~/.nvm/versions/node/v10.12.0/bin/node
      Yarn: 1.16.0 - ~/.nvm/versions/node/v10.12.0/bin/yarn
      npm: 6.9.0 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        API Levels: 23
        Build Tools: 23.0.1
    IDEs:
      Xcode: 10.1/10B61 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.8.3 => 16.8.3
      react-native: 0.59.8 => 0.59.8
    npmGlobalPackages:
      react-native-cli: 2.0.1
      react-native-git-upgrade: 0.2.7

Steps to Reproduce

  1. Make a list (FlatList or Array.map, it happens with both)
  2. The renderItem should get an onPress event
  3. The onPress event changes the style to a high zIndex and position absolute

Expected Behavior

I expect that the component with the high zIndex is over every other component.

Actual Behavior

It seems like it ignores the zIndex property. The behavior is exactly like I would not apply any zIndex. I can remove the zIndex and I get the same result.
Google Drive Link to a screenshot.

Reproducible Demo

Github Demo Project rn Branche This is just an extract out of my current rn project.
Expo Link (Not on the latest rn version)
Google Drive Video which show the issue

@react-native-bot react-native-bot added the Platform: macOS Building on macOS. label Mar 29, 2018
@mattmcdonald-uk
Copy link

mattmcdonald-uk commented Mar 29, 2018

I was about to open a proposal on this, but might be better to just discuss in this issue.

I came across the same problem and worked around it by adding an additional cellStyle prop to my FlatList component. I then added this cellStyle prop to the styles array generated by CellRenderer in VirtualizedList.

Setting the zIndex to the parent element generated by CellRenderer works as you'd expect.

Happy to put in a PR if this seems like an acceptable solution.

@TheNoim For the time being you could use patch-package to make the same change in this commit: https://github.com/mattmcdonald-uk/react-native/commit/a59888b9b68698985b784b6ee29a80c9b878e28d

@kelset kelset added Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. 🔶Lists and removed Platform: macOS Building on macOS. labels Mar 29, 2018
@react-native-bot react-native-bot added the Platform: macOS Building on macOS. label Mar 29, 2018
@hramos hramos removed the Platform: macOS Building on macOS. label Mar 29, 2018
@react-native-bot react-native-bot added the Platform: macOS Building on macOS. label Mar 29, 2018
@hramos hramos removed the Platform: macOS Building on macOS. label Mar 29, 2018
@react-native-bot react-native-bot added the Platform: macOS Building on macOS. label Mar 29, 2018
@TheNoim
Copy link
Author

TheNoim commented Mar 29, 2018

@mattmcdonald-uk Thanks for the solution, but I can't use it. I tried to modify your attempt (mattmcdonald-uk/react-native@a59888b). In my example repo I use a wrapper component which can't have the cellStyle prop and I tried to access the applied style with other methods, but I failed. It is a workaround but not a final solution. This should really get fixed.

Off topic: react-native-bot 1, @hramos 0 :D Just kidding.

@hramos hramos removed the Platform: macOS Building on macOS. label Mar 29, 2018
@ethanyuwang
Copy link

Having the same issue here

@TheNoim
Copy link
Author

TheNoim commented Apr 27, 2018

Will we ever get a fix for this? I mean, this is the second issue about this and still: nothing

@hramos hramos added the Help Wanted :octocat: Issues ideal for external contributors. label May 10, 2018
@hramos
Copy link
Contributor

hramos commented May 10, 2018

As far as I can tell, no PRs have been submitted to address this issue. I'm adding the Help Wanted tag to surface the issue more broadly.

@react-native-bot

This comment has been minimized.

@Haraldson
Copy link

Haraldson commented May 16, 2018

No need to override any React Native internals to set zIndex on the item wrapper views of a FlatList, here’s how I achieved it;

<FlatList
    data={timeline}
    renderItem={this.getItemView}
    CellRendererComponent={({ children, index, style, ...props }) => {
        const cellStyle = [
            style,

            // I want each item to have a higher zIndex than the previous one,
            // in reversed order due to the FlatList being inverted
            { zIndex: this.state.timeline.length - index }
        ]
		
        // OverflowableView for Android...
        return (
            <OverflowableView style={cellStyle} index={index} {...props}>
                {children}
            </OverflowableView>
        )
    }}
    inverted />

@TheNoim
Copy link
Author

TheNoim commented May 21, 2018

@Haraldson It could solve my exact case (Not tested), but the not the core problem with zIndex in a List.
We can consider this as an workaround.
@react-native-bot Still there in v0.55. Source

@isaachinman
Copy link

@Haraldson That workaround looks reasonable, but unfortunately when I override CellRendererComponent, handling of keys seems to break, even if I pass the same key to the first child returned in CellRendererComponent. Any advice there?

@Haraldson
Copy link

Haraldson commented Oct 5, 2018

@isaachinman I don’t quite get the problem? key should be part of props, which is spread into the Cell view.

@isaachinman
Copy link

@Haraldson Let me explain.

Generally speaking, I am attempting to animate the reorder of items within a FlatList using LayoutAnimation. Here is the simplest possible example of that, working just fine.

The problem I've run into is that depending on the direction an item is moving (up or down), it will animate behind the other items, as it is after them in the literal JSX order.

So, I want to increase the zIndex/elevation of an item just when it is animating. Which is how I arrived at this thread. Here's the same example, but using the most basic version of your CellRendererComponent example. The animation is now broken, despite the keys being present and remaining identical.

Even if I only return children directly, the animation breaks due to full re-renders of everything.

Hopefully this is clear... I have no idea what's going on here.

@TheNoim
Copy link
Author

TheNoim commented Oct 5, 2018

I think this issue should definitely get fixed. A workaround would be ok, but I would prefer something by the core team of rn.

@kelset
Copy link
Contributor

kelset commented May 16, 2019

Hello there 👋 this issue seems to have been inactive for the past few months. Because of this, it's likely that the issue is not a high priority anymore or it has been solved by OP; for these reasons, we'll close it.

But please, if it's actually still an issue with 0.59 please comment below and we can reopen it or please send us a Pull Request with a fix 😊

@kelset kelset closed this as completed May 16, 2019
@TheNoim
Copy link
Author

TheNoim commented May 25, 2019

@kelset Issue still persists.

@kelset
Copy link
Contributor

kelset commented May 27, 2019

Can you submit a repro?

@TheNoim
Copy link
Author

TheNoim commented May 27, 2019

I did. In the initial issue. I updated the repo.

@kelset
Copy link
Contributor

kelset commented May 27, 2019

The one submitted above is using RN 0.54 (https://github.com/TheNoim/zIndexBug/blob/3e36101903ab96841ba4841e6db214cb4a7df50e/package.json#L18)- as I mentioned when I closed the issue, we'd need one with 0.59

@TheNoim
Copy link
Author

TheNoim commented May 27, 2019

went through the hassle and updated to 0.59

@TheNoim
Copy link
Author

TheNoim commented Jun 6, 2019

@kelset I proved, it is still an issue. Please reopen it again.

@kelset
Copy link
Contributor

kelset commented Jun 7, 2019

sorry, I've missed the other comment. Reopening.

@ghost
Copy link

ghost commented Aug 4, 2019

@kelset Hello. Could you tell me please, what we can do with issue? I`m trying to make this case in android and I have bad result - the ScrollView is under it.... ()

@TheNoim
Copy link
Author

TheNoim commented Aug 11, 2019

@KassoBrapik Not sure if this is the same issue.

@adnandothussain
Copy link

@TheNoim is there any way to position fix element inside ScrollView ?

@fabOnReact

This comment has been minimized.

@fabOnReact
Copy link
Contributor

fabOnReact commented Apr 28, 2020

the example is based on rn 0.54.3 https://github.com/TheNoim/zIndexBug/blob/rn/package.json#L18

There is another example based on rn 0.59 https://github.com/TheNoim/zIndexBug/blob/master/package.json#L12

I am not sure if the original issue reproduces on latest react-native version and on android, there are several workaround for fixing zIndex issues and they can be adopted in this use case.

@fabOnReact
Copy link
Contributor

fabOnReact commented May 18, 2020

I tried to test the above issue in the react native master branch. During the implementation of the example I noticed that @TheNoim added dependencies like 'react-native-material-cards' that are not part of the react-native library. I believe that reproducible examples should not include dependencies other then react-native...

fabOnReact added a commit to fabOnReact/AwesomeProject that referenced this issue May 28, 2020
@CarbonC
Copy link

CarbonC commented Jun 2, 2020

No need to override any React Native internals to set zIndex on the item wrapper views of a FlatList, here’s how I achieved it;

<FlatList
    data={timeline}
    renderItem={this.getItemView}
    CellRendererComponent={({ children, index, style, ...props }) => {
        const cellStyle = [
            style,

            // I want each item to have a higher zIndex than the previous one,
            // in reversed order due to the FlatList being inverted
            { zIndex: this.state.timeline.length - index }
        ]
		
        // OverflowableView for Android...
        return (
            <OverflowableView style={cellStyle} index={index} {...props}>
                {children}
            </OverflowableView>
        )
    }}
    inverted />

This particular code causes a crash on android react native 62.2... So its no longer a valid workaround.

@seunghyun-woo
Copy link

seunghyun-woo commented Jun 4, 2020

No need to override any React Native internals to set zIndex on the item wrapper views of a FlatList, here’s how I achieved it;

<FlatList
    data={timeline}
    renderItem={this.getItemView}
    CellRendererComponent={({ children, index, style, ...props }) => {
        const cellStyle = [
            style,

            // I want each item to have a higher zIndex than the previous one,
            // in reversed order due to the FlatList being inverted
            { zIndex: this.state.timeline.length - index }
        ]
		
        // OverflowableView for Android...
        return (
            <OverflowableView style={cellStyle} index={index} {...props}>
                {children}
            </OverflowableView>
        )
    }}
    inverted />

This workaround worked as I expected, but when I do this onEndReached is not called, so FlatList doesn't load more data.
Anyone having this problem?

Here is my code:

const CellRendererComponent = (props: any) => {
  const { children } = props
  const [isSwipingCell, setIsSwipingCell] = useState(false)

  const onSwipeStart = () => {
    setIsSwipingCell(true)
  }

  const onSwipeEnd = () => {
    setIsSwipingCell(false)
  }

  return (
    <View
      style={{
        zIndex: isSwipingCell ? 100 : 0,
        flexDirection: 'row',
        width: size.screenWidth,
      }}>
      {React.cloneElement(children[0].props.children[0].props.children, {
        onSwipeStart: onSwipeStart,
        onSwipeEnd: onSwipeEnd,
      })}
      {children[0].props.children[1] &&
        React.cloneElement(children[0].props.children[1].props.children, {
          onSwipeStart: onSwipeStart,
          onSwipeEnd: onSwipeEnd,
        })}
    </View>
  )
}

export default function LikeList() {
  const [data, setData] = useState(DATA)

  const onSwipeSuccess = (id: string) => {
    setData(_data => _data.filter(item => item.id !== id))
  }

  const renderItem = useCallback(({ index, item }) => {
    return (
      <MovingCard
        index={index}
        data={item}
        onSwipeSuccess={onSwipeSuccess}
        onSwipeStart={undefined}
        onSwipeEnd={undefined}
      />
    )
  }, [])

  const keyExtractor = useCallback(item => {
    return item.id
  }, [])

  return (
    <View
      style={{
        flex: 1,
        backgroundColor: color.background.primary,
      }}>
      <FlatList
        data={data}
        CellRendererComponent={CellRendererComponent}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
        numColumns={2}
        horizontal={false}
      />
    </View>
  )
}

@yoannmoinet
Copy link

Couldn't make this workaround work on Android without also having removeClippedSubviews={false} explicitly set.

It is set to true by default on Android, and false on iOS.

@bolan9999
Copy link

Couldn't make this workaround work on Android without also having removeClippedSubviews={false} explicitly set.

It is set to true by default on Android, and false on iOS.

It's working, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Component: FlatList Help Wanted :octocat: Issues ideal for external contributors. Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project.
Projects
None yet
Development

No branches or pull requests