Skip to content

Commit

Permalink
Refactor grid
Browse files Browse the repository at this point in the history
  • Loading branch information
schiehll committed May 22, 2019
1 parent d889058 commit 78ad248
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
41 changes: 41 additions & 0 deletions src/components/grid/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { useRef, useState, useEffect } from 'react'

import * as S from './styles'

const Grid = ({ children, gap, minWidth = 400 }) => {
const wrapper = useRef()
const [columnsNumber, setColumnsNumber] = useState(3)

const calculateColumnsNumber = () => {
setColumnsNumber(Math.floor(wrapper.current.offsetWidth / minWidth) || 1)
}

useEffect(() => {
calculateColumnsNumber()

window.addEventListener(`resize`, calculateColumnsNumber)
return () => window.removeEventListener(`resize`, calculateColumnsNumber)
}, [])

return (
<S.Wrapper ref={wrapper} gap={gap}>
{children
.reduce((columns, child, i) => {
columns[i % columnsNumber] = (
columns[i % columnsNumber] || []
).concat(child)

return columns
}, [])
.map((column, i) => {
return (
<S.Column key={i} gap={gap}>
{column}
</S.Column>
)
})}
</S.Wrapper>
)
}

export default Grid
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import styled from 'styled-components'
export const Wrapper = styled.div`
display: grid;
grid-auto-flow: column;
grid-gap: ${props => props.gap || `1em`};
grid-gap: ${({ gap }) => gap || `1em`};
`

export const Col = styled.div`
export const Column = styled.div`
display: grid;
grid-gap: ${props => props.gap || `1em`};
grid-gap: ${({ gap }) => gap || `1em`};
grid-auto-rows: max-content;
`
43 changes: 0 additions & 43 deletions src/components/masonry/index.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/components/widgets/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import Masonry from 'components/masonry'
import Grid from 'components/grid'
import Overview from 'docs/overview.mdx'
import Other from 'docs/other.mdx'

Expand All @@ -8,7 +8,7 @@ import * as S from './styles'
const Widgets = () => {
return (
<S.Wrapper>
<Masonry gap={'20px'}>
<Grid gap="20px">
<S.Widget>
<Overview />
</S.Widget>
Expand Down Expand Up @@ -39,7 +39,7 @@ const Widgets = () => {
<S.Widget>
<Overview />
</S.Widget>
</Masonry>
</Grid>
</S.Wrapper>
)
}
Expand Down

0 comments on commit 78ad248

Please sign in to comment.