Skip to content

Instantly share code, notes, and snippets.

@babacarcissedia
Created February 25, 2021 14:41
Show Gist options
  • Save babacarcissedia/6f87dfb058fabf95da90dd7db8f355b8 to your computer and use it in GitHub Desktop.
Save babacarcissedia/6f87dfb058fabf95da90dd7db8f355b8 to your computer and use it in GitHub Desktop.
Responsive table - Filling the viewport
import chunk from "lodash/chunk"
// "items" is the generic term used
// to refer to your database items
const chunkedItems = (items, size) => {
return chunk(items, size)
}
const getChunkFor = (width) => {
if (size < 480) {
return 30
} else if (size < 960) {
return 15
} else {
return 10
}
}
// using debounce to improve performance
// Resize event fire much more time than we need in this case
window.addEventListener('resize', debounce(function () {
const size = getChunkFor(window.innerWidth)
const itemGroups = chunkedItems(items, size)
// use itemGroups for rendering
// usage code: for (items in itemGroups) ...
}, 300))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment