Image Carousel

Image Carousel

Image Carousel

This guide explains how to edit and use the ImageCarousel component for your project. You’ll learn how to:

  1. Add or replace images in the carousel.

  2. Customize navigation behavior (arrows and dots).

  3. Adjust animations and transitions for a personalized experience.

  4. Test and integrate the carousel into your website.

Tutorial

Step 1: Add or Replace Images

Locate the images Array

The carousel images are stored in the images array:

const images = [
  "/images/Image-A.webp",
  "/images/Image-B.webp",
  "/images/Image-C.webp",
  "/images/Image-D.webp",
  "/images/Image-E.webp",
  "/images/Image-F.webp",
  "/images/Image-G.webp",
  "/images/Image-H.webp",
  "/images/Image-I.webp",
]

  • Replace Images: Update the file paths (/images/Image-X.webp) with the paths to your own images.

  • Add Images: Add additional file paths to the array.

  • Remove Images: Delete unwanted file paths.

Example Update:

const images = [
  "/images/photo1.jpg",
  "/images/photo2.jpg",
  "/images/photo3.jpg",
  "/images/photo4.jpg",
]

Step 2: Customize Navigation

Arrow Buttons

The ArrowLeftIcon and ArrowRightIcon components handle navigation:

<ArrowLeftIcon
  width="24"
  height="24"
  className="bg-neutral-100 hover:bg-neutral-200 rounded-full h-8 w-8 p-2 transition-all ease-in-out duration-300"
/>
  • Customize Appearance: Modify the className for different colors, sizes, or hover effects.

Example Update:

  • Change Arrow Size: Update width and height.

Example Update:


Dots (Pagination)

The dots are generated dynamically in this block:

<motion.div
  layout
  transition={{ duration: 0.3 }}
  style={{
    width: idx == current ? "24px" : "8px",
    backgroundColor:
      idx === current ? "rgb(255,255,255)" : "rgb(82, 82, 82)",
    borderRadius: 9999,
  }}
  className={`h-2 ${idx === current ? "bg-white" : "bg-zinc-600"}`}
></motion.div>
  • Edit Size: Change width for the active dot and 8px for inactive dots.

  • Change Colors: Update backgroundColor for active (rgb(255,255,255)) and inactive (rgb(82, 82, 82)) states.

Example Update:


Step 3: Adjust Animations and Transitions

Image Transition

The motion effect for sliding images is defined here:

<motion.div
  animate={{ x: `calc(-${current * 100}% - ${current}rem)` }}
  transition={{ duration: 0.7, ease: [0.32, 0.72, 0, 1] }}
>
  • Adjust Speed (duration): Increase or decrease 0.7 to make the animation slower or faster.

  • Change Easing: Modify [0.32, 0.72, 0, 1] to experiment with different easing curves (e.g., easeInOut or linear).

Example Update:

Step 4: Accessibility Improvements

  1. Add Alt Text: Update alt={image} with meaningful descriptions of your images. Replace image with descriptive text.

    • Example: "Photo of a beach during sunset".

  2. Keyboard Navigation: Add tabIndex="0" to the arrow buttons for better accessibility.

Example Update:

<button onClick={onPrevClick} tabIndex="0">

Step 5: Testing Your Carousel

  1. Preview Locally: Use npm run dev to test your carousel.

  2. Responsive Check:

    • Resize your browser to confirm the carousel adjusts properly across devices.

    • Verify images are centered and fully visible on all screen sizes.

  3. Test Navigation:

    • Use arrow buttons to move between images.

    • Test the dots to jump directly to a specific image.

Step 6: Best Practices

  1. Image Optimization:

    • Use .webp or compressed formats for fast loading.

    • Ensure images have consistent dimensions to avoid layout shifts.

  2. Smooth Animations:

    • Test easing and duration values for fluid transitions.

    • Avoid overly fast or slow animations that might frustrate users.

  3. Accessibility:

    • Provide clear alt descriptions.

    • Ensure arrow buttons and dots are keyboard-navigable.

Example Updates for Your Project

Custom Image List

const images = [
  "/assets/product1.webp",
  "/assets/product2.webp",
  "/assets/product3.webp",
  "/assets/product4.webp",
]

Button Customization

<ArrowLeftIcon
  width="28"
  height="28"
  className="bg-blue-600 hover:bg-blue-800 rounded-full h-10 w-10 p-2"
/>
<ArrowRightIcon
  width="28"
  height="28"
  className="bg-blue-600 hover:bg-blue-800 rounded-full h-10 w-10 p-2"
/>