import React, { useState, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { MessageCircle, X, PhoneCall } from 'lucide-react'; import { useToast } from "@/components/ui/use-toast"; const WhatsAppButton = ({ phoneNumber }) => { const { toast } = useToast(); const [showPopup, setShowPopup] = useState(false); const whatsappLink = `https://wa.me/${phoneNumber.replace(/\D/g, '')}?text=Hi! I'm interested in your products.`; // Close popup on outside click useEffect(() => { if (!showPopup) return; const handleClick = (e) => { if (!e.target.closest('.whatsapp-popup') && !e.target.closest('.whatsapp-btn')) { setShowPopup(false); } }; document.addEventListener('mousedown', handleClick); return () => document.removeEventListener('mousedown', handleClick); }, [showPopup]); const handleButtonClick = () => { setShowPopup((prev) => !prev); }; const handleChatClick = () => { const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent); window.open(whatsappLink, '_blank'); if (!isMobile) { navigator.clipboard.writeText(phoneNumber) .then(() => { toast({ title: "📞 Phone Number Copied!", description: `${phoneNumber} copied to clipboard. We're ready to assist you!`, duration: 4000, className: "toast-success-theme", }); }) .catch(() => { toast({ title: "⚠️ Oops!", description: "Could not copy phone number. Please try manually.", variant: "destructive", duration: 3000, className: "toast-destructive-theme", }); }); } }; return ( <> Contact us on WhatsApp {showPopup && ( Need help with your order? Get instant assistance! setShowPopup(false)} className="ml-auto text-gray-400 hover:text-gray-700" aria-label="Close" > Our experts are available to help you select the perfect spices for your recipes. Response Time: Usually within 40 minutes Chat with us now )} > ); }; export default WhatsAppButton;
Get instant assistance!
Our experts are available to help you select the perfect spices for your recipes.