Prevent layout shifts when thumbnails load in the chapter selector #4713
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.
Prevent layout shifts when thumbnails load in the chapter selector
Pull Request Type
Description
When you expand the chapters selector for the first time on a video, the thumbnails are fetched from the network, unfortunately as that happens asynchronously, it means that as they load in they cause layout shifts. Those unexpected layout shifts are an unpleasant experience for the user, as they might have already started reading some text, that then changed position on the page, without them doing anything (e.g. no scrolling). We already set a width for the thumbnails in the CSS to avoid them taking up too much of the page, but we don't hardcode a height, to avoid potentially squashing together the image.
As YouTube's API helpfully tells us the height and width, we can calculate the desired aspect ratio and set that with CSS in the Vue template, before the image is downloaded. Using the aspect ratio, instead of just setting the height, lets us change the width in the CSS file in the future, without having to make changes to the Vue file.
Screenshots
Don't know how to do a screen recording and the issue isn't that hard to notice.
Testing
Needs to be tested on the local API, as Invidious doesn't support chapters yet, so we have to extract them from the description ourselves and don't have thumbnails.
With this pull request when you open the chapters selector for the first time, the items should just stay where they are when the images load in, instead of moving up and down.
You probably want to pick different videos for the before and after testing, as the issue is a lot less noticeable when the images come from the cache instead of over the network.
Some videos on the
@YouTube
channel that have chapters:Desktop