forked from StarkMindsHQ/StrellerMinds-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveReaction.tsx
More file actions
134 lines (121 loc) · 3.41 KB
/
Copy pathInteractiveReaction.tsx
File metadata and controls
134 lines (121 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use client';
import React, { useState, useEffect } from 'react';
import { motion, AnimatePresence, useAnimation } from 'framer-motion';
import {
Heart,
ThumbsUp,
MessageCircle,
Share2,
Bookmark,
LucideIcon,
} from 'lucide-react';
import { cn } from '@/lib/utils';
export type ReactionType = 'like' | 'love' | 'comment' | 'share' | 'bookmark';
interface InteractiveReactionProps {
reactionType: ReactionType;
count: number;
onReact?: () => void;
isActive?: boolean;
className?: string;
showCount?: boolean;
}
const iconMap: Record<ReactionType, LucideIcon> = {
like: ThumbsUp,
love: Heart,
comment: MessageCircle,
share: Share2,
bookmark: Bookmark,
};
const InteractiveReaction: React.FC<InteractiveReactionProps> = ({
reactionType,
count,
onReact,
isActive = false,
className,
showCount = true,
}) => {
const [prevCount, setPrevCount] = useState(count);
const controls = useAnimation();
const Icon = iconMap[reactionType];
useEffect(() => {
if (count !== prevCount) {
setPrevCount(count);
// Trigger a small bump animation on count change
controls.start({
scale: [1, 1.2, 1],
transition: { duration: 0.2 },
});
}
}, [count, prevCount, controls]);
const handleClick = () => {
if (onReact) {
onReact();
}
// Trigger specific animation based on type
if (reactionType === 'love') {
controls.start({
scale: [1, 1.5, 1],
transition: { duration: 0.4, type: 'spring' },
});
} else if (reactionType === 'like') {
controls.start({
rotate: [0, -20, 20, -10, 0],
transition: { duration: 0.5 },
});
} else {
controls.start({
scale: [1, 0.9, 1.1, 1],
transition: { duration: 0.3 },
});
}
};
// Color mapping
const activeColorClass: Record<ReactionType, string> = {
like: 'text-blue-500',
love: 'text-red-500',
comment: 'text-green-500',
share: 'text-indigo-500',
bookmark: 'text-yellow-500',
};
const fillClass =
isActive && ['love', 'like', 'bookmark'].includes(reactionType)
? 'fill-current'
: '';
return (
<motion.button
className={cn(
'group flex items-center gap-1.5 px-3 py-1.5 rounded-full transition-colors hover:bg-gray-100 dark:hover:bg-gray-800/50',
isActive
? activeColorClass[reactionType]
: 'text-gray-500 dark:text-gray-400',
className,
)}
onClick={handleClick}
whileTap={{ scale: 0.95 }}
>
<motion.div
animate={controls}
className="relative flex items-center justify-center"
>
<Icon size={20} className={cn('transition-colors', fillClass)} />
</motion.div>
{showCount && (
<div className="relative h-5 min-w-[1ch] flex flex-col justify-center overflow-hidden">
<AnimatePresence mode="popLayout" initial={false}>
<motion.span
key={count}
initial={{ y: count > prevCount ? 15 : -15, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
exit={{ y: count > prevCount ? -15 : 15, opacity: 0 }}
transition={{ type: 'spring', stiffness: 300, damping: 30 }}
className="font-medium text-sm leading-none"
>
{count}
</motion.span>
</AnimatePresence>
</div>
)}
</motion.button>
);
};
export default InteractiveReaction;