feat: improve loading state in CarouselHero and CategorySection with ImageSkeletonIcon

This commit is contained in:
Ardeman 2025-03-20 18:22:07 +08:00
parent 33096ab7c1
commit 63bbf70bd6
3 changed files with 30 additions and 13 deletions

View File

@ -77,10 +77,7 @@ export const CarouselHero = (properties: TNews) => {
<div className="embla__container hero flex sm:gap-x-8"> <div className="embla__container hero flex sm:gap-x-8">
<Suspense <Suspense
fallback={ fallback={
<div <div className="embla__slide hero flex w-full min-w-0 flex-none animate-pulse justify-between gap-3 max-sm:mt-2 sm:flex">
role="status"
className="embla__slide hero flex w-full min-w-0 flex-none animate-pulse justify-between gap-3 max-sm:mt-2 sm:flex"
>
<div className="flex aspect-[174/100] h-full items-center justify-center rounded-md bg-gray-300 dark:bg-gray-700"> <div className="flex aspect-[174/100] h-full items-center justify-center rounded-md bg-gray-300 dark:bg-gray-700">
<ImageSkeletonIcon /> <ImageSkeletonIcon />
</div> </div>

View File

@ -85,7 +85,6 @@ export const CarouselSection = (properties: TNews) => {
fallback={Array.from({ length: 3 }).map((_, index) => ( fallback={Array.from({ length: 3 }).map((_, index) => (
<div <div
key={index} key={index}
role="status"
className="embla__slide flex w-full min-w-0 flex-none animate-pulse flex-col justify-between gap-3 sm:w-1/3" className="embla__slide flex w-full min-w-0 flex-none animate-pulse flex-col justify-between gap-3 sm:w-1/3"
> >
<div className="flex h-[280px] w-full items-center justify-center rounded-md bg-gray-300 dark:bg-gray-700"> <div className="flex h-[280px] w-full items-center justify-center rounded-md bg-gray-300 dark:bg-gray-700">

View File

@ -6,6 +6,7 @@ import { twMerge } from 'tailwind-merge'
import { ErrorAwait } from '~/components/error/await' import { ErrorAwait } from '~/components/error/await'
import { CarouselNextIcon } from '~/components/icons/carousel-next' import { CarouselNextIcon } from '~/components/icons/carousel-next'
import { CarouselPreviousIcon } from '~/components/icons/carousel-previous' import { CarouselPreviousIcon } from '~/components/icons/carousel-previous'
import { ImageSkeletonIcon } from '~/components/icons/image-skeleton'
import { Button } from '~/components/ui/button' import { Button } from '~/components/ui/button'
import { useNewsContext } from '~/contexts/news' import { useNewsContext } from '~/contexts/news'
import type { loader } from '~/routes/_news' import type { loader } from '~/routes/_news'
@ -43,7 +44,31 @@ export const CategorySection = (properties: TNews) => {
</div> </div>
<div className="grid sm:grid-cols-3 sm:gap-x-8"> <div className="grid sm:grid-cols-3 sm:gap-x-8">
<Suspense fallback={<div>Loading...</div>}> <Suspense
fallback={Array.from({ length: 3 }).map((_, index) => (
<div
key={index}
className="grid gap-3 sm:gap-x-8"
>
<div className="flex h-[280px] w-full items-center justify-center rounded-md bg-gray-300 dark:bg-gray-700">
<ImageSkeletonIcon />
</div>
<div className="flex w-full flex-col gap-4">
<div className="flex flex-col gap-4">
<div className="h-6 w-full rounded-full bg-gray-200 dark:bg-gray-700"></div>
<div className="h-6 w-[20%] rounded-full bg-gray-200 dark:bg-gray-700"></div>
</div>
<div className="flex flex-col gap-2.5">
<div className="h-5 max-w-[80%] rounded-full bg-gray-200 dark:bg-gray-700"></div>
<div className="h-5 rounded-full bg-gray-200 dark:bg-gray-700"></div>
<div className="h-5 max-w-[50%] rounded-full bg-gray-200 dark:bg-gray-700"></div>
</div>
</div>
<div className="h-[50px] w-full bg-gray-200 dark:bg-gray-700" />
<span className="sr-only">Loading...</span>
</div>
))}
>
<Await <Await
resolve={items} resolve={items}
errorElement={<ErrorAwait />} errorElement={<ErrorAwait />}
@ -56,18 +81,14 @@ export const CategorySection = (properties: TNews) => {
) => ( ) => (
<div <div
key={index} key={index}
className={twMerge('grid gap-3 sm:gap-x-8')} className="grid gap-3 sm:gap-x-8"
> >
<img <img
className={twMerge( className="aspect-[174/100] w-full rounded-md object-cover sm:aspect-[5/4]"
'aspect-[174/100] w-full rounded-md object-cover sm:aspect-[5/4]',
)}
src={featured_image} src={featured_image}
alt={title} alt={title}
/> />
<div <div className="flex flex-col justify-between gap-4">
className={twMerge('flex flex-col justify-between gap-4')}
>
<Tags <Tags
tags={tags} tags={tags}
is_premium={is_premium} is_premium={is_premium}