Created
February 25, 2021 14:41
-
-
Save babacarcissedia/6f87dfb058fabf95da90dd7db8f355b8 to your computer and use it in GitHub Desktop.
Responsive table - Filling the viewport
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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