Skip to content

Instantly share code, notes, and snippets.

@paolaozv
Created November 11, 2020 16:25
Show Gist options
  • Select an option

  • Save paolaozv/c5a8d3e274125bb4b2201df72d4b2af4 to your computer and use it in GitHub Desktop.

Select an option

Save paolaozv/c5a8d3e274125bb4b2201df72d4b2af4 to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from 'react';
import { FiChevronRight, FiChevronLeft } from 'react-icons/fi';
import { FaQuoteRight } from 'react-icons/fa';
import data from './data';
function Slider() {
const [people, setPeople] = useState(data);
const [index, setIndex] = useState(0);
const [isOverPrevBtn, setIsOverPrevBtn] = useState(false);
const [isOverNextBtn, setIsOverNextBtn] = useState(false);
useEffect(() => {
const lastIndex = people.length - 1;
if (index < 0) {
setIndex(lastIndex);
}
if (index > lastIndex) {
setIndex(0);
}
}, [index, people]);
useEffect(() => {
if (isOverPrevBtn) {
const interval = setInterval(() => {
setIndex((prevIndex) => {
return prevIndex - 1;
});
}, 1000);
return () => clearInterval(interval);
}
}, [isOverPrevBtn]);
useEffect(() => {
if (isOverNextBtn) {
const interval = setInterval(() => {
setIndex((prevIndex) => {
return prevIndex + 1;
});
}, 1000);
return () => clearInterval(interval);
}
}, [isOverNextBtn]);
return (
<section className="section">
<div className="title">
<h2>
<span>/</span> reviews
</h2>
</div>
<section className="section-center">
{people.map((person, personIndex) => {
const { id, name, image, title, quote } = person;
let position = 'nextSlide';
if (personIndex === index) {
position = 'activeSlide';
}
if (personIndex === index - 1 || (index === 0 && personIndex === people.length - 1)) {
position = 'lastSlide';
}
return (
<article key={id} className={position}>
<img src={image} alt={name} className='person-img' />
<h4>{name}</h4>
<p className="title">
{title}
</p>
<p className="text">
{quote}
</p>
<FaQuoteRight className='icon' />
</article>
);
})}
<button className="prev" onMouseEnter={() => setIsOverPrevBtn(true)} onMouseLeave={() => setIsOverPrevBtn(false)}>
<FiChevronLeft />
</button>
<button className="next" onMouseEnter={() => setIsOverNextBtn(true)} onMouseLeave={() => setIsOverNextBtn(false)}>
<FiChevronRight />
</button>
</section>
</section>
);
};
export default Slider;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment