Skip to content

Instantly share code, notes, and snippets.

@leonardof02
Last active April 25, 2024 20:09
Show Gist options
  • Select an option

  • Save leonardof02/4e6babe5bba5cfbfdcc0ec9107bbb2db to your computer and use it in GitHub Desktop.

Select an option

Save leonardof02/4e6babe5bba5cfbfdcc0ec9107bbb2db to your computer and use it in GitHub Desktop.
Slider / Carrousel Solutions
const breakpoints = {
"(min-width: 1241px)": {
slides: {
perView: 3,
},
},
"(max-width: 1240px)": {
slides: {
perView: 3,
},
},
"(max-width: 1068px)": {
slides: {
perView: 2,
},
},
"(max-width: 709px)": {
slides: {
perView: 1,
},
},
};
export { breakpoints };
"use client";
import React, { useEffect, useState } from "react";
import "keen-slider/keen-slider.min.css";
import { useKeenSlider } from "keen-slider/react";
import OpinionCard from "./OpinionCard";
import { breakpoints } from "./OpinionSlider.breakpoints";
import { Box, Flex, Stack } from "@chakra-ui/react";
export default function OpinionSlider() {
const [sliderRef, instanceRef] = useKeenSlider({
slideChanged() {
const changedIndex = instanceRef.current?.track.details.rel ?? 0;
setCurrentIndex(changedIndex);
},
breakpoints,
});
const [currentIndex, setCurrentIndex] = useState(0);
const [sliderLenght, setSliderLenght] = useState(0);
useEffect(() => {
const lenght = (instanceRef.current?.track.details.maxIdx ?? 0) + 1;
setSliderLenght(lenght);
}, [instanceRef.current?.track.details.maxIdx, instanceRef]);
return (
<Stack spacing={"40px"}>
<Box ref={sliderRef} className="keen-slider">
{Array.from({ length: 4 }).map((_, key) => (
<Box className="keen-slider__slide" key={key}>
<OpinionCard />
</Box>
))}
</Box>
<Flex width={"full"} justifyContent={"center"} alignItems={"center"} gap={"10px"}>
{Array.from({ length: sliderLenght }).map((_, index) => (
<Box
width={"10px"}
height={"10px"}
rounded={"full"}
cursor={"pointer"}
key={index}
backgroundColor={index !== currentIndex ? "white" : "brand"}
onClick={() => {
instanceRef.current?.moveToIdx(index);
}}
></Box>
))}
</Flex>
</Stack>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment