Fix layout shift for multi-image posts #1673
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This has been bugging me for a while.
When we encounter a post with a gallery in feed (or profile — anywhere we display posts), we try to render it without images first, then measure the container's width, and then relayout images based on that width. As a result, there is a noticeable layout shift after the first render — even though the actual layout is entirely dependent on its container and doesn't even depend on the image sizes.
This shift is very bad both because it's visually noticeable (the screen always paints first with empty images) and because it screws the perf — everything below has to relayout again.
The fix is to use flexbox instead of manual calculations. I don't actually know flexbox so I had some help from ChatGPT with this. I might have uncovered a potential RN bug with how the
gap
property works, so there's a bit of a hack in the code. I've checked iOS, Android, and web (Chrome, FF, Safari), and the hack seems to work.Before
Notice the layout jump after the glimmer.
ezgif.com-resize.3.mov
After
No layout jump after the glimmer.
ezgif.com-resize.4.mov