🕹️ Trading Card
Level up your UI game with our interactive Trading Card component! Featuring rank, name, and character description, this card isn't just for show—it comes alive with a 3D hover effect that adds depth and style
Preview
Unstoppable force, unrivaled skill - Messi's legacy is built on precision, perseverance, and a relentless drive to redefine greatness on the field.
Follow below steps 👇🏻
Install dependencies
1npm i framer-motion
Component
Create a file trading-card.tsx in your components folder and paste this code
1import { motion, useAnimationControls } from 'framer-motion';2import React, { useRef } from 'react';34interface TradingCardProps {5illustration: React.ReactNode;6rank?: number;7name: string;8description: string;9}1011const TradingCard: React.FC<TradingCardProps> = ({ illustration, rank, name, description }) => {12const cardRef = useRef<HTMLDivElement>(null);1314const backgroundControls = useAnimationControls();15const contentControls = useAnimationControls();16const cardControls = useAnimationControls();1718const onMouseEnter: React.MouseEventHandler<HTMLDivElement> = (e) => {19contentControls.start({ x: -300 });20backgroundControls.start({ scale: 1.05, opacity: 1 });21};2223const onMouseLeave = () => {24contentControls.start({ x: 0, transition: { delay: 0.5 } });25backgroundControls.start({ scale: 0.95, opacity: 0.4 });26cardControls.start({ transform: `rotateY(0deg) rotateX(0deg)` });27};2829const onMouseMove: React.MouseEventHandler<HTMLDivElement> = (e) => {30if (!cardRef.current) return;3132const rect = cardRef.current?.getBoundingClientRect();33if (!rect) return;3435const mx = e.clientX - rect.left;36const my = e.clientY - rect.top;3738const width = rect.right - rect.left;39const height = rect.bottom - rect.top;4041const xd = (mx - width / 2) / 10;42const yd = (height / 2 - my) / 10;4344cardRef.current.style.transform = `perspective(1000px) rotateY(${xd}deg) rotateX(${yd}deg)`;45};4647return (48<motion.div49ref={cardRef}50onMouseMove={onMouseMove}51onMouseEnter={onMouseEnter}52onMouseLeave={onMouseLeave}53initial={{54transform: 'perspective(1000px) rotateX(0deg) rotateY(0deg)'55}}56style={{57transformStyle: 'preserve-3d'58}}59animate={cardControls}60className="flex flex-col hover:scale-105 transition-all duration-200 ease-linear items-start justify-end rounded-lg relative shadow-xl overflow-hidden h-[400px] w-[300px] cursor-pointer border-[1px] border-neutral-800"61>62<motion.div63className="h-full w-full absolute z-0"64initial={{65opacity: 0.4,66scale: 0.9567}}68animate={backgroundControls}69transition={{ duration: 0.7, ease: 'backOut' }}70>71{illustration}72</motion.div>73{rank && <div className="font-semibold absolute top-5 right-5 z-10">#{rank}</div>}74<motion.div75animate={contentControls}76transition={{ duration: 0.5, ease: 'backOut' }}77className="p-5 h-full w-full flex flex-col justify-end bg-transparent z-10 opacity-90"78>79<div className="font-medium">80{name.split(' ').map((word) => {81return (82<div83key={word}84className="flex flex-col items-start justify-start text-4xl font-bold"85>86{word} <br />87</div>88);89})}90</div>91<p className="text-sm">{description}</p>92</motion.div>93</motion.div>94);95};9697export default TradingCard;
Usage
1<TradingCard2illustration={3<div4className="h-full w-full inset-0 bg-cover bg-center"5style={{6backgroundImage: 'url(https://i.pinimg.com/736x/9c/fa/15/9cfa15fab5013f15b472f91450be5f01.jpg)'7}}8></div>9}10rank={1}11name="Lionel Messi"12description="Unstoppable force, unrivaled skill - Messi's legacy is built on precision, perseverance, and a relentless drive to redefine greatness on the field."13/>
⭐️ Got a question or feedback?
Feel free to reach out!