-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33eb97a
commit 3075e3b
Showing
8 changed files
with
243 additions
and
171 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect, useState, useRef, RefObject } from 'react'; | ||
|
||
const useAutoplayVisibility = (): [boolean, RefObject<HTMLDivElement>] => { | ||
const [autoplayEnabled, setAutoplayEnabled] = useState(true); | ||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver( | ||
([entry]) => { | ||
setAutoplayEnabled(entry.isIntersecting); | ||
}, | ||
{ threshold: 0.5 } | ||
); | ||
|
||
if (ref.current) { | ||
observer.observe(ref.current); | ||
} | ||
|
||
return () => { | ||
if (ref.current) { | ||
observer.unobserve(ref.current); | ||
} | ||
}; | ||
}, [ref]); | ||
|
||
return [autoplayEnabled, ref]; | ||
}; | ||
|
||
export default useAutoplayVisibility; |
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
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,80 @@ | ||
import Image from "next/image"; | ||
import { Autoplay, EffectFade } from "swiper/modules"; | ||
import { Swiper, SwiperSlide } from "swiper/react"; | ||
import SwiperCore from "swiper"; | ||
|
||
import "swiper/css"; | ||
import "swiper/css/effect-fade"; | ||
|
||
import { useEffect, useRef, useState } from "react"; | ||
import useAutoplayVisibility from "./hook"; | ||
import { useAppSelector } from "@/store/store"; | ||
|
||
type SwiperCore = any; | ||
|
||
const SwiperComponent = ({ images }: { images: any }) => { | ||
const [autoplayEnabled, ref] = useAutoplayVisibility(); | ||
const screenHeight = useAppSelector((state) => state.app.screenHeight); | ||
|
||
const swiperRef = useRef<SwiperCore | null>(null); | ||
|
||
useEffect(() => { | ||
if (swiperRef.current) { | ||
if (autoplayEnabled) { | ||
swiperRef.current.autoplay.start(); | ||
} else { | ||
swiperRef.current.autoplay.stop(); | ||
} | ||
} | ||
}, [autoplayEnabled]); | ||
|
||
return ( | ||
<div ref={ref} className={`${autoplayEnabled}`}> | ||
<Swiper | ||
onSwiper={(swiper) => { | ||
swiperRef.current = swiper; | ||
if (autoplayEnabled) { | ||
swiper.autoplay.start(); | ||
} | ||
}} | ||
modules={[Autoplay, EffectFade]} | ||
slidesPerView={1} | ||
loop={true} | ||
autoplay={{ | ||
delay: 3000, | ||
disableOnInteraction: false, | ||
}} | ||
effect="fade" | ||
className="w-full" | ||
style={{ | ||
height: screenHeight != 0 ? screenHeight + "px" : "100vh", | ||
}} | ||
> | ||
{images.map((imageUrl: any, index: any) => ( | ||
<> | ||
<SwiperSlide key={"mobile-image-" + index}> | ||
<div | ||
className="h-full" | ||
style={{ | ||
backgroundImage: `url(${imageUrl})`, | ||
}} | ||
> | ||
<div className="flex h-full w-full items-center bg-black bg-opacity-20 backdrop-blur-sm"> | ||
<Image | ||
src={imageUrl} | ||
height={100} | ||
width={100} | ||
alt="menu-dish-image" | ||
className="w-full" | ||
/> | ||
</div> | ||
</div> | ||
</SwiperSlide> | ||
</> | ||
))} | ||
</Swiper> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SwiperComponent; |
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
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
Oops, something went wrong.