Image Comparison
nyxui/image-comparisonFreeComponent
A image comparison component that allows users to compare images.
Live preview
live · rendered by the registry
Rendered by shadcn/ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://nyxui.com/r/image-comparison.jsonSource
1"use client";23import { cn } from "@/lib/utils";4import {5 useState,6 createContext,7 useContext,8 useEffect,9 ReactNode,10 useRef,11} from "react";12import {13 motion,14 MotionValue,15 useMotionValue,16 useSpring,17 useTransform,18 AnimationOptions,19} from "motion/react";2021const SliderContext = createContext<22 | {23 position: number;24 setPosition: (pos: number) => void;25 motionPosition: MotionValue<number>;26 orientation: "horizontal" | "vertical";27 isDragging: boolean;28 contentDimensions: { width: number; height: number } | null;29 }30 | undefined31>(undefined);3233export type ContrastSliderProps = {34 children: React.ReactNode;35 className?: string;36 hoverControl?: boolean;37 orientation?: "horizontal" | "vertical";38 defaultPosition?: number;39 animationConfig?: Partial<AnimationOptions>;40 dividerColor?: string;41 constrainToContent?: boolean;42};4344const DEFAULT_ANIMATION_CONFIG = {45 damping: 15,46 stiffness: 400,47 mass: 0.4,48};4950function ImageSlider({51 children,52 className,53 hoverControl = false,54 orientation = "horizontal",55 defaultPosition = 50,56 animationConfig,57 dividerColor,58 constrainToContent = false,59}: ContrastSliderProps) {60 const [isActive, setIsActive] = useState(false);61 const baseMotion = useMotionValue(defaultPosition);62 const springMotion = useSpring(63 baseMotion,64 animationConfig ?? DEFAULT_ANIMATION_CONFIG,65 );66 const [position, setPosition] = useState(defaultPosition);67 const [contentDimensions, setContentDimensions] = useState<{68 width: number;69 height: number;70 } | null>(null);71 const containerRef = useRef<HTMLDivElement>(null);7273 const motionToUse = hoverControl ? baseMotion : springMotion;7475 useEffect(() => {76 baseMotion.set(defaultPosition);77 setPosition(defaultPosition);78 }, [defaultPosition, baseMotion]);7980 useEffect(() => {81 if (containerRef.current) {82 const updateDimensions = () => {83 const container = containerRef.current;84 if (!container) return;8586 const firstImg = container.querySelector("img");87 if (!firstImg) return;8889 if (firstImg.complete) {90 calculateDimensions(firstImg);91 } else {92 firstImg.onload = () => calculateDimensions(firstImg);93 }94 };9596 const calculateDimensions = (img: HTMLImageElement) => {97 const container = containerRef.current;98 if (!container) return;99100 const { naturalWidth, naturalHeight } = img;101// … truncated
Files it writes
More from shadcn/ui
All 68 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| 3D Layered Card3d-layered-card | shadcn/ui | Components | Free | 1 dep | |
| Animated Code Blockanimated-code-block | shadcn/ui | Components | Free | 3 deps | |
| Animated Grainy Backgroundanimated-grainy-bg | shadcn/ui | Components | Free | 1 dep | |
| Animated Textanimated-text | shadcn/ui | Components | Free | 1 dep | |
| Apple Glass Effectapple-glass-effect | shadcn/ui | Components | Free | 1 dep | |
| Bubble Backgroundbubble-background | shadcn/ui | Components | Free | no deps | |
| Custom Cursorcustom-cursor | shadcn/ui | Components | Free | 1 dep | |
| Cyberpunk Cardcyberpunk-card | shadcn/ui | Components | Free | no deps |
