-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: lazy load images and hide download button on vertical mobile view
- Loading branch information
Showing
2 changed files
with
133 additions
and
123 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,68 +1,68 @@ | ||
import React, { useState, useEffect, useCallback } from 'react' | ||
import useEmblaCarousel from 'embla-carousel-react' | ||
import { Thumb } from './EmblaCarouselThumbsButton' | ||
|
||
const EmblaCarousel = (props) => { | ||
const { slides, options, plugins } = props | ||
const [selectedIndex, setSelectedIndex] = useState(0) | ||
const [emblaMainRef, emblaMainApi] = useEmblaCarousel(options, plugins) | ||
const [emblaThumbsRef, emblaThumbsApi] = useEmblaCarousel({ | ||
containScroll: 'keepSnaps', | ||
dragFree: true, | ||
} | ||
) | ||
|
||
const onThumbClick = useCallback( | ||
(index) => { | ||
if (!emblaMainApi || !emblaThumbsApi) return | ||
emblaMainApi.scrollTo(index) | ||
}, | ||
[emblaMainApi, emblaThumbsApi] | ||
) | ||
|
||
const onSelect = useCallback(() => { | ||
if (!emblaMainApi || !emblaThumbsApi) return | ||
setSelectedIndex(emblaMainApi.selectedScrollSnap()) | ||
emblaThumbsApi.scrollTo(emblaMainApi.selectedScrollSnap()) | ||
}, [emblaMainApi, emblaThumbsApi, setSelectedIndex]) | ||
|
||
useEffect(() => { | ||
if (!emblaMainApi) return | ||
onSelect() | ||
|
||
emblaMainApi.on('select', onSelect).on('reInit', onSelect) | ||
}, [emblaMainApi, onSelect]) | ||
|
||
return ( | ||
<div className="embla"> | ||
<div className="embla__viewport lg:mb-4" ref={emblaMainRef}> | ||
<div className="embla__container"> | ||
{slides.map((slide, index) => ( | ||
<div key={index} className="embla__slide space-y-2"> | ||
<img src={slide.image.src} alt="qsv pro feature demo GIF" className={index === slides.length - 1 ? `border-4 rounded h-[35rem] mx-auto border-zinc-600` : `border-4 rounded border-zinc-600`} /> | ||
<h2 className="text-2xl font-bold pt-4 text-[#F2F2F2]">{slide.title}</h2> | ||
<p className="text-lg text-center text-[#F2F2F2]">{slide.description}</p> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
|
||
<div className="embla-thumbs"> | ||
<div className="embla-thumbs__viewport" ref={emblaThumbsRef}> | ||
<div className="embla-thumbs__container flex gap-2 w-full justify-evenly"> | ||
{slides.map((slide, index) => ( | ||
<Thumb | ||
key={index} | ||
onClick={() => onThumbClick(index)} | ||
selected={index === selectedIndex} | ||
index={slide} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default EmblaCarousel | ||
import React, { useState, useEffect, useCallback } from 'react' | ||
import useEmblaCarousel from 'embla-carousel-react' | ||
import { Thumb } from './EmblaCarouselThumbsButton' | ||
|
||
const EmblaCarousel = (props) => { | ||
const { slides, options, plugins } = props | ||
const [selectedIndex, setSelectedIndex] = useState(0) | ||
const [emblaMainRef, emblaMainApi] = useEmblaCarousel(options, plugins) | ||
const [emblaThumbsRef, emblaThumbsApi] = useEmblaCarousel({ | ||
containScroll: 'keepSnaps', | ||
dragFree: true, | ||
} | ||
) | ||
|
||
const onThumbClick = useCallback( | ||
(index) => { | ||
if (!emblaMainApi || !emblaThumbsApi) return | ||
emblaMainApi.scrollTo(index) | ||
}, | ||
[emblaMainApi, emblaThumbsApi] | ||
) | ||
|
||
const onSelect = useCallback(() => { | ||
if (!emblaMainApi || !emblaThumbsApi) return | ||
setSelectedIndex(emblaMainApi.selectedScrollSnap()) | ||
emblaThumbsApi.scrollTo(emblaMainApi.selectedScrollSnap()) | ||
}, [emblaMainApi, emblaThumbsApi, setSelectedIndex]) | ||
|
||
useEffect(() => { | ||
if (!emblaMainApi) return | ||
onSelect() | ||
|
||
emblaMainApi.on('select', onSelect).on('reInit', onSelect) | ||
}, [emblaMainApi, onSelect]) | ||
|
||
return ( | ||
<div className="embla"> | ||
<div className="embla__viewport lg:mb-4" ref={emblaMainRef}> | ||
<div className="embla__container"> | ||
{slides.map((slide, index) => ( | ||
<div key={index} className="embla__slide space-y-2"> | ||
<img decoding="async" loading="lazy" src={slide.image.src} alt="qsv pro feature demo GIF" className={index === slides.length - 1 ? `border-4 rounded h-[35rem] mx-auto border-zinc-600` : `border-4 rounded border-zinc-600`} /> | ||
<h2 className="text-2xl font-bold pt-4 text-[#F2F2F2]">{slide.title}</h2> | ||
<p className="text-lg text-center text-[#F2F2F2]">{slide.description}</p> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
|
||
<div className="embla-thumbs"> | ||
<div className="embla-thumbs__viewport" ref={emblaThumbsRef}> | ||
<div className="embla-thumbs__container flex gap-2 w-full justify-evenly"> | ||
{slides.map((slide, index) => ( | ||
<Thumb | ||
key={index} | ||
onClick={() => onThumbClick(index)} | ||
selected={index === selectedIndex} | ||
index={slide} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default EmblaCarousel |
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