-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update Carousel loading behavior in Akash Insiders page (#453)
fix: update Carousel loading behavior in Akash Insiders page
- Loading branch information
1 parent
73213b2
commit 32b40c1
Showing
7 changed files
with
183 additions
and
90 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
src/components/community-pages/custom-carousel/Carousel.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
.carousel-container { | ||
position: relative; | ||
width: 100%; | ||
overflow: hidden; | ||
} | ||
|
||
.carousel-track { | ||
display: flex; | ||
width: fit-content; | ||
will-change: transform; | ||
} | ||
|
||
@keyframes smooth-scroll { | ||
from { | ||
transform: translateX(0); | ||
} | ||
to { | ||
transform: translateX(calc(-1 * var(--scroll-width))); | ||
} | ||
} | ||
|
||
.animate-scroll { | ||
animation: smooth-scroll var(--animation-duration) linear infinite; | ||
animation-fill-mode: forwards; | ||
} | ||
|
||
.carousel-track { | ||
width: fit-content; | ||
will-change: transform; | ||
} | ||
|
||
.carousel-item img { | ||
max-width: 100%; | ||
height: auto; | ||
} | ||
|
||
@media (min-width: 768px) { | ||
.carousel-item img { | ||
height: 100%; | ||
object-fit: cover; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/components/community-pages/custom-carousel/Carousel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from "react"; | ||
import { CarouselItem } from "./CarouselItem"; | ||
import { useCarousel } from "./useCarousel"; | ||
import type { CarouselProps } from "./types"; | ||
import "./Carousel.css"; | ||
|
||
export const Carousel: React.FC<CarouselProps> = ({ | ||
images, | ||
speed = 50, | ||
gap = 20, | ||
}) => { | ||
const { containerRef, trackRef, isLoaded } = useCarousel(speed); | ||
|
||
const displayImages = [...images, ...images, ...images]; | ||
|
||
return ( | ||
<div | ||
ref={containerRef} | ||
className="carousel-container w-full overflow-hidden bg-background" | ||
aria-label="Image Carousel" | ||
> | ||
<div | ||
ref={trackRef} | ||
className="carousel-track flex" | ||
style={{ gap: `${gap}px` }} | ||
> | ||
{displayImages.map((image, index) => ( | ||
<CarouselItem | ||
key={`${image.src}-${index}`} | ||
src={image.src} | ||
alt={image.alt} | ||
isLoaded={isLoaded} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; |
31 changes: 31 additions & 0 deletions
31
src/components/community-pages/custom-carousel/CarouselItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
|
||
interface CarouselItemProps { | ||
src: string; | ||
alt: string; | ||
isLoaded: boolean; | ||
} | ||
|
||
export const CarouselItem: React.FC<CarouselItemProps> = ({ src, alt, isLoaded }) => ( | ||
<div className="carousel-item mx-3 flex-shrink-0 md:mx-5 lg:mx-[28px]"> | ||
<img | ||
src={src} | ||
alt={alt} | ||
width={260} | ||
height={380} | ||
className={` | ||
aspect-auto | ||
max-h-[14rem] | ||
w-auto | ||
md:h-full | ||
md:max-h-[unset] | ||
md:object-cover | ||
${isLoaded ? 'opacity-100' : 'opacity-0'} | ||
transition-opacity | ||
duration-300 | ||
`} | ||
draggable={false} | ||
loading="lazy" | ||
/> | ||
</div> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export interface CarouselImage { | ||
src: string; | ||
alt: string; | ||
} | ||
|
||
export interface CarouselProps { | ||
images: CarouselImage[]; | ||
speed?: number; | ||
gap?: number; | ||
} |
50 changes: 50 additions & 0 deletions
50
src/components/community-pages/custom-carousel/useCarousel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
export const useCarousel = (speed = 50) => { | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const trackRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
const track = trackRef.current; | ||
if (!track) return; | ||
|
||
const images = Array.from(track.getElementsByTagName("img")); | ||
Promise.all( | ||
images.map( | ||
(img) => | ||
new Promise((resolve) => { | ||
if (img.complete) resolve(null); | ||
img.onload = () => resolve(null); | ||
img.onerror = () => resolve(null); | ||
}), | ||
), | ||
).then(() => { | ||
setIsLoaded(true); | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!isLoaded) return; | ||
|
||
const track = trackRef.current; | ||
if (!track) return; | ||
|
||
const totalWidth = track.scrollWidth; | ||
const duration = totalWidth / speed; | ||
|
||
track.style.setProperty("--scroll-width", `${totalWidth}px`); | ||
track.style.setProperty("--animation-duration", `${duration}s`); | ||
track.classList.add("animate-scroll"); | ||
|
||
return () => { | ||
track.classList.remove("animate-scroll"); | ||
}; | ||
}, [isLoaded, speed]); | ||
|
||
return { | ||
containerRef, | ||
trackRef, | ||
isLoaded, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters