Zuma32

React Swiper With custom nav

Jul 29th, 2025 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.40 KB | Source Code | 0 0
  1. import { car } from "../assets/images/imageIndex";
  2. import { Swiper, SwiperSlide } from "swiper/react";
  3. import { Navigation, Pagination, A11y, Parallax, Scrollbar } from "swiper/modules";
  4. import "swiper/css";
  5. import "swiper/css/navigation";
  6. import { motion } from "framer-motion";
  7. import { div } from "framer-motion/client";
  8. import { useEffect, useRef, useState } from "react";
  9. import { ChevronLeft, ChevronRight } from 'lucide-react';
  10.  
  11.  
  12. function FeaturedVehicle({ ref }) {
  13.   const prevRef = useRef(null);
  14.   const nextRef = useRef(null);
  15.   const [swiperInstance, setSwiperInstance] = useState(null);
  16.   const [isBeginning, setIsBeginning] = useState(true);
  17.   const [isEnd, setIsEnd] = useState(false);
  18.  
  19.   useEffect(() => {
  20.     // check if swiperInstance, navigation, prevref and nextref current is not null
  21.     if(swiperInstance && swiperInstance.params.navigation && prevRef.current && nextRef.current) {
  22.         swiperInstance.params.navigation.prevEl = prevRef.current;
  23.         swiperInstance.params.navigation.nextEl = nextRef.current;
  24.  
  25.         // refresh the navigation system
  26.         swiperInstance.navigation.destroy(); // destroy existing nav
  27.         swiperInstance.navigation.init();    // reinit
  28.         swiperInstance.navigation.update();  // update with new refs
  29.  
  30.         if (swiperInstance) {
  31.           // update swiper begining and end
  32.           const updateEdgeStates = () => {
  33.               setIsBeginning(swiperInstance.isBeginning);
  34.               setIsEnd(swiperInstance.isEnd);
  35.           }
  36.           updateEdgeStates();
  37.  
  38.           swiperInstance.on("slideChange", updateEdgeStates);
  39.  
  40.           cleanup = () => {
  41.             swiperInstance.off("slideChange", updateEdgeStates);
  42.           }
  43.         }
  44.     }
  45.  
  46.     // when components unmounts run cleanup
  47.     return cleanup;
  48.   }, [swiperInstance])
  49.  
  50.   return (
  51.     <section
  52.       className="py-10 md:py-20 px-2 sm:px-4 md:px-12 lg:px-20 bg-gradient-to-r from-yellow-400 to-yellow-500 w-full"
  53.       ref={ref}
  54.     >
  55.       <h2 className="text-2xl sm:text-3xl md:text-4xl font-extrabold text-center mb-3 md:mb-4 text-gray-900 font-open-sans">
  56.         Featured Vehicles
  57.       </h2>
  58.       <p className="text-center text-gray-700 mb-6 md:mb-12 max-w-2xl mx-auto text-base sm:text-lg">
  59.         Explore our range of vehicles carefully curated for performance, style, and reliability.
  60.       </p>
  61.  
  62.       {/* <div className="flex justify-between mt-4">
  63.         <button ref={prevRef} className="text-white bg-gray-800 px-4 py-2 rounded">
  64.           Prev
  65.         </button>
  66.         <button ref={nextRef} className="text-white bg-gray-800 px-4 py-2 rounded">
  67.           Next
  68.         </button>
  69.       </div> */}
  70.  
  71.       <div className="relative">
  72.         {(isBeginning) ? "true" : "false"}
  73.         <button ref={prevRef} disabled={isBeginning} className={`absolute left-2 ${(isBeginning ) ? "bg-gray-300/20":"bg-gray-300"}  top-1/2 z-10 cursor-pointer  p-2 rounded-full`}>
  74.           <ChevronLeft />
  75.         </button>
  76.         <button ref={nextRef} disabled={isEnd}  className={`absolute right-2 ${(isEnd ) ? "bg-gray-300/20":"bg-gray-300"} top-1/2 z-10 cursor-pointer bg-gray-300 p-2 rounded-full`}>
  77.           <ChevronRight />
  78.         </button>
  79.  
  80.         <Swiper
  81.             className="max-w-7xl mx-auto w-full"
  82.             spaceBetween={10}
  83.             modules={[Navigation, A11y]}
  84.             onSwiper={setSwiperInstance}
  85.             observer={true}
  86.             observeParents={true}
  87.             breakpoints={{
  88.               640: {
  89.                 slidesPerView: 1
  90.               },
  91.               768: {
  92.                 slidesPerView: 2,
  93.               },
  94.               1024: {
  95.                 slidesPerView: 3,
  96.               },
  97.             }}
  98.       >
  99.         {vehicles.map((vehicle, idx) => (
  100.           <SwiperSlide key={idx}>
  101.             <div className=" shadow-2xl bg-gray-900 mx-auto  ">
  102.               <img src={vehicle.image} alt={vehicle.name} />
  103.               <div className="flex flex-col p-10">
  104.                   {
  105.                     vehicle.features.map((feature, idx) => {
  106.                     return (
  107.                         <span className="text-white"> {feature }</span>
  108.                       )
  109.                     })
  110.                   }
  111.               </div>
  112.               {}
  113.             </div>
  114.           </SwiperSlide>
  115.         ))}
  116.       </Swiper>
  117.       </div>
  118.      
  119.    
  120.     </section>
  121.   );
  122. }
  123.  
  124. export default FeaturedVehicle;
Advertisement
Add Comment
Please, Sign In to add comment