This episode is for members only

Sign up to access "One Million Checkboxes with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
16. Fixing responsiveness on load

Transcript

00:00
Let's just recap on our responsive stuff. We know that we're using a bounding box value here to grab the width of the scroller
00:07
to make this responsive. But if we kind of put it in here a little bit and hit refresh, and let's just try and catch it, you can see that it will slightly overlap.
00:15
So I'm going to try my best to catch this. There we go. Now, the reason for this, let's just recap what we did earlier with the item size.
00:23
We took the initial width of the container, and then, of course, we divided that by the amount of grid items we want, which spread everything out nicely for us.
00:33
The problem is, if we come to this with a smaller viewport, of course, we're not going to have a container size of 1, 2, 3, 2. So what we need to do is always make sure
00:44
that this value goes ahead and gets set properly. Now, we already have this onResizeHandler, which takes the width and calculates the grid item's value.
00:54
So what we can actually do here is just invoke onResize immediately when we hit the page. Let's go ahead and see what happens. So we'll get back to the point here
01:04
where we were trying to break it. And as you can see, when we reload the page, this always now goes ahead and sets the correct amount of items and grid based on the width,
01:16
regardless of if we refresh the page or if we pull this in. So a really quick fix there to set these properly. And of course, you can tweak the values as well if you need to.

Episode summary

In this episode, we’re addressing a common issue with making our grid responsive, especially right after a page load. First, we recap how we grab the width of the container—using a bounding box—and how we use that to divide up our grid items evenly across the available space.

However, as shown in the video, there's a little glitch: when you resize the viewport and refresh the page, sometimes items overlap or don’t lay out correctly. This is because the initial calculation for the grid items’ widths is only happening after a certain event (like resizing), not immediately on load.

To fix this, we update our approach so that the layout calculation happens right away when the page loads, as well as when the window resizes. The quick fix is simply invoking our onResize handler as soon as the page loads, ensuring the grid always uses the correct container size whether the page is loaded or resized. Now, no matter what the viewport size is—on page load or after a resize—the items fit perfectly!

You can continue to tune the grid’s responsive behavior if needed, but this tip helps nail that initial page load issue.

Episode discussion

No comments, yet. Be the first!