-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: smooth loop + draggable carousel
- Loading branch information
Showing
5 changed files
with
91 additions
and
79 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
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,39 +1,44 @@ | ||
import Slider from 'react-slick'; | ||
import Arrow from '../illustration/arrow'; | ||
import React, { useEffect, useState, useRef } from 'react'; | ||
import React, { useCallback, useEffect, useState } from 'react' | ||
import useEmblaCarousel from 'embla-carousel-react' | ||
import AutoScroll from 'embla-carousel-auto-scroll' | ||
import { useMediaQuery } from 'react-responsive'; | ||
|
||
function ReactSlider({ children }) { | ||
|
||
function EmblaCarousel({ slides }) { | ||
const isMobile = useMediaQuery({ maxWidth: '590px' }); | ||
const [slides, setSlides] = useState(1); | ||
|
||
const scrollOptions = { | ||
playOnInit: true, | ||
stopOnInteraction: false, | ||
stopOnMouseEnter: true, | ||
startDelay: 200, | ||
speed: 1 | ||
} | ||
|
||
useEffect(() => { | ||
if (isMobile) { | ||
setSlides(1); | ||
} | ||
}, [isMobile]); | ||
const slider = useRef(null); | ||
const settings = { | ||
slidesToScroll: slides, | ||
infinite: true, | ||
autoplay: true, | ||
speed: 10000, | ||
autoplaySpeed: 0, | ||
centerMode: true, | ||
cssEase: 'linear', | ||
variableWidth: isMobile ? false : true, | ||
arrows: false, | ||
}; | ||
const options = { | ||
loop: true, | ||
dragFree: true | ||
} | ||
|
||
const [emblaRef] = useEmblaCarousel({ ...options }, [ | ||
AutoScroll({ ...scrollOptions }) | ||
]); | ||
|
||
return ( | ||
<> | ||
{ children.length > 4 || isMobile ? | ||
(<Slider ref={slider} {...settings}> | ||
{children} | ||
</Slider>):( | ||
<div className='flex m-2 justify-center'>{children}</div>)} | ||
</> | ||
{slides.length > 4 || isMobile ? ( | ||
<div className="embla" ref={emblaRef}> | ||
<div className="embla__container"> | ||
{slides.map((item, idx) => ( | ||
<div className="embla__slide" key={`embla__slide_${idx}`}>{item}</div> | ||
))} | ||
</div> | ||
</div> | ||
) : ( | ||
<div className='flex m-2 justify-center'>{slides}</div> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default ReactSlider; | ||
export default EmblaCarousel; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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