Home/Components/Cards/Image Comparison Slider React
Code · Live Preview
Componenttsx
// src/components/ImageComparison/ImageComparison.tsx
import * as Slider from "react-compare-slider/components";
import { useReactCompareSlider } from "react-compare-slider/hooks";
// import UserImage1 from "../../assets/images/sadie-sink-1.jpg";
// import UserImage2 from "../../assets/images/sadie-sink-2.jpeg";

export const ImageComparison = () => {
  const sliderProps = useReactCompareSlider({ portrait: false });

  return (
    <Slider.Provider {...sliderProps}>
      <Slider.Root>
        <Slider.Item item="itemOne">
          <Slider.Image
            src={
              "https://res.cloudinary.com/dyswu0gin/image/upload/v1784518411/sadie-sink-elle-3840x2160-24726_xgyh9z.jpg"
            }
            alt="Image one"
          />
        </Slider.Item>
        <Slider.Item item="itemTwo">
          <Slider.Image
            src={
              "https://res.cloudinary.com/dyswu0gin/image/upload/v1784518410/thumb-1920-1331868_rmej2i.jpg"
            }
            alt="Image two"
          />
        </Slider.Item>
        <Slider.HandleRoot>
          <Slider.Handle />
        </Slider.HandleRoot>
      </Slider.Root>
    </Slider.Provider>
  );
};
DesktopSandpack

Component details

Overview, install command, dependencies, and project links.

An interactive before/after image comparison slider built with React and the react-compare-slider library. Drag the handle left or right to reveal and compare two images side by side. Supports both horizontal and portrait (vertical) modes - switch between them by changing portrait: false to portrait: true in the useReactCompareSlider hook. Built with a clean component-based API from react-compare-slider that separates the provider, root, items, images, and handle into individual composable pieces - easy to style and extend. Drop in your own images by swapping the imports in the component. Zero custom slider logic needed - the library handles all drag, touch, and keyboard interactions out of the box.

Install

terminal
npm
npm install react-compare-slider
Added Jul 20, 20261 comment

Discussion

Feedback, implementation tips, and usage notes.

1 thread

Comments

Sign in to join the conversation