"use client"; import { useCallback, useRef, useState } from "react"; import { Handle, Position, type NodeProps } from "@xyflow/react"; import { ImageIcon } from "lucide-react"; import BaseNodeWrapper from "./base-node-wrapper"; interface CompareNodeData { leftUrl?: string; rightUrl?: string; leftLabel?: string; rightLabel?: string; } export default function CompareNode({ data, selected }: NodeProps) { const nodeData = data as CompareNodeData; const [sliderX, setSliderX] = useState(50); const containerRef = useRef(null); const hasLeft = !!nodeData.leftUrl; const hasRight = !!nodeData.rightUrl; const handleMouseDown = useCallback((event: React.MouseEvent) => { event.stopPropagation(); const move = (moveEvent: MouseEvent) => { if (!containerRef.current) return; const rect = containerRef.current.getBoundingClientRect(); const x = Math.max( 0, Math.min(1, (moveEvent.clientX - rect.left) / rect.width), ); setSliderX(x * 100); }; const up = () => { window.removeEventListener("mousemove", move); window.removeEventListener("mouseup", up); }; window.addEventListener("mousemove", move); window.addEventListener("mouseup", up); }, []); const handleTouchStart = useCallback((event: React.TouchEvent) => { event.stopPropagation(); const move = (moveEvent: TouchEvent) => { if (!containerRef.current || moveEvent.touches.length === 0) return; const rect = containerRef.current.getBoundingClientRect(); const touch = moveEvent.touches[0]; const x = Math.max(0, Math.min(1, (touch.clientX - rect.left) / rect.width)); setSliderX(x * 100); }; const end = () => { window.removeEventListener("touchmove", move); window.removeEventListener("touchend", end); }; window.addEventListener("touchmove", move); window.addEventListener("touchend", end); }, []); return (
⚖️ Compare
{!hasLeft && !hasRight && (

Connect two image nodes - left handle (blue) and right handle (green)

)} {hasRight && ( // eslint-disable-next-line @next/next/no-img-element {nodeData.rightLabel )} {hasLeft && (
{/* eslint-disable-next-line @next/next/no-img-element */} {nodeData.leftLabel
)} {hasLeft && hasRight && ( <>
)} {hasLeft && (
{nodeData.leftLabel ?? "Before"}
)} {hasRight && (
{nodeData.rightLabel ?? "After"}
)}
); }