react-native: Elevation and border radius do not work well with opacity from Android 9
The way elevation works is that it creates a border around the element that is then shifted to the bottom. The background color of the element then hides the inside part of the border.
When we apply border radius to the same element, the border gets thicker to accommodate for the increased radius. So far so good.
Unfortunately if we then apply the opacity to the parent, the background color of the element gets semitransparent, making the border visible and creating an ugly effect.
React Native version: 0.57 Expo SDK 32 Android version: 9.0
Steps To Reproduce
Create an element and apply, elevation, border radius and background color. Then apply opacity on its parent.
Describe what you expected to happen:
The border underneath the element should not leak out
Snack, code example, or link to a repository:
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 43
- Comments: 28 (4 by maintainers)
Using
backgroundColor: '#fff'
sets it right and works as expected.I found that this problem occur when changing opacity on a wrapping element that contain child elements using elevation for shadow. What fixed this bug for me temporarily was to set flag needsOffscreenAlphaCompositing on the wrapping element that i animated opacity style prop for. (For best practice usage check: https://reactnative.dev/docs/view#needsoffscreenalphacompositing) Otherwise setting opacity on each of the child elements should also work, instead of only the parent wrapper.
This was mentioned here: https://github.com/facebook/react-native/issues/23090#issuecomment-669157170
I discovered a simple way to fix the problem, which is adding an opacity property to the child component to overide the effect done by the parent element, such as
opacity: 0.99;
, see if it works for you.The problem is that if you lower or animate the opacity of this element, fading it out, the background color doesn’t help. You can still see the shadow being rendered underneath, like a thick border.
worked for me, what happened was my component was transparent hence i can see whats happening in the back, setting a background color makes it work as intended
Facing this issue with RN 0.59.8 as well.
Seems like
needsOffscreenAlphaCompositing
isn’t working for me. Anyone else encountered and sorted this?@ferrannp hello sir, is there anyways to make this bug more priority. I am waiting for this too long sir.
Still facing the issue on RN 0.60.4
Still not work well.
Seems like the issue is reproducible only on Android version 9.0 (I was testing that bug also on Android version 7.0 and 8.1 where it looks properly).
Still an issue.
Setting
backgroundColor: '#fff'
works.Using @simongoot solution, needsOffscreenAlphaCompositing worked great. In case someone else has the problem with a
TouchableOpacity
, whereneedsOffscreenAlphaCompositing
did not work for me. You can replace theTouchableOpacity
with anAnimated
Presseable
and then animate the opacity viaonPressIn
andonPressOut
.I think this is related to https://github.com/facebook/react-native/issues/23090 and https://github.com/react-navigation/react-navigation/issues/5535.
This does not work for me, is there already another fix? (Expect from using two View components (one for shadow and one for the background color with opacity))