Skip to content

Commit

Permalink
fix: smooth loop + draggable carousel
Browse files Browse the repository at this point in the history
  • Loading branch information
r3yc0n1c committed Dec 17, 2024
1 parent 3f3d293 commit 66ef649
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 79 deletions.
12 changes: 6 additions & 6 deletions components/Header/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import Heading from '../Typography/heading';
import Paragraph from '../Typography/paragraph';
import Button from '../Buttons/button';
import ReactSlider from '../Slider/slider';
import EmblaCarousel from '../Slider/slider';
import cities from '../../config/city-lists.json';
import Venue from '../Venue/venue';
import Announcement from '../announcement';
Expand All @@ -11,6 +11,10 @@ import { useMediaQuery } from 'react-responsive';

function Header() {
const isMobile = useMediaQuery({ maxWidth: '590px' });
const SLIDES = cities.map((city) => {
return <Venue key={city.name} city={city}/>;
})

return (
<div className='relative'>
<div className='container w-full flex items-center justify-center'>
Expand Down Expand Up @@ -46,11 +50,7 @@ function Header() {
</div>
</div>
<div className='mt-24'>
<ReactSlider>
{cities.map((city) => {
return <Venue key={city.name} city={city}/>;
})}
</ReactSlider>
<EmblaCarousel slides={SLIDES} />
</div>
</div>
);
Expand Down
63 changes: 34 additions & 29 deletions components/Slider/slider.js
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;
81 changes: 38 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"@googleapis/sheets": "^7.0.0",
"axios": "^1.7.4",
"d3": "^7.8.5",
"embla-carousel-auto-scroll": "^8.5.1",
"embla-carousel-react": "^8.5.1",
"lucide-react": "^0.350.0",
"net": "^1.0.2",
"next": "^14.2.10",
Expand All @@ -32,7 +34,6 @@
"react-hot-toast": "^2.4.1",
"react-responsive": "^9.0.0",
"react-select": "^5.7.5",
"react-slick": "^0.29.0",
"react-youtube-embed": "^1.0.3",
"slick-carousel": "^1.8.1"
},
Expand Down
11 changes: 11 additions & 0 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ body {
bottom: 0 !important;
}

.embla {
overflow: hidden;
}
.embla__container {
display: flex;
}
.embla__slide {
flex: 0 0 75%;
min-width: 0;
}

.gradient-bg {
background-image: linear-gradient(225deg, #2DCCFD 9.35%, #AD20E2 88.41%) !important;
-webkit-mask-image: linear-gradient(45deg,#000 25%,rgba(0,0,0,.2) 50%,#000 75%);
Expand Down

0 comments on commit 66ef649

Please sign in to comment.