In this episode, we tackle the issue of our scroller grid not being responsive—when you resize the browser, part of the grid was getting chopped off. We do a quick recap on how we're calculating our grid items and sizing, which is based on the container width divided by the number of grid items.
To make this dynamic, we set up an event that listens for resize events on our scroller. We then use the awesome @vueuse/core
composable, specifically useElementBounding
, to grab the current width of the scroller whenever it changes. This lets us update the number of grid items on the fly as the window resizes, instead of sticking to a fixed grid.
We walk through installing VueUse, connecting our scroller with a ref, and using the live width value in our calculation. It all comes together so that when you shrink or expand the window, the grid smoothly decreases or increases with no more ugly cut-offs. At the end, you can check items and watch as the grid stays nicely responsive. If you want to, you can even tweak the grid sizing values for your own style. All done—our grid is looking way more pro now!