-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.tsx
129 lines (120 loc) · 2.96 KB
/
index.tsx
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React, { useState, useEffect, useRef, useCallback } from 'react'
import { largestRect } from 'rect-scaler'
interface Props {
children: React.ReactNode
className?: string
boxClassName?: string
updateLayoutRef?: React.MutableRefObject<(() => void) | undefined>
boxAspectRatio?: number
}
type Layout = {
rows: number
cols: number
width: number
height: number
area: number
}
function recalculateLayout(
containerWidth: number,
containerHeight: number,
numRects: number,
rectAspectRatio: number = 1
) {
return largestRect(
containerWidth,
containerHeight,
numRects,
rectAspectRatio,
1
)
}
function usePackedGridLayout(
boxAspectRatio: number = 1
): [
Layout | undefined,
(numBoxes: number) => void,
(el?: HTMLDivElement | null) => void
] {
const [numBoxes, setNumBoxes] = useState(0)
const [layout, setLayout] = useState<Layout>()
const containerRef = useRef<HTMLDivElement>()
const updateLayout = useCallback(
(el?: HTMLDivElement | null) => {
if (el != null) {
containerRef.current = el
}
if (numBoxes > 0 && containerRef.current) {
const width = containerRef.current.getBoundingClientRect().width
const height = containerRef.current.getBoundingClientRect().height
setLayout(recalculateLayout(width, height, numBoxes, boxAspectRatio))
}
},
[numBoxes, boxAspectRatio]
)
useEffect(() => {
updateLayout()
const listener = () => updateLayout()
window.addEventListener('resize', listener)
return () => {
window.removeEventListener('resize', listener)
}
}, [updateLayout])
return [layout, setNumBoxes, updateLayout]
}
export function PackedGrid({
children: _children,
className,
boxClassName,
updateLayoutRef,
boxAspectRatio
}: Props) {
const children = React.Children.toArray(_children).filter(Boolean)
const [layout, setNumBoxes, updateLayout] = usePackedGridLayout(
boxAspectRatio
)
useEffect(() => {
setNumBoxes(React.Children.count(children))
}, [children])
useEffect(() => {
if (updateLayoutRef) {
updateLayoutRef.current = () => updateLayout()
}
return () => {
if (updateLayoutRef) {
updateLayoutRef.current = undefined
}
}
}, [updateLayout, updateLayoutRef])
return (
<div
className={className}
ref={updateLayout}
style={{
display: 'flex',
flexFlow: 'row wrap',
placeContent: 'center'
}}
>
{React.Children.map(children, (child) => (
<div
className={boxClassName}
style={
layout
? {
display: 'block',
width: '100%',
height: '100%',
maxWidth: `${layout.width}px`,
maxHeight: `${layout.height}px`
}
: {
display: 'none'
}
}
>
{child}
</div>
))}
</div>
)
}