@moontra/moonui-pro 2.0.22 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/dist/index.mjs +215 -214
  2. package/package.json +4 -2
  3. package/src/__tests__/use-intersection-observer.test.tsx +216 -0
  4. package/src/__tests__/use-local-storage.test.tsx +174 -0
  5. package/src/__tests__/use-pro-access.test.tsx +183 -0
  6. package/src/components/advanced-chart/advanced-chart.test.tsx +281 -0
  7. package/src/components/advanced-chart/index.tsx +412 -0
  8. package/src/components/advanced-forms/index.tsx +431 -0
  9. package/src/components/animated-button/index.tsx +202 -0
  10. package/src/components/calendar/event-dialog.tsx +372 -0
  11. package/src/components/calendar/index.tsx +557 -0
  12. package/src/components/color-picker/index.tsx +434 -0
  13. package/src/components/dashboard/index.tsx +334 -0
  14. package/src/components/data-table/data-table.test.tsx +187 -0
  15. package/src/components/data-table/index.tsx +368 -0
  16. package/src/components/draggable-list/index.tsx +100 -0
  17. package/src/components/enhanced/button.tsx +360 -0
  18. package/src/components/enhanced/card.tsx +272 -0
  19. package/src/components/enhanced/dialog.tsx +248 -0
  20. package/src/components/enhanced/index.ts +3 -0
  21. package/src/components/error-boundary/index.tsx +111 -0
  22. package/src/components/file-upload/file-upload.test.tsx +242 -0
  23. package/src/components/file-upload/index.tsx +362 -0
  24. package/src/components/floating-action-button/index.tsx +209 -0
  25. package/src/components/github-stars/index.tsx +414 -0
  26. package/src/components/health-check/index.tsx +441 -0
  27. package/src/components/hover-card-3d/index.tsx +170 -0
  28. package/src/components/index.ts +76 -0
  29. package/src/components/kanban/index.tsx +436 -0
  30. package/src/components/lazy-component/index.tsx +342 -0
  31. package/src/components/magnetic-button/index.tsx +170 -0
  32. package/src/components/memory-efficient-data/index.tsx +352 -0
  33. package/src/components/optimized-image/index.tsx +427 -0
  34. package/src/components/performance-debugger/index.tsx +591 -0
  35. package/src/components/performance-monitor/index.tsx +775 -0
  36. package/src/components/pinch-zoom/index.tsx +172 -0
  37. package/src/components/rich-text-editor/index-old-backup.tsx +443 -0
  38. package/src/components/rich-text-editor/index.tsx +1537 -0
  39. package/src/components/rich-text-editor/slash-commands-extension.ts +220 -0
  40. package/src/components/rich-text-editor/slash-commands.css +35 -0
  41. package/src/components/rich-text-editor/table-styles.css +65 -0
  42. package/src/components/spotlight-card/index.tsx +194 -0
  43. package/src/components/swipeable-card/index.tsx +100 -0
  44. package/src/components/timeline/index.tsx +333 -0
  45. package/src/components/ui/animated-button.tsx +185 -0
  46. package/src/components/ui/avatar.tsx +135 -0
  47. package/src/components/ui/badge.tsx +225 -0
  48. package/src/components/ui/button.tsx +221 -0
  49. package/src/components/ui/card.tsx +141 -0
  50. package/src/components/ui/checkbox.tsx +256 -0
  51. package/src/components/ui/color-picker.tsx +95 -0
  52. package/src/components/ui/dialog.tsx +332 -0
  53. package/src/components/ui/dropdown-menu.tsx +200 -0
  54. package/src/components/ui/hover-card-3d.tsx +103 -0
  55. package/src/components/ui/index.ts +33 -0
  56. package/src/components/ui/input.tsx +219 -0
  57. package/src/components/ui/label.tsx +26 -0
  58. package/src/components/ui/magnetic-button.tsx +129 -0
  59. package/src/components/ui/popover.tsx +183 -0
  60. package/src/components/ui/select.tsx +273 -0
  61. package/src/components/ui/separator.tsx +140 -0
  62. package/src/components/ui/slider.tsx +351 -0
  63. package/src/components/ui/spotlight-card.tsx +119 -0
  64. package/src/components/ui/switch.tsx +83 -0
  65. package/src/components/ui/tabs.tsx +195 -0
  66. package/src/components/ui/textarea.tsx +25 -0
  67. package/src/components/ui/toast.tsx +313 -0
  68. package/src/components/ui/tooltip.tsx +152 -0
  69. package/src/components/virtual-list/index.tsx +369 -0
  70. package/src/hooks/use-chart.ts +205 -0
  71. package/src/hooks/use-data-table.ts +182 -0
  72. package/src/hooks/use-docs-pro-access.ts +13 -0
  73. package/src/hooks/use-license-check.ts +65 -0
  74. package/src/hooks/use-subscription.ts +19 -0
  75. package/src/index.ts +14 -0
  76. package/src/lib/micro-interactions.ts +255 -0
  77. package/src/lib/utils.ts +6 -0
  78. package/src/patterns/login-form/index.tsx +276 -0
  79. package/src/patterns/login-form/types.ts +67 -0
  80. package/src/setupTests.ts +41 -0
  81. package/src/styles/design-system.css +365 -0
  82. package/src/styles/index.css +4 -0
  83. package/src/styles/tailwind.css +6 -0
  84. package/src/styles/tokens.css +453 -0
  85. package/src/types/moonui.d.ts +22 -0
  86. package/src/use-intersection-observer.tsx +154 -0
  87. package/src/use-local-storage.tsx +71 -0
  88. package/src/use-paddle.ts +138 -0
  89. package/src/use-performance-optimizer.ts +379 -0
  90. package/src/use-pro-access.ts +141 -0
  91. package/src/use-scroll-animation.ts +221 -0
  92. package/src/use-subscription.ts +37 -0
  93. package/src/use-toast.ts +32 -0
  94. package/src/utils/chart-helpers.ts +257 -0
  95. package/src/utils/cn.ts +69 -0
  96. package/src/utils/data-processing.ts +151 -0
  97. package/src/utils/license-guard.tsx +177 -0
  98. package/src/utils/license-validator.tsx +183 -0
  99. package/src/utils/package-guard.ts +60 -0
@@ -0,0 +1,220 @@
1
+ import { Extension } from '@tiptap/core';
2
+ import { Plugin, PluginKey } from '@tiptap/pm/state';
3
+ import { Decoration, DecorationSet } from '@tiptap/pm/view';
4
+ import type { SlashCommand } from '@/lib/ai-providers';
5
+
6
+ declare module '@tiptap/core' {
7
+ interface Commands<ReturnType> {
8
+ slashCommands: {
9
+ selectSlashCommand: (index: number) => ReturnType;
10
+ };
11
+ }
12
+ }
13
+
14
+ export interface SlashCommandsOptions {
15
+ commands: SlashCommand[];
16
+ onSelectCommand: (command: SlashCommand) => void;
17
+ }
18
+
19
+ const slashCommandsPluginKey = new PluginKey('slashCommands');
20
+
21
+ export const SlashCommandsExtension = Extension.create<SlashCommandsOptions>({
22
+ name: 'slashCommands',
23
+
24
+ addOptions() {
25
+ return {
26
+ commands: [],
27
+ onSelectCommand: () => {},
28
+ };
29
+ },
30
+
31
+ addCommands() {
32
+ return {
33
+ selectSlashCommand: (index: number) => ({ editor }) => {
34
+ const state = slashCommandsPluginKey.getState(editor.state);
35
+ if (state && state.active && state.filteredCommands[index]) {
36
+ const command = state.filteredCommands[index];
37
+
38
+ // Clear the slash command
39
+ const { from, to } = state.range;
40
+ editor.chain()
41
+ .deleteRange({ from, to })
42
+ .focus()
43
+ .run();
44
+
45
+ // Execute the command
46
+ this.options.onSelectCommand(command);
47
+
48
+ return true;
49
+ }
50
+ return false;
51
+ },
52
+ };
53
+ },
54
+
55
+ addKeyboardShortcuts() {
56
+ return {
57
+ ArrowUp: ({ editor }) => {
58
+ const state = slashCommandsPluginKey.getState(editor.state);
59
+ if (state && state.active) {
60
+ const newIndex = state.selectedIndex > 0 ? state.selectedIndex - 1 : state.filteredCommands.length - 1;
61
+ editor.view.dispatch(
62
+ editor.state.tr.setMeta(slashCommandsPluginKey, { selectedIndex: newIndex })
63
+ );
64
+ return true;
65
+ }
66
+ return false;
67
+ },
68
+ ArrowDown: ({ editor }) => {
69
+ const state = slashCommandsPluginKey.getState(editor.state);
70
+ if (state && state.active) {
71
+ const newIndex = state.selectedIndex < state.filteredCommands.length - 1 ? state.selectedIndex + 1 : 0;
72
+ editor.view.dispatch(
73
+ editor.state.tr.setMeta(slashCommandsPluginKey, { selectedIndex: newIndex })
74
+ );
75
+ return true;
76
+ }
77
+ return false;
78
+ },
79
+ Enter: ({ editor }) => {
80
+ const state = slashCommandsPluginKey.getState(editor.state);
81
+ if (state && state.active) {
82
+ editor.commands.selectSlashCommand(state.selectedIndex);
83
+ return true;
84
+ }
85
+ return false;
86
+ },
87
+ Escape: ({ editor }) => {
88
+ const state = slashCommandsPluginKey.getState(editor.state);
89
+ if (state && state.active) {
90
+ editor.view.dispatch(
91
+ editor.state.tr.setMeta(slashCommandsPluginKey, { active: false })
92
+ );
93
+ return true;
94
+ }
95
+ return false;
96
+ },
97
+ };
98
+ },
99
+
100
+ addProseMirrorPlugins() {
101
+ return [
102
+ new Plugin({
103
+ key: slashCommandsPluginKey,
104
+ state: {
105
+ init() {
106
+ return {
107
+ active: false,
108
+ range: { from: 0, to: 0 },
109
+ query: '',
110
+ filteredCommands: [],
111
+ selectedIndex: 0,
112
+ };
113
+ },
114
+ apply(transaction, value, oldState, newState) {
115
+ const meta = transaction.getMeta(slashCommandsPluginKey);
116
+ if (meta) {
117
+ return { ...value, ...meta };
118
+ }
119
+
120
+ // Check for slash character
121
+ const { selection } = newState;
122
+ const { empty, $from } = selection;
123
+
124
+ if (!empty) {
125
+ return { ...value, active: false };
126
+ }
127
+
128
+ const textBefore = $from.parent.textBetween(
129
+ Math.max(0, $from.parentOffset - 50),
130
+ $from.parentOffset,
131
+ null,
132
+ '\ufffc'
133
+ );
134
+
135
+ const match = textBefore.match(/\/(\w*)$/);
136
+
137
+ if (match) {
138
+ const query = match[1].toLowerCase();
139
+ const filteredCommands = this.options.commands.filter(cmd =>
140
+ cmd.command.toLowerCase().includes(query) ||
141
+ cmd.description.toLowerCase().includes(query)
142
+ );
143
+
144
+ return {
145
+ active: true,
146
+ range: {
147
+ from: $from.pos - match[0].length,
148
+ to: $from.pos,
149
+ },
150
+ query,
151
+ filteredCommands,
152
+ selectedIndex: 0,
153
+ };
154
+ }
155
+
156
+ return { ...value, active: false };
157
+ },
158
+ },
159
+ props: {
160
+ decorations(state) {
161
+ const pluginState = this.getState(state);
162
+ if (!pluginState.active) {
163
+ return DecorationSet.empty;
164
+ }
165
+
166
+ // Slash menu decoration
167
+ const { from } = pluginState.range;
168
+ const decoration = Decoration.widget(from, () => {
169
+ const container = document.createElement('div');
170
+ container.className = 'slash-commands-menu';
171
+
172
+ pluginState.filteredCommands.forEach((cmd, index) => {
173
+ const item = document.createElement('div');
174
+ item.className = `slash-command-item ${index === pluginState.selectedIndex ? 'selected' : ''}`;
175
+
176
+ if (cmd.icon) {
177
+ const iconWrapper = document.createElement('div');
178
+ iconWrapper.innerHTML = cmd.icon;
179
+ item.appendChild(iconWrapper);
180
+ }
181
+
182
+ const textWrapper = document.createElement('div');
183
+
184
+ const commandName = document.createElement('div');
185
+ commandName.className = 'command-name';
186
+ commandName.textContent = `/${cmd.command}`;
187
+ textWrapper.appendChild(commandName);
188
+
189
+ const commandDesc = document.createElement('div');
190
+ commandDesc.className = 'command-description';
191
+ commandDesc.textContent = cmd.description;
192
+ textWrapper.appendChild(commandDesc);
193
+
194
+ item.appendChild(textWrapper);
195
+
196
+ item.addEventListener('click', () => {
197
+ const editor = (state as any).editor;
198
+ editor.commands.selectSlashCommand(index);
199
+ });
200
+
201
+ container.appendChild(item);
202
+ });
203
+
204
+ if (pluginState.filteredCommands.length === 0) {
205
+ const empty = document.createElement('div');
206
+ empty.className = 'text-gray-500 p-3 text-sm';
207
+ empty.textContent = 'No commands found';
208
+ container.appendChild(empty);
209
+ }
210
+
211
+ return container;
212
+ });
213
+
214
+ return DecorationSet.create(state.doc, [decoration]);
215
+ },
216
+ },
217
+ }),
218
+ ];
219
+ },
220
+ });
@@ -0,0 +1,35 @@
1
+ /* Slash Commands Menu Styles */
2
+ .slash-commands-menu {
3
+ @apply bg-white dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg;
4
+ @apply max-h-80 overflow-y-auto min-w-[280px] p-2;
5
+ }
6
+
7
+ .slash-command-item {
8
+ @apply flex items-center gap-3 px-3 py-2 rounded-md cursor-pointer;
9
+ @apply hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors;
10
+ }
11
+
12
+ .slash-command-item.selected {
13
+ @apply bg-gray-100 dark:bg-gray-800;
14
+ }
15
+
16
+ .slash-command-item svg {
17
+ @apply w-4 h-4 text-gray-500 dark:text-gray-400;
18
+ }
19
+
20
+ .slash-command-item > div {
21
+ @apply flex-1;
22
+ }
23
+
24
+ .slash-command-item .command-name {
25
+ @apply font-medium text-gray-900 dark:text-gray-100 text-sm;
26
+ }
27
+
28
+ .slash-command-item .command-description {
29
+ @apply text-xs text-gray-500 dark:text-gray-400 mt-0.5;
30
+ }
31
+
32
+ /* Dark mode support */
33
+ .dark .slash-commands-menu {
34
+ box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -2px rgba(0, 0, 0, 0.2);
35
+ }
@@ -0,0 +1,65 @@
1
+ /* Table styles for RichTextEditor */
2
+ .ProseMirror table {
3
+ border-collapse: collapse;
4
+ margin: 0;
5
+ overflow: hidden;
6
+ table-layout: fixed;
7
+ width: 100%;
8
+ border: 1px solid #d1d5db;
9
+ }
10
+
11
+ .ProseMirror table td,
12
+ .ProseMirror table th {
13
+ border: 1px solid #d1d5db;
14
+ box-sizing: border-box;
15
+ min-width: 1em;
16
+ padding: 6px 8px;
17
+ position: relative;
18
+ vertical-align: top;
19
+ }
20
+
21
+ .ProseMirror table th {
22
+ background-color: #f9fafb;
23
+ font-weight: 600;
24
+ text-align: left;
25
+ }
26
+
27
+ .ProseMirror table .selectedCell:after {
28
+ background-color: rgba(59, 130, 246, 0.1);
29
+ content: '';
30
+ left: 0;
31
+ right: 0;
32
+ top: 0;
33
+ bottom: 0;
34
+ pointer-events: none;
35
+ position: absolute;
36
+ z-index: 2;
37
+ }
38
+
39
+ .ProseMirror table .column-resize-handle {
40
+ background-color: #3b82f6;
41
+ bottom: -2px;
42
+ position: absolute;
43
+ right: -2px;
44
+ top: 0;
45
+ width: 4px;
46
+ pointer-events: none;
47
+ }
48
+
49
+ /* Dark mode styles */
50
+ .dark .ProseMirror table {
51
+ border: 1px solid #374151;
52
+ }
53
+
54
+ .dark .ProseMirror table td,
55
+ .dark .ProseMirror table th {
56
+ border: 1px solid #374151;
57
+ }
58
+
59
+ .dark .ProseMirror table th {
60
+ background-color: #111827;
61
+ }
62
+
63
+ .dark .ProseMirror table .selectedCell:after {
64
+ background-color: rgba(59, 130, 246, 0.2);
65
+ }
@@ -0,0 +1,194 @@
1
+ "use client"
2
+
3
+ import React, { useRef, useState } from "react"
4
+ import { motion } from "framer-motion"
5
+ import { cn } from "../../lib/utils"
6
+ import { Card, CardContent } from "../ui/card"
7
+ import { Button } from "../ui/button"
8
+ import { Lock, Sparkles } from "lucide-react"
9
+ import { useSubscription } from "../../hooks/use-subscription"
10
+
11
+ export interface SpotlightCardProps extends React.HTMLAttributes<HTMLDivElement> {
12
+ spotlightColor?: string
13
+ spotlightSize?: number
14
+ intensity?: number
15
+ borderGlow?: boolean
16
+ }
17
+
18
+ const SpotlightCardInternal = React.forwardRef<HTMLDivElement, SpotlightCardProps>(
19
+ ({
20
+ children,
21
+ className,
22
+ spotlightColor = "rgba(255, 255, 255, 0.25)",
23
+ spotlightSize = 300,
24
+ intensity = 0.8,
25
+ borderGlow = true,
26
+ ...props
27
+ }, ref) => {
28
+ const cardRef = useRef<HTMLDivElement>(null)
29
+ const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 })
30
+ const [isHovered, setIsHovered] = useState(false)
31
+
32
+ const handleMouseMove = (e: React.MouseEvent) => {
33
+ if (!cardRef.current) return
34
+
35
+ const rect = cardRef.current.getBoundingClientRect()
36
+ const x = e.clientX - rect.left
37
+ const y = e.clientY - rect.top
38
+
39
+ setMousePosition({ x, y })
40
+ }
41
+
42
+ const handleMouseEnter = () => {
43
+ setIsHovered(true)
44
+ }
45
+
46
+ const handleMouseLeave = () => {
47
+ setIsHovered(false)
48
+ }
49
+
50
+ return (
51
+ <motion.div
52
+ ref={(node) => {
53
+ cardRef.current = node
54
+ if (typeof ref === "function") {
55
+ ref(node)
56
+ } else if (ref) {
57
+ ref.current = node
58
+ }
59
+ }}
60
+ className={cn(
61
+ "relative overflow-hidden rounded-lg border bg-card text-card-foreground shadow-sm",
62
+ className
63
+ )}
64
+ onMouseMove={handleMouseMove}
65
+ onMouseEnter={handleMouseEnter}
66
+ onMouseLeave={handleMouseLeave}
67
+ whileHover={{ scale: 1.02 }}
68
+ transition={{ duration: 0.2 }}
69
+ {...props}
70
+ >
71
+ {/* Spotlight effect */}
72
+ <motion.div
73
+ className="absolute inset-0 pointer-events-none"
74
+ animate={{
75
+ opacity: isHovered ? intensity : 0,
76
+ }}
77
+ transition={{ duration: 0.3 }}
78
+ style={{
79
+ background: `radial-gradient(${spotlightSize}px circle at ${mousePosition.x}px ${mousePosition.y}px, ${spotlightColor}, transparent 70%)`,
80
+ }}
81
+ />
82
+
83
+ {/* Secondary glow effect */}
84
+ <motion.div
85
+ className="absolute inset-0 pointer-events-none"
86
+ animate={{
87
+ opacity: isHovered ? intensity * 0.5 : 0,
88
+ }}
89
+ transition={{ duration: 0.4, delay: 0.1 }}
90
+ style={{
91
+ background: `radial-gradient(${spotlightSize * 1.5}px circle at ${mousePosition.x}px ${mousePosition.y}px, ${spotlightColor.replace('0.25', '0.1')}, transparent 80%)`,
92
+ }}
93
+ />
94
+
95
+ {/* Border glow */}
96
+ {borderGlow && (
97
+ <motion.div
98
+ className="absolute inset-0 rounded-lg pointer-events-none"
99
+ animate={{
100
+ opacity: isHovered ? 0.6 : 0,
101
+ }}
102
+ transition={{ duration: 0.3 }}
103
+ style={{
104
+ background: `linear-gradient(135deg, transparent 30%, ${spotlightColor.replace('0.25', '0.4')} 50%, transparent 70%)`,
105
+ backgroundSize: '200% 200%',
106
+ backgroundPosition: `${(mousePosition.x / cardRef.current?.offsetWidth!) * 100}% ${(mousePosition.y / cardRef.current?.offsetHeight!) * 100}%`,
107
+ }}
108
+ />
109
+ )}
110
+
111
+ {/* Content */}
112
+ <div className="relative z-10">
113
+ {children}
114
+ </div>
115
+
116
+ {/* Subtle animated particles */}
117
+ <motion.div
118
+ className="absolute inset-0 pointer-events-none"
119
+ animate={{
120
+ opacity: isHovered ? 0.3 : 0,
121
+ }}
122
+ transition={{ duration: 0.5 }}
123
+ >
124
+ {[...Array(3)].map((_, i) => (
125
+ <motion.div
126
+ key={i}
127
+ className="absolute w-1 h-1 bg-white rounded-full"
128
+ animate={isHovered ? {
129
+ x: [mousePosition.x, mousePosition.x + Math.random() * 100 - 50],
130
+ y: [mousePosition.y, mousePosition.y + Math.random() * 100 - 50],
131
+ opacity: [0, 1, 0],
132
+ scale: [0, 1, 0],
133
+ } : {}}
134
+ transition={{
135
+ duration: 2,
136
+ delay: i * 0.2,
137
+ repeat: isHovered ? Infinity : 0,
138
+ ease: "easeOut"
139
+ }}
140
+ style={{
141
+ left: mousePosition.x,
142
+ top: mousePosition.y,
143
+ }}
144
+ />
145
+ ))}
146
+ </motion.div>
147
+ </motion.div>
148
+ )
149
+ }
150
+ )
151
+
152
+ SpotlightCardInternal.displayName = "SpotlightCardInternal"
153
+
154
+ export const SpotlightCard = React.forwardRef<HTMLDivElement, SpotlightCardProps>(
155
+ ({ className, ...props }, ref) => {
156
+ // Check if we're in docs mode or have pro access
157
+ const docsProAccess = { hasAccess: true } // Pro access assumed in package
158
+ const { hasProAccess, isLoading } = useSubscription()
159
+
160
+ // In docs mode, always show the component
161
+ const canShowComponent = docsProAccess.isDocsMode || hasProAccess
162
+
163
+ // If not in docs mode and no pro access, show upgrade prompt
164
+ if (!docsProAccess.isDocsMode && !isLoading && !hasProAccess) {
165
+ return (
166
+ <Card className={cn("w-fit", className)}>
167
+ <CardContent className="py-6 text-center">
168
+ <div className="space-y-4">
169
+ <div className="rounded-full bg-purple-100 dark:bg-purple-900/30 p-3 w-fit mx-auto">
170
+ <Lock className="h-6 w-6 text-purple-600 dark:text-purple-400" />
171
+ </div>
172
+ <div>
173
+ <h3 className="font-semibold text-sm mb-2">Pro Feature</h3>
174
+ <p className="text-muted-foreground text-xs mb-4">
175
+ Spotlight Card is available exclusively to MoonUI Pro subscribers.
176
+ </p>
177
+ <a href="/pricing">
178
+ <Button size="sm">
179
+ <Sparkles className="mr-2 h-4 w-4" />
180
+ Upgrade to Pro
181
+ </Button>
182
+ </a>
183
+ </div>
184
+ </div>
185
+ </CardContent>
186
+ </Card>
187
+ )
188
+ }
189
+
190
+ return <SpotlightCardInternal className={className} ref={ref} {...props} />
191
+ }
192
+ )
193
+
194
+ SpotlightCard.displayName = "SpotlightCard"
@@ -0,0 +1,100 @@
1
+ "use client"
2
+
3
+ import React, { ReactNode } from "react"
4
+ import { motion, useMotionValue, useTransform, animate, PanInfo } from "framer-motion"
5
+ import { cn } from "../../lib/utils"
6
+
7
+ export interface SwipeableCardProps {
8
+ children: ReactNode
9
+ onSwipeLeft?: () => void
10
+ onSwipeRight?: () => void
11
+ onSwipeStart?: () => void
12
+ onSwipeEnd?: () => void
13
+ threshold?: number
14
+ disabled?: boolean
15
+ className?: string
16
+ style?: React.CSSProperties
17
+ }
18
+
19
+ export const SwipeableCard = React.forwardRef<HTMLDivElement, SwipeableCardProps>(
20
+ ({
21
+ children,
22
+ onSwipeLeft,
23
+ onSwipeRight,
24
+ onSwipeStart,
25
+ onSwipeEnd,
26
+ threshold = 100,
27
+ disabled = false,
28
+ className,
29
+ style,
30
+ ...props
31
+ }, ref) => {
32
+ const x = useMotionValue(0)
33
+ const rotate = useTransform(x, [-200, 200], [-30, 30])
34
+ const likeOpacity = useTransform(x, [0, threshold], [0, 1])
35
+ const nopeOpacity = useTransform(x, [-threshold, 0], [1, 0])
36
+
37
+ const handleDragStart = () => {
38
+ if (disabled) return
39
+ onSwipeStart?.()
40
+ }
41
+
42
+ const handleDragEnd = (event: any, info: PanInfo) => {
43
+ if (disabled) return
44
+
45
+ onSwipeEnd?.()
46
+
47
+ if (Math.abs(info.offset.x) > threshold) {
48
+ const direction = info.offset.x > 0 ? "right" : "left"
49
+ const exitX = info.offset.x > 0 ? 400 : -400
50
+
51
+ animate(x, exitX, { duration: 0.3 })
52
+
53
+ if (direction === "right" && onSwipeRight) {
54
+ onSwipeRight()
55
+ } else if (direction === "left" && onSwipeLeft) {
56
+ onSwipeLeft()
57
+ }
58
+ } else {
59
+ animate(x, 0, { type: "spring", stiffness: 300, damping: 20 })
60
+ }
61
+ }
62
+
63
+ return (
64
+ <motion.div
65
+ ref={ref}
66
+ drag={disabled ? false : "x"}
67
+ dragConstraints={{ left: -300, right: 300 }}
68
+ style={{ x, rotate, ...style }}
69
+ onDragStart={handleDragStart}
70
+ onDragEnd={handleDragEnd}
71
+ className={cn(
72
+ "relative cursor-grab active:cursor-grabbing select-none",
73
+ disabled && "cursor-default",
74
+ className
75
+ )}
76
+ {...props}
77
+ >
78
+ {/* Like indicator */}
79
+ <motion.div
80
+ style={{ opacity: likeOpacity }}
81
+ className="absolute top-4 right-4 bg-green-500 text-white px-3 py-1 rounded-lg font-bold text-lg rotate-[15deg] pointer-events-none z-10"
82
+ >
83
+ LIKE
84
+ </motion.div>
85
+
86
+ {/* Nope indicator */}
87
+ <motion.div
88
+ style={{ opacity: nopeOpacity }}
89
+ className="absolute top-4 left-4 bg-red-500 text-white px-3 py-1 rounded-lg font-bold text-lg rotate-[-15deg] pointer-events-none z-10"
90
+ >
91
+ NOPE
92
+ </motion.div>
93
+
94
+ {children}
95
+ </motion.div>
96
+ )
97
+ }
98
+ )
99
+
100
+ SwipeableCard.displayName = "SwipeableCard"