'use client'; import { motion, useMotionValue, useTransform, PanInfo } from "framer-motion"; import Image from "next/image"; import React, { useState, useEffect } from "react"; interface SwipeCardProps { children: React.ReactNode; onSendToBack: () => void; } function SwipeCard({ children, onSendToBack }: SwipeCardProps) { const x = useMotionValue(0); const y = useMotionValue(0); const [shadow, setShadow] = useState("0px 0px 15px rgba(0,0,0,0.3)"); const rotateX = useTransform(y, [-200, 200], [30, -30]); const rotateY = useTransform(x, [-200, 200], [-30, 30]); useEffect(() => { const unsubscribeX = x.onChange(latestX => { const latestY = y.get(); setShadow(`${latestX / 10}px ${latestY / 10}px 15px rgba(0,0,0,0.3)`); }); const unsubscribeY = y.onChange(latestY => { const latestX = x.get(); setShadow(`${latestX / 10}px ${latestY / 10}px 15px rgba(0,0,0,0.3)`); }); return () => { unsubscribeX(); unsubscribeY(); }; }, [x, y]); function handleDragEnd(_: any, info: PanInfo) { const threshold = 150; if (Math.abs(info.offset.x) > threshold || Math.abs(info.offset.y) > threshold) { onSendToBack(); } else { x.set(0); y.set(0); } } return ( {children} ); } interface SwipeCardsProps { cards: { id: number; z: number; img: string }[]; } function SwipeCards({ cards }: SwipeCardsProps) { const [cardList, setCardList] = useState(cards); const moveToBack = (id: number) => { setCardList((prevCards) => { const updatedCards = [...prevCards]; const cardIndex = updatedCards.findIndex((card) => card.id === id); const [movedCard] = updatedCards.splice(cardIndex, 1); updatedCards.unshift(movedCard); return updatedCards; }); }; return (
{cardList.map((card, index) => ( moveToBack(card.id)}> {`card-${card.id}`} ))}
); } export default SwipeCards;