Skip to content

Commit ac0d3b3

Browse files
author
Carla Goncalves
committed
fix(post-card): prevent CLS on post images
Replace bare <img> with an absolutely-positioned img inside the existing `relative aspect-video` wrapper. The aspect-ratio box reserves space before the image loads, eliminating layout shift without adding a next/image dependency to the UI package.
1 parent 0b7278c commit ac0d3b3

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

packages/ui/src/components/post-card.tsx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ export function PostCard({
4646

4747
const titleClassName = cn(
4848
"text-foreground-neutral font-mona-sans mt-4 mb-2",
49-
isFeatured ? "text-2xl font-bold" : "text-md md:text-lg font-[650] sm:font-bold",
49+
isFeatured
50+
? "text-2xl font-bold"
51+
: "text-md md:text-lg font-[650] sm:font-bold",
5052
);
5153
const excerptClassName = cn(
5254
"text-sm text-foreground-neutral-weak line-clamp-2",
@@ -61,13 +63,28 @@ export function PostCard({
6163
<>
6264
<div>
6365
<div className="eyebrow flex gap-2 items-center">
64-
{post.badge && <Badge color="success" label={post.badge} className="w-fit text-xs" />}
65-
{post.date && <span className="text-xs text-foreground-neutral-weak">{post.date}</span>}
66+
{post.badge && (
67+
<Badge
68+
color="success"
69+
label={post.badge}
70+
className="w-fit text-xs"
71+
/>
72+
)}
73+
{post.date && (
74+
<span className="text-xs text-foreground-neutral-weak">
75+
{post.date}
76+
</span>
77+
)}
6678
</div>
6779
{post.title && <h2 className={titleClassName}>{post.title}</h2>}
6880
{post.excerpt && <p className={excerptClassName}>{post.excerpt}</p>}
6981
</div>
70-
{post.author && <AuthorAvatarGroup authors={[post.author]} className={authorClassName} />}
82+
{post.author && (
83+
<AuthorAvatarGroup
84+
authors={[post.author]}
85+
className={authorClassName}
86+
/>
87+
)}
7188
</>
7289
);
7390

@@ -78,7 +95,7 @@ export function PostCard({
7895
<img
7996
src={post.imageSrc}
8097
alt={post.imageAlt ?? post.title}
81-
className={imageClassName}
98+
className={cn(imageClassName, "absolute inset-0 w-full h-full")}
8299
loading={isFeatured ? "eager" : "lazy"}
83100
/>
84101
</div>
@@ -93,7 +110,12 @@ export function PostCard({
93110
{postBody}
94111
</Card>
95112
) : (
96-
<div className={cn("flex flex-col justify-between", !vertical && "order-1")}>
113+
<div
114+
className={cn(
115+
"flex flex-col justify-between",
116+
!vertical && "order-1",
117+
)}
118+
>
97119
{postBody}
98120
</div>
99121
)}

0 commit comments

Comments
 (0)