Skip to content

Commit

Permalink
responsive grid - clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
giannif committed Jan 27, 2025
1 parent c39237a commit 4a3aca2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
25 changes: 12 additions & 13 deletions packages/react-components/src/components/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,9 @@ class Grid extends PureComponent<Props, State> {
unmounted: boolean = false
paginator = gifPaginator(this.props.fetchGifs, this.state.gifs)
static getDerivedStateFromProps: GetDerivedStateFromProps<Props, State> = (
{ columns, gutter, width, externalGifs }: Props,
{ externalGifs }: Props,
prevState: State
) => {
const gutterOffset = gutter * (columns - 1)
const gifWidth = Math.floor((width - gutterOffset) / columns)
if (prevState.gifWidth !== gifWidth) {
return { gifWidth }
}
if (externalGifs && externalGifs !== prevState.gifs) {
return { gifs: externalGifs }
}
Expand Down Expand Up @@ -181,14 +176,16 @@ class Grid extends PureComponent<Props, State> {
loader: LoaderVisual = DotsLoader,
fetchPriority,
} = this.props
const { gifWidth, gifs, isError, isDoneFetching } = this.state
const { gifs, isError, isDoneFetching } = this.state

const showLoader = !isDoneFetching
const isFirstLoad = gifs.length === 0
let columnWidth = `${Math.floor((width - gutter * (columns - 1)) / columns)}px`
if (percentWidth) {
const gutterOffset = (gutter * (columns - 1)) / columns
columnWidth = `calc(${(gifWidth / width) * 100}% + ${gutterOffset}px)`
}

const totalGutterPx = gutter * (columns - 1)

// gif width to determin rendition and display size (when not percentage)
const gifWidth = (width - totalGutterPx) / columns

// put gifs into their columns
const sortedIntoColumns = getColumns(columns, columnOffsets, gifs, gifWidth)
return (
Expand All @@ -208,7 +205,9 @@ class Grid extends PureComponent<Props, State> {
display: 'flex',
flexDirection: 'column',
gap: gutter,
width: columnWidth,
width: percentWidth
? `calc(${(width / columns) * 100}% + ${totalGutterPx / columns}px)`
: gifWidth,
marginTop: columnOffsets?.[columnIndex],
}}
>
Expand Down
3 changes: 1 addition & 2 deletions packages/react-components/src/components/loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ const bouncer = keyframes`
transform: scale(1.75) translateY(-20px);
}
`
const loaderHeight = 37
const loaderHeight = 52
const Container = styled.div`
display: flex;
align-items: center;
height: ${loaderHeight}px;
padding-top: 15px;
margin: 0 auto;
text-align: center;
justify-content: center;
Expand Down
10 changes: 10 additions & 0 deletions packages/react-components/stories/grid.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ export const GridResponsiveContainer: Story = {
},
}

export const GridResponsiveSmall: Story = {
args: {
percentWidth: '100%',
width: 100,
columns: 4,
gutter: 10,
containerStyles: { width: 800, backgroundColor: 'aquamarine' },
},
}

export const GridResponsiveContainerWithOffsets: Story = {
args: {
width: 1040,
Expand Down

0 comments on commit 4a3aca2

Please sign in to comment.