1
1
"use client" ;
2
- import { on } from "events" ;
3
- import { RefObject , useEffect , useLayoutEffect , useRef , useState } from "react" ;
2
+ import { useEffect , useRef , useState } from "react" ;
4
3
import { Layout , Layouts , Responsive , WidthProvider } from "react-grid-layout" ;
5
4
6
5
const ResponsiveGridLayout = WidthProvider ( Responsive ) ;
@@ -18,12 +17,14 @@ export default function CardGrid({
18
17
containerPadding,
19
18
children,
20
19
sizes,
20
+ flexInSingleColumn,
21
21
} : {
22
22
gridFormat : [ string , number , number ] [ ] ;
23
23
margin : [ number , number ] ;
24
24
containerPadding : [ number , number ] ;
25
25
children : React . ReactElement [ ] ;
26
26
sizes ?: [ number , number ] [ ] ;
27
+ flexInSingleColumn ?: boolean ;
27
28
} ) {
28
29
// Grid format that defines the breakpoints and the number of columns for each breakpoint
29
30
// [breakpointName, breakPointSize, col]
@@ -38,15 +39,36 @@ export default function CardGrid({
38
39
39
40
var items = children == undefined || children == null ? [ ] : children ;
40
41
var [ rowHeight , setRowHeight ] = useState ( 0 ) ;
42
+ var [ isSingleColumn , setIsSingleColumn ] = useState ( false ) ;
41
43
42
44
const gridRef = useRef ( null ) ;
43
45
useEffect ( ( ) => {
44
46
const current = gridRef . current as any | null ;
45
47
const width = current ?. elementRef . current . offsetWidth ;
46
48
const cols = findColNum ( width ) ;
47
49
onWidthChange ( width , margin , cols , containerPadding ) ;
50
+ setIsSingleColumn ( cols == 1 ) ;
48
51
} ) ;
49
52
53
+ const divRef = useRef ( null ) ;
54
+ useEffect ( ( ) => {
55
+ const observer = new ResizeObserver ( ( entries ) => {
56
+ const width = entries [ 0 ] . contentRect . width ;
57
+ if ( width == 0 ) {
58
+ return ;
59
+ }
60
+ const cols = findColNum ( width ) ;
61
+ onWidthChange ( width , margin , cols , containerPadding ) ;
62
+ setIsSingleColumn ( cols == 1 ) ;
63
+ } ) ;
64
+ if ( divRef . current ) {
65
+ observer . observe ( divRef . current ) ;
66
+ }
67
+ return ( ) => {
68
+ divRef . current && observer . unobserve ( divRef . current ) ;
69
+ } ;
70
+ } , [ isSingleColumn ] ) ;
71
+
50
72
/**
51
73
* Finds the number of columns based on the container width
52
74
* @param width
@@ -125,7 +147,6 @@ export default function CardGrid({
125
147
var positions : boolean [ ] [ ] = [ new Array ( col ) . fill ( false ) ] ;
126
148
127
149
for ( let i = 0 ; i < items . length ; i ++ ) {
128
-
129
150
var width = sizes [ i ] [ 0 ] ;
130
151
var height = sizes [ i ] [ 1 ] ;
131
152
@@ -221,21 +242,35 @@ export default function CardGrid({
221
242
return cols ;
222
243
}
223
244
224
- return (
225
- < ResponsiveGridLayout
226
- ref = { gridRef }
227
- className = "layout"
228
- layouts = { makeLayout ( ) }
229
- margin = { margin }
230
- containerPadding = { containerPadding }
231
- rowHeight = { rowHeight }
232
- breakpoints = { makeBreakpoints ( ) }
233
- cols = { makeCols ( ) }
234
- onWidthChange = { onWidthChange }
235
- >
236
- { items . map ( ( item , index ) => (
237
- < div key = { index . toString ( ) } > { item } </ div >
238
- ) ) }
239
- </ ResponsiveGridLayout >
240
- ) ;
245
+ if ( flexInSingleColumn && isSingleColumn ) {
246
+ return (
247
+ < div
248
+ className = "layout"
249
+ style = { { display : "flex" , flexDirection : "column" , gap : margin [ 1 ] } }
250
+ ref = { divRef }
251
+ >
252
+ { items . map ( ( item , index ) => (
253
+ < div key = { index . toString ( ) } > { item } </ div >
254
+ ) ) }
255
+ </ div >
256
+ ) ;
257
+ } else {
258
+ return (
259
+ < ResponsiveGridLayout
260
+ ref = { gridRef }
261
+ className = "layout"
262
+ layouts = { makeLayout ( ) }
263
+ margin = { margin }
264
+ containerPadding = { containerPadding }
265
+ rowHeight = { rowHeight }
266
+ breakpoints = { makeBreakpoints ( ) }
267
+ cols = { makeCols ( ) }
268
+ onWidthChange = { onWidthChange }
269
+ >
270
+ { items . map ( ( item , index ) => (
271
+ < div key = { index . toString ( ) } > { item } </ div >
272
+ ) ) }
273
+ </ ResponsiveGridLayout >
274
+ ) ;
275
+ }
241
276
}
0 commit comments