Content of lightbox not displayed
When showing a lightbox (in a single tab app), the content (just a Text component) is randomly displayed (sometimes it is, sometimes not).
The lightbox background is correctly displayed. The content's functions componentDidMount/render/componentWillUnmount are correctly called.
[email protected]
[email protected]
[email protected] (patched with the fix)
Emulator Genymotion Samsung Galaxy S6 - 5.0.0 - API 21 - 1440x2560
build.gradle :
compileSdkVersion 25
buildToolsVersion "25.0.1"
we have same problem
Same issue here - seems about 1 in 4 calls will render the background blur and shading, but not the content of the root view of the lightbox screen. This seems to be an issue on Android only. Tapping to dismiss the invisible root view still works. Going to have to use a modal or something other on Android.
We didn't experienced this in physical devices, not even official android emulator, we only have this issue in genymotion. Can be related to screen resolution/ratio?
Update here - I was able to get the lightbox to work consistently on Android, at least with my setup ([email protected], Android 8.1.0, on-device Pixel 2). The key seemed to be adding the 'adjustSoftInput' key when showing the lightbox:
Navigation.showLightBox({
screen: ANY_SCREEN_ID,
style: {
tapBackgroundToDismiss: true,
backgroundBlur: 'dark', // 'dark' / 'light' / 'xlight' / 'none' - the type of blur on the background
backgroundColor: 'rgba(0, 0, 0, 0.7)',
},
passProps: { },
adjustSoftInput: 'resize',
});
My main activity in the AndroidManifest also has this attribute: android:windowSoftInputMode="adjustResize"
EDIT - I spoke too soon. A release build with these settings still has the issue, where 1 in 5 launches of the lightbox results in no content. I'll try to repro with the example project tomorrow.
Facing the same issue here, can anybody fix it ?
I guess this can be fixed by wrapping your container in a transparent flex container:
export default class Alert extends Component {
render() {
return (
<View style={styles.flexContainer}>
<View style={styles.container}>
<Text>
{title}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
flexContainer: {
flex: 1,
backgroundColor: 'transparent',
alignItems: 'center',
justifyContent: 'center',
},
container: {
backgroundColor: '#ffffff',
borderRadius: 5,
padding: 16,
justifyContent: 'center',
width: Math.max(Dimensions.get('window').width * 0.7, 320),
minHeight: 120,
},
});
could you please test this?
Just Try it and it doesn't work
Is it necessary to set the minHeight and width ?
{
width: Math.max(Dimensions.get('window').width * 0.7, 320),
minHeight: 120
}
no, you can set those whatever you want, but it's necessary to wrap your container by flexContainer.
Ya, I have try it but it doesn't work Anyway thanks
I was calling showLightBox in a Promise.then() function, by moving showLightBox outside of then function I can no longer reproduce this issue, if you're doing same thing could you please test this too? also make sure showLightBox is not wrapped by any try/catch.
Thanks for reply again.
After double checking, I am pretty sure that I am not putting the showLightBox function inside either Promise.then() or try/catch
Expected Behaviour : With the Loading Indicator ( Hourglass )
Abnormal Behaviour :

Got a little further on this one. Whether or not the lightbox shows seems to be related to the operational complexity of the render() method of the component. Very simple render() methods that return quickly prevent the lightbox from showing. More complex, longer to execute (or layout?) render() methods cause the lightbox to show predictably. This explains why I was finding that only production builds were showing the problem - evidently development builds have enough extra processing going on (perhaps through the tcp bridge?) to cause the lightbox to show every time. Production builds with performance optimizations exhibit the 'no lightbox content' behavior. It also explains why adding some view wrappers or complicated styles seems to help some folks above (cc @sijad @kibot914 )
So there seems to be some sort of race condition going on.
In my component, my 'loading' state is very simple, and after the component finishes fetching data, it renders a full display. This 'loading' state was causing the lightbox to not appear, by being so quick to render. I can add a little bit of arbitrary complexity, and get the lightbox to show every time on my Pixel 2, production build:
const STYLES = StyleSheet.create({
rootContainer: {
width: windowDimensions.width * 0.85,
backgroundColor: 'white',
padding: 30,
borderRadius: 5,
...Platform.select({
android: {
// Android does not seem to compute the height correctly using flex
height: 335,
},
}),
},
})
_operationalComplexity() {
for (let i = 0; i < 500; i += 1) {
md5(`message${i}`);
}
}
render() {
if (this.state.loading) {
this._operationalComplexity();
}
return (
<View style={STYLES.rootContainer}>
{this.state.loading ? this._showLoading() : this._showFullComponent()}
</View>
);
}
Obviously, this isn't great in terms of a real fix since the arbitrary 'complexity' depends on device characteristics. I'll keep looking into the internals of the rendering on the lightbox.
Well, I'm about as far as I can get with my debugging skills without knowing more about React Native/ React Native Navigation. It looks like when things go wrong, the measured dimensions for the ContentView owned by the LightBox are 0. I've added some logging here in onMeasure in ContentView.java:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
int heightSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.AT_MOST);
super.onMeasure(widthSpec, heightSpec);
final int measuredHeight = viewMeasurer.getMeasuredHeight(heightSpec);
final int measuredWidth = viewMeasurer.getMeasuredWidth(widthSpec);
Log.d("ReactNative", "Measured dimensions for " + this.screenId + ": (" + measuredWidth + "," + measuredHeight + ")");
setMeasuredDimension(measuredWidth, measuredHeight);
}
The 'good' lightbox displays show measured dimensions of (1080,1794), while the instances where the lightbox content fails to display show measurements of (0,1794) and (0,0).
@jbettcher Thanks for your work man.
So can we fix this by simply setting the width and height of the lightbox in javascript level ?
Or do we have to fix it in ContentView.java ? Say setting a specific measuredHeight and measuredWidth in ContentView.java
I've tried several javascript-level fixes like you describe @kibot914. Unfortunately, this is an issue in the android UI layer. From what I can tell there's a race at times between the creation of the react ContentView that holds the screen component and the onDisplayListener for that content. When things go bad, the onDisplayListener seems to fire before the ContentView has created the components. It takes a measurement (here) and one of those (width, height) numbers comes back 0. The animateShow then happens on content with a zero width or height, resulting in no content being shown.
I'm trying to figure out why it needs to make this measurement at all. Seems to me the content size should be bounded by the display area of the screen and not the child content measurements, but there's probably stuff I'm missing.
Here's a potential fix for this one: jbettcher/react-native-navigation@0b01bb97ad0de049cb4b35349bea871837534158. It uses an android view tree observer/global layout listener instead of the react onDisplayListener, and holds off on the animation until both the child content screen is present, and its height/width are non-zero.
Obligatory "I have no idea what I'm doing" caveat, but it seems to work well for my use cases. I'll do more testing with this one tomorrow.
@jbettcher I can confirm the fix is working. This needs to be in a future release 👍
Cool, glad this is working for folks. It fixed the issues I was having as well, and I haven't found any problems in production.
Happy to submit a PR - Not sure what the project maintainers have as far as recommendations for that go, but I'll start with the request and go from there.