diff --git a/packages/react-components/src/components/grid.tsx b/packages/react-components/src/components/grid.tsx index a4aa08b6..8d0b24d8 100644 --- a/packages/react-components/src/components/grid.tsx +++ b/packages/react-components/src/components/grid.tsx @@ -169,16 +169,15 @@ class Grid extends PureComponent { const showLoader = !isDoneFetching const isFirstLoad = gifs.length === 0 const gutterOffset = (gutter * (columns - 1)) / columns - const gifPercentWidth = percentWidth ? `calc(${(gifWidth / width) * 100}% + ${gutterOffset}px)` : undefined - let itemWidth: number | undefined - if (!gifPercentWidth) { - const gutterOffset2 = gutter * (columns - 1) - itemWidth = Math.floor((width - gutterOffset2) / columns) + let columnWidth: string = `${Math.floor((width - gutter * (columns - 1)) / columns)}px` + if (percentWidth) { + columnWidth = `calc(${(gifWidth / width) * 100}% + ${gutterOffset}px)` } // put gifs into their columns const sorter: [IGif[]][] = [] const columnHeights: number[] = fillArray(columns, columnOffsets) gifs.forEach((gif) => { + // get the shortest column const columnTarget = columnHeights.indexOf(Math.min(...columnHeights)) const [existingGifs = []] = sorter[columnTarget] || [] sorter[columnTarget] = [[...existingGifs, gif]] @@ -191,48 +190,45 @@ class Grid extends PureComponent { } return ( -
+
- {(sorter || []).map(([_gifs], columnIndex) => { - return ( -
- {(_gifs || []).map((gif) => { - return ( - - ) - })} -
- ) - })} + {sorter.map(([columnGifs = []], columnIndex) => ( +
+ {columnGifs.map((gif) => ( + + ))} +
+ ))}
{!showLoader && gifs.length === 0 && noResultsMessage} {isError ? ( diff --git a/packages/react-components/stories/grid.stories.tsx b/packages/react-components/stories/grid.stories.tsx index 2e130d9e..bf4d8ef4 100644 --- a/packages/react-components/stories/grid.stories.tsx +++ b/packages/react-components/stories/grid.stories.tsx @@ -126,6 +126,16 @@ export const GridResponsiveContainer: Story = { }, } +export const GridResponsiveContainerWithOffsets: Story = { + args: { + width: 1040, + columns: 4, + gutter: 10, + columnOffsets: [0, 0, 350, 350], + containerStyles: { width: 1040, backgroundColor: 'mediumpurple' }, + }, +} + export const GridCustomLoader: Story = { args: { loader: () =>

🌀

,