-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
43 lines (35 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React, { useEffect, useRef } from "react"
import PropTypes from "prop-types"
import MagicGrid from "magic-grid"
const MagicGridWrapper = ({ as: Component = "div", children, ...props }) => {
const container = useRef(null)
useEffect(() => {
let grid = null
let timeout
// magic-grid handles resizing via its own `listen` method
// unfortunately event listener it creates is not being cleaned up
// that's why we don't use it and have our own instead
// see: https://github.com/e-oj/Magic-Grid/issues/24
const resize = () => {
if (!timeout)
timeout = setTimeout(() => {
grid && grid.positionItems()
timeout = null
}, 200)
}
if (!grid) {
grid = new MagicGrid({ container: container.current, ...props })
window.addEventListener("resize", resize)
}
grid.positionItems()
return () => {
window.removeEventListener("resize", resize)
}
})
return <Component ref={container}>{children}</Component>
}
MagicGridWrapper.propTypes = {
as: PropTypes.oneOfType([PropTypes.string, PropTypes.elementType]),
children: PropTypes.arrayOf(PropTypes.node),
}
export default MagicGridWrapper