image-comparison
jolyui-ui/image-comparisonFreeComponent
jolyui/ui publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by jolyui/ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://jolyui.dev/r/image-comparison.jsonSource
1import { GripHorizontal, GripVertical } from "lucide-react";2import { motion, useMotionValue, useTransform } from "motion/react";3import type React from "react";4import { useCallback, useEffect, useRef, useState } from "react";5import { cn } from "@/lib/utils";67// Basic Comparison Slider8interface ImageComparisonProps {9 beforeImage: string;10 afterImage: string;11 beforeLabel?: string;12 afterLabel?: string;13 className?: string;14 initialPosition?: number;15 orientation?: "horizontal" | "vertical";16 showLabels?: boolean;17 sliderColor?: string;18}1920export function ImageComparison({21 beforeImage,22 afterImage,23 beforeLabel = "Before",24 afterLabel = "After",25 className,26 initialPosition = 50,27 orientation = "horizontal",28 showLabels = true,29 sliderColor = "hsl(var(--background))",30}: ImageComparisonProps) {31 const containerRef = useRef<HTMLDivElement>(null);32 const [position, setPosition] = useState(initialPosition);33 const [isDragging, setIsDragging] = useState(false);3435 const handleMove = useCallback(36 (clientX: number, clientY: number) => {37 if (!containerRef.current) return;3839 const rect = containerRef.current.getBoundingClientRect();40 let newPosition: number;4142 if (orientation === "horizontal") {43 newPosition = ((clientX - rect.left) / rect.width) * 100;44 } else {45 newPosition = ((clientY - rect.top) / rect.height) * 100;46 }4748 setPosition(Math.max(0, Math.min(100, newPosition)));49 },50 [orientation],51 );5253 const handleMouseDown = (e: React.MouseEvent) => {54 e.preventDefault();55 setIsDragging(true);56 };5758 const handleTouchStart = () => {59 setIsDragging(true);60 };6162 useEffect(() => {63 const handleMouseMove = (e: MouseEvent) => {64 if (isDragging) {65 handleMove(e.clientX, e.clientY);66 }67 };6869 const handleTouchMove = (e: TouchEvent) => {70 if (isDragging && e.touches[0]) {71 handleMove(e.touches[0].clientX, e.touches[0].clientY);72 }73 };7475 const handleEnd = () => {76 setIsDragging(false);77 };7879 if (isDragging) {80 window.addEventListener("mousemove", handleMouseMove);81 window.addEventListener("mouseup", handleEnd);82 window.addEventListener("touchmove", handleTouchMove);83 window.addEventListener("touchend", handleEnd);84 }8586 return () => {87 window.removeEventListener("mousemove", handleMouseMove);88 window.removeEventListener("mouseup", handleEnd);89// … truncated
What it pulls in
npm packages
Files it writes
More from jolyui/ui
All 199 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| ai-prompt-boxai-prompt-box | jolyui/ui | Components | Free | 4 deps | |
| animated-beamanimated-beam | jolyui/ui | Components | Free | 1 dep | |
| animated-tableanimated-table | jolyui/ui | Components | Free | 2 deps | |
| animated-theme-toggleanimated-theme-toggle | jolyui/ui | Components | Free | 2 deps | |
| animated-toastanimated-toast | jolyui/ui | Components | Free | 2 deps | |
| animated-tooltipanimated-tooltip | jolyui/ui | Components | Free | 1 dep | |
| bento-gridbento-grid | jolyui/ui | Components | Free | no deps | |
| buttonbutton | jolyui/ui | Components | Free | 3 deps |
