'use client'; import React, { useState, useRef, useEffect, MouseEvent, TouchEvent } from 'react'; import Image from 'next/image'; import { projectsData } from '@/content/projects'; import { GripVertical } from 'lucide-react'; export const BeforeAfterSlider = () => { const [sliderPosition, setSliderPosition] = useState(50); const [isDragging, setIsDragging] = useState(false); const containerRef = useRef(null); // Pobierz pierwszy projekt z kategorii remonty, który ma beforeAfter const project = projectsData.find(p => p.category === 'remonty' && p.beforeAfter); if (!project || !project.beforeAfter) return null; const handleMove = (clientX: number) => { if (!containerRef.current) return; const rect = containerRef.current.getBoundingClientRect(); const x = Math.max(0, Math.min(clientX - rect.left, rect.width)); const percentage = (x / rect.width) * 100; setSliderPosition(percentage); }; const onMouseMove = (e: globalThis.MouseEvent) => { if (!isDragging) return; handleMove(e.clientX); }; const onTouchMove = (e: globalThis.TouchEvent) => { if (!isDragging) return; handleMove(e.touches[0].clientX); }; const stopDragging = () => { setIsDragging(false); }; useEffect(() => { if (isDragging) { window.addEventListener('mousemove', onMouseMove); window.addEventListener('mouseup', stopDragging); window.addEventListener('touchmove', onTouchMove); window.addEventListener('touchend', stopDragging); } else { window.removeEventListener('mousemove', onMouseMove); window.removeEventListener('mouseup', stopDragging); window.removeEventListener('touchmove', onTouchMove); window.removeEventListener('touchend', stopDragging); } return () => { window.removeEventListener('mousemove', onMouseMove); window.removeEventListener('mouseup', stopDragging); window.removeEventListener('touchmove', onTouchMove); window.removeEventListener('touchend', stopDragging); }; }, [isDragging]); return (

Przed i Po

Zobacz metamorfozę na jednym z naszych ostatnich projektów.

setIsDragging(true)} onTouchStart={() => setIsDragging(true)} > {/* Obrazek PO (na spodzie) */}
Wygląd po remoncie
Po
{/* Obrazek PRZED (na wierzchu, obcięty) */}
{/* Używamy native img dla prostszego manipulowania wymiarami wewnątrz overflow */} Wygląd przed remontem
Przed
{/* Suwak (linia i uchwyt) */}
); };