|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { motion, AnimatePresence } from 'framer-motion'; |
| 4 | +import { ChevronRight, ChevronLeft, X } from 'lucide-react'; |
| 5 | +import { Button } from '@/components/ui/button'; |
| 6 | +import { useEffect, useState } from 'react'; |
| 7 | + |
| 8 | +interface TourTooltipProps { |
| 9 | + title: string; |
| 10 | + description: string; |
| 11 | + step: number; |
| 12 | + totalSteps: number; |
| 13 | + targetRect: DOMRect | null; |
| 14 | + onNext: () => void; |
| 15 | + onPrevious: () => void; |
| 16 | + onSkip: () => void; |
| 17 | + onComplete: () => void; |
| 18 | +} |
| 19 | + |
| 20 | +export function TourTooltip({ |
| 21 | + title, |
| 22 | + description, |
| 23 | + step, |
| 24 | + totalSteps, |
| 25 | + targetRect, |
| 26 | + onNext, |
| 27 | + onPrevious, |
| 28 | + onSkip, |
| 29 | + onComplete, |
| 30 | +}: TourTooltipProps) { |
| 31 | + const [position, setPosition] = useState({ top: 0, left: 0 }); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + if (!targetRect) return; |
| 35 | + |
| 36 | + const padding = 16; |
| 37 | + const tooltipHeight = 200; |
| 38 | + const tooltipWidth = 300; |
| 39 | + |
| 40 | + let top = targetRect.bottom + padding; |
| 41 | + let left = targetRect.left + targetRect.width / 2 - tooltipWidth / 2; |
| 42 | + |
| 43 | + // Adjust if tooltip goes off-screen |
| 44 | + if (left < padding) { |
| 45 | + left = padding; |
| 46 | + } else if (left + tooltipWidth > window.innerWidth - padding) { |
| 47 | + left = window.innerWidth - tooltipWidth - padding; |
| 48 | + } |
| 49 | + |
| 50 | + if (top + tooltipHeight > window.innerHeight) { |
| 51 | + top = targetRect.top - tooltipHeight - padding; |
| 52 | + } |
| 53 | + |
| 54 | + setPosition({ top, left }); |
| 55 | + }, [targetRect]); |
| 56 | + |
| 57 | + if (!targetRect) return null; |
| 58 | + |
| 59 | + return ( |
| 60 | + <AnimatePresence> |
| 61 | + <motion.div |
| 62 | + key={`tooltip-${step}`} |
| 63 | + initial={{ opacity: 0, scale: 0.9, y: -10 }} |
| 64 | + animate={{ opacity: 1, scale: 1, y: 0 }} |
| 65 | + exit={{ opacity: 0, scale: 0.9, y: -10 }} |
| 66 | + transition={{ duration: 0.2 }} |
| 67 | + className="fixed z-50 bg-background border border-border rounded-lg shadow-lg p-4 w-80 pointer-events-auto" |
| 68 | + style={{ |
| 69 | + top: `${position.top}px`, |
| 70 | + left: `${position.left}px`, |
| 71 | + }} |
| 72 | + > |
| 73 | + {/* Header */} |
| 74 | + <div className="flex items-start justify-between mb-3"> |
| 75 | + <div> |
| 76 | + <h3 className="text-sm font-bold text-foreground">{title}</h3> |
| 77 | + <p className="text-xs text-muted-foreground mt-1">{description}</p> |
| 78 | + </div> |
| 79 | + <button |
| 80 | + onClick={onSkip} |
| 81 | + className="ml-2 p-1 hover:bg-secondary rounded transition-colors flex-shrink-0" |
| 82 | + aria-label="Close tour" |
| 83 | + > |
| 84 | + <X size={14} className="text-muted-foreground" /> |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + |
| 88 | + {/* Step Counter */} |
| 89 | + <div className="mb-4"> |
| 90 | + <div className="flex items-center gap-2 mb-2"> |
| 91 | + <div className="text-xs font-semibold text-muted-foreground"> |
| 92 | + {step} of {totalSteps} |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + <div className="w-full h-1 bg-secondary rounded-full overflow-hidden"> |
| 96 | + <motion.div |
| 97 | + className="h-full bg-primary" |
| 98 | + initial={{ width: 0 }} |
| 99 | + animate={{ width: `${(step / totalSteps) * 100}%` }} |
| 100 | + transition={{ duration: 0.3 }} |
| 101 | + /> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + |
| 105 | + {/* Actions */} |
| 106 | + <div className="flex gap-2"> |
| 107 | + {step > 1 && ( |
| 108 | + <Button |
| 109 | + size="sm" |
| 110 | + variant="ghost" |
| 111 | + onClick={onPrevious} |
| 112 | + className="gap-1 flex-1" |
| 113 | + > |
| 114 | + <ChevronLeft size={14} /> |
| 115 | + Previous |
| 116 | + </Button> |
| 117 | + )} |
| 118 | + |
| 119 | + <Button |
| 120 | + size="sm" |
| 121 | + variant={step === totalSteps ? 'default' : 'ghost'} |
| 122 | + onClick={step === totalSteps ? onComplete : onNext} |
| 123 | + className="gap-1 flex-1" |
| 124 | + > |
| 125 | + {step === totalSteps ? 'Done' : 'Next'} |
| 126 | + {step < totalSteps && <ChevronRight size={14} />} |
| 127 | + </Button> |
| 128 | + |
| 129 | + <Button |
| 130 | + size="sm" |
| 131 | + variant="ghost" |
| 132 | + onClick={onSkip} |
| 133 | + className="flex-1" |
| 134 | + > |
| 135 | + Skip |
| 136 | + </Button> |
| 137 | + </div> |
| 138 | + </motion.div> |
| 139 | + </AnimatePresence> |
| 140 | + ); |
| 141 | +} |
0 commit comments