Skip to content

Commit 62ed88b

Browse files
feat: add option to flex card grid when in single column
1 parent 8141c46 commit 62ed88b

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

components/CardGrid.tsx

+55-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use client";
2-
import { on } from "events";
3-
import { RefObject, useEffect, useLayoutEffect, useRef, useState } from "react";
2+
import { useEffect, useRef, useState } from "react";
43
import { Layout, Layouts, Responsive, WidthProvider } from "react-grid-layout";
54

65
const ResponsiveGridLayout = WidthProvider(Responsive);
@@ -18,12 +17,14 @@ export default function CardGrid({
1817
containerPadding,
1918
children,
2019
sizes,
20+
flexInSingleColumn,
2121
}: {
2222
gridFormat: [string, number, number][];
2323
margin: [number, number];
2424
containerPadding: [number, number];
2525
children: React.ReactElement[];
2626
sizes?: [number, number][];
27+
flexInSingleColumn?: boolean;
2728
}) {
2829
// Grid format that defines the breakpoints and the number of columns for each breakpoint
2930
// [breakpointName, breakPointSize, col]
@@ -38,15 +39,36 @@ export default function CardGrid({
3839

3940
var items = children == undefined || children == null ? [] : children;
4041
var [rowHeight, setRowHeight] = useState(0);
42+
var [isSingleColumn, setIsSingleColumn] = useState(false);
4143

4244
const gridRef = useRef(null);
4345
useEffect(() => {
4446
const current = gridRef.current as any | null;
4547
const width = current?.elementRef.current.offsetWidth;
4648
const cols = findColNum(width);
4749
onWidthChange(width, margin, cols, containerPadding);
50+
setIsSingleColumn(cols == 1);
4851
});
4952

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+
5072
/**
5173
* Finds the number of columns based on the container width
5274
* @param width
@@ -125,7 +147,6 @@ export default function CardGrid({
125147
var positions: boolean[][] = [new Array(col).fill(false)];
126148

127149
for (let i = 0; i < items.length; i++) {
128-
129150
var width = sizes[i][0];
130151
var height = sizes[i][1];
131152

@@ -221,21 +242,35 @@ export default function CardGrid({
221242
return cols;
222243
}
223244

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+
}
241276
}

0 commit comments

Comments
 (0)