Before And After Slider
Before And After Slider is a React and Tailwind CSS media component from Bund UI, licensed MIT. Copy it and paste it into your project.
Bund UI
MIT
media
What it is
Before And After Slider is a React and Tailwind CSS media component from Bund UI, licensed MIT. Copy it and paste it into your project.
How to use it
Open it in the catalogue, press copy, and paste it into your project. The prompt you copy carries the code and the rules that tell your agent to reproduce it without changing anything.
Five free copies a month. No card.
No packages beyond the project baseline
Licence
This component comes from Bund UI and is redistributed under the MIT licence, which permits commercial use. The copyright notice travels with the code.
Before And After Slider
"use client";
import { cn } from "@/lib/utils";
import { useState, createContext, useContext } from "react";
import {
motion,
MotionValue,
SpringOptions,
useMotionValue,
useSpring,
useTransform
} from "motion/react";
const ImageComparisonContext = createContext<
| {
sliderPosition: number;
setSliderPosition: (pos: number) => void;
motionSliderPosition: MotionValue<number>;
}
| undefined
>(undefined);
export type ImageComparisonProps = {
children: React.ReactNode;
className?: string;
enableHover?: boolean;
springOptions?: SpringOptions;
};
const DEFAULT_SPRING_OPTIONS = {
bounce: 0,
duration: 0
};
function ImageComparison({
children,
className,
enableHover,
springOptions
}: ImageComparisonProps) {
const [isDragging, setIsDragging] = useState(false);
const motionValue = useMotionValue(50);
const motionSliderPosition = useSpring(motionValue, springOptions ?? DEFAULT_SPRING_OPTIONS);
const [sliderPosition, setSliderPosition] = useState(50);
const handleDrag = (event: React.MouseEvent | React.TouchEvent) => {
if (!isDragging && !enableHover) return;
const containerRect = (event.currentTarget as HTMLElement).getBoundingClientRect();
const x =
"touches" in event
? event.touches[0].clientX - containerRect.left
: (event as React.MouseEvent).clientX - containerRect.left;
const percentage = Math.min(Math.max((x / containerRect.width) * 100, 0), 100);
motionValue.set(percentage);
setSliderPosition(percentage);
};
return (
<ImageComparisonContext.Provider
value={{ sliderPosition, setSliderPosition, motionSliderPosition }}>
<div
className={cn(
"relative overflow-hidden select-none",
enableHover && "cursor-ew-resize",
className
)}
onMouseMove={handleDrag}
onMouseDown={() => !enableHover && setIsDragging(true)}
onMouseUp={() => !enableHover && setIsDragging(false)}
onMouseLeave={() => !enableHover && setIsDragging(false)}
onTouchMove={handleDrag}
onTouchStart={() => !enableHover && setIsDragging(true)}
onTouchEnd={() => !enableHover && setIsDragging(false)}>
{children}
</div>
</ImageComparisonContext.Provider>
);
}
const ImageComparisonImage = ({
className,
alt,
src,
position
}: {
className?: string;
alt: string;
src: string;
position: "left" | "right";
}) => {
const { motionSliderPosition } = useContext(ImageComparisonContext)!;
const leftClipPath = useTransform(motionSliderPosition, (value) => `inset(0 0 0 ${value}%)`);
const rightClipPath = useTransform(
motionSliderPosition,
(value) => `inset(0 ${100 - value}% 0 0)`
);
return (
<motion.img
src={src}
alt={alt}
className={cn("absolute inset-0 h-full w-full object-cover", className)}
style={{
clipPath: position === "left" ? leftClipPath : rightClipPath
}}
/>
);
};
const ImageComparisonSlider = ({
className,
children
}: {
className: string;
children?: React.ReactNode;
}) => {
const { motionSliderPosition } = useContext(ImageComparisonContext)!;
const left = useTransform(motionSliderPosition, (value) => `${value}%`);
return (
<motion.div
className={cn("absolute top-0 bottom-0 w-1 cursor-ew-resize", className)}
style={{
left
}}>
{children}
</motion.div>
);
};
export { ImageComparison, ImageComparisonImage, ImageComparisonSlider };
import { ImageComparison, ImageComparisonImage, ImageComparisonSlider } from "./image-comparison";
export default function ImageComparisonBasic() {
return (
<ImageComparison className="aspect-16/9 w-full rounded-lg" enableHover>
<ImageComparisonImage
className="grayscale"
src="https://images.unsplash.com/photo-1755593574938-6d66d28f8e57?q=80&w=750&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
alt="bundui dark image"
position="left"
/>
<ImageComparisonImage
src="https://images.unsplash.com/photo-1755593574938-6d66d28f8e57?q=80&w=750&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
alt="bundui light image"
position="right"
/>
<ImageComparisonSlider className="w-0.5 bg-white/30 backdrop-blur-xs">
<div className="absolute top-1/2 left-1/2 size-4 -translate-x-1/2 -translate-y-1/2 rounded-full bg-white"></div>
</ImageComparisonSlider>
</ImageComparison>
);
}