Skip to content

Commit

Permalink
add loading card to cut down on CLS
Browse files Browse the repository at this point in the history
  • Loading branch information
gregrickaby committed Nov 26, 2023
1 parent 011ab4d commit 33cffe0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
17 changes: 17 additions & 0 deletions components/LoadingCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import classes from '@/components/Results.module.css'
import {AspectRatio, Card, Flex, Skeleton} from '@mantine/core'

export default function LoadingCard() {
return (
<Card className={classes.card}>
<AspectRatio ratio={3 / 2}>
<Skeleton animate={false} height="100%" width="100%" />
</AspectRatio>
<Card.Section p="md">
<Flex justify="center" align="center">
<Skeleton animate={false} height={24} width="80%" />
</Flex>
</Card.Section>
</Card>
)
}
29 changes: 17 additions & 12 deletions components/Results.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client'

import LoadingCard from '@/components/LoadingCard'
import Media from '@/components/Media'
import {useRedditContext} from '@/components/RedditProvider'
import classes from '@/components/Results.module.css'
Expand Down Expand Up @@ -79,18 +80,22 @@ export default function Results() {
return (
<>
<SimpleGrid cols={{base: 1, sm: 3, lg: 3}}>
{posts.map((post, index) => (
<Card className={classes.card} key={index}>
<AspectRatio ratio={3 / 2}>
<Media key={post.id} {...post} index={index} />
</AspectRatio>
<Card.Section p="md">
<Anchor className={classes.title} href={post.permalink} mt={8}>
{post.title}
</Anchor>
</Card.Section>
</Card>
))}
{loading &&
// loop over and display 24 LoadingCard components
[...Array(24)].map((_, index) => <LoadingCard key={index} />)}
{!loading &&
posts.map((post, index) => (
<Card className={classes.card} key={index}>
<AspectRatio ratio={3 / 2}>
<Media key={post.id} {...post} index={index} />
</AspectRatio>
<Card.Section p="md">
<Anchor className={classes.title} href={post.permalink} mt={8}>
{post.title}
</Anchor>
</Card.Section>
</Card>
))}
</SimpleGrid>
{!loading && (
<Flex justify="center" align="center" p="xl">
Expand Down

1 comment on commit 33cffe0

@vercel
Copy link

@vercel vercel bot commented on 33cffe0 Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.