Skip to content

Commit e51c055

Browse files
Improve grid rendering with few items
1 parent 1cc4a66 commit e51c055

File tree

4 files changed

+51
-28
lines changed

4 files changed

+51
-28
lines changed

src/components/Common/Grid.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1-
import type { ReactNode } from "react";
1+
import React, { type ReactNode } from "react";
22

33
interface GridProps {
44
className?: string;
55
children: ReactNode;
6+
forceFullWidth?: boolean;
67
}
78

8-
export default function Grid({ className, children }: GridProps) {
9+
export default function Grid({ className, children, forceFullWidth = false }: GridProps) {
10+
// Reason: Use React.Children.toArray to properly flatten fragments
11+
const childArray = React.Children.toArray(children);
12+
const count = childArray.length;
13+
14+
// Consider less than 3 items as not having a full row
15+
const hasFullRow = forceFullWidth || count >= 3;
16+
17+
if (hasFullRow) {
18+
// Normal grid behavior for 3+ items
19+
return (
20+
<div
21+
className={`grid grid-cols-1 gap-4 md:grid-cols-[repeat(auto-fit,minmax(300px,1fr))] ${
22+
className || ""
23+
}`.trim()}
24+
>
25+
{children}
26+
</div>
27+
);
28+
}
29+
30+
// Constrained width for 1-2 items
931
return (
1032
<div
11-
className={`grid grid-cols-1 gap-4 md:grid-cols-[repeat(auto-fit,minmax(300px,1fr))] ${
33+
className={`grid grid-cols-1 gap-4 md:grid-cols-[repeat(auto-fit,minmax(300px,400px))] md:justify-center ${
1234
className || ""
1335
}`.trim()}
1436
>

src/components/Event/EventGallery.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function EventGallery({ event }: Props) {
1717
}
1818

1919
return (
20-
<SimpleSection wide grid title={"Event Gallery"} element={<GalleryDisclaimer />}>
20+
<SimpleSection wide title={"Event Gallery"} element={<GalleryDisclaimer />}>
2121
<EventGalleryImages event={event} />
2222
</SimpleSection>
2323
);

src/components/Event/EventGalleryImages.tsx

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
22

33
import { LuImageOff } from "react-icons/lu";
44

5+
import Grid from "@/components/Common/Grid";
56
import type { EventEnriched } from "@/content";
67
import { isEventUpcoming } from "@/utils/eventFilters";
78

@@ -83,27 +84,29 @@ export default function EventGalleryImages({ event }: Props) {
8384

8485
return (
8586
<>
86-
{reversedImages.map((img, index) => (
87-
<button
88-
key={img.id}
89-
onClick={() => handleImageClick(index)}
90-
className="glass-border rounded-box w-full overflow-hidden"
91-
type="button"
92-
aria-label={`View larger image: ${img.data.caption ?? ""}`}
93-
data-testid={`gallery-image-${index}`}
94-
>
95-
<img
96-
src={img.thumbnail.src}
97-
srcSet={img.thumbnail.srcSet}
98-
sizes={img.thumbnail.sizes}
99-
alt={img.data.caption ?? ""}
100-
className="bg-base-300 aspect-[4/3] w-full cursor-pointer object-cover transition-opacity hover:opacity-90"
101-
loading="lazy"
102-
width={320}
103-
height={240}
104-
/>
105-
</button>
106-
))}
87+
<Grid>
88+
{reversedImages.map((img, index) => (
89+
<button
90+
key={img.id}
91+
onClick={() => handleImageClick(index)}
92+
className="glass-border rounded-box w-full overflow-hidden"
93+
type="button"
94+
aria-label={`View larger image: ${img.data.caption ?? ""}`}
95+
data-testid={`gallery-image-${index}`}
96+
>
97+
<img
98+
src={img.thumbnail.src}
99+
srcSet={img.thumbnail.srcSet}
100+
sizes={img.thumbnail.sizes}
101+
alt={img.data.caption ?? ""}
102+
className="bg-base-300 aspect-[4/3] w-full cursor-pointer object-cover transition-opacity hover:opacity-90"
103+
loading="lazy"
104+
width={320}
105+
height={240}
106+
/>
107+
</button>
108+
))}
109+
</Grid>
107110
<EventImageModal
108111
allImages={galleryImages}
109112
isOpen={isModalOpen}

src/components/Events/EventsViewAlbum.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ export default function EventsViewAlbum({ events }: Props) {
3232
<EventCardList events={[event]} />
3333
</Container>
3434
<Container wide className="mt-6">
35-
<Grid data-testid="event-gallery-images">
36-
<EventGalleryImages event={event} />
37-
</Grid>
35+
<EventGalleryImages event={event} />
3836
</Container>
3937
</div>
4038
))}

0 commit comments

Comments
 (0)