import React, { useState, useRef } from 'react'; import { Share2, X, Twitter, Facebook, Linkedin, Link as LinkIcon } from 'lucide-react'; import { useOnClickOutside } from '../hooks/useOnClickOutside'; interface ShareMenuProps { title: string; url: string; description: string; } const ShareMenu: React.FC = ({ title, url, description }) => { const [isOpen, setIsOpen] = useState(false); const [copyStatus, setCopyStatus] = useState<'idle' | 'copied'>('idle'); const menuRef = useRef(null); useOnClickOutside(menuRef, () => setIsOpen(false)); const shareLinks = [ { name: 'Twitter', icon: Twitter, href: `https://twitter.com/intent/tweet?text=${encodeURIComponent(title)}&url=${encodeURIComponent(url)}`, label: 'Share on Twitter' }, { name: 'Facebook', icon: Facebook, href: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`, label: 'Share on Facebook' }, { name: 'LinkedIn', icon: Linkedin, href: `https://www.linkedin.com/shareArticle?mini=true&url=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&summary=${encodeURIComponent(description)}`, label: 'Share on LinkedIn' }, ]; const copyToClipboard = async () => { try { await navigator.clipboard.writeText(url); setCopyStatus('copied'); setTimeout(() => setCopyStatus('idle'), 2000); } catch (err) { console.error('Failed to copy:', err); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Escape') { setIsOpen(false); } }; return (
{isOpen && (
{shareLinks.map((link) => ( ))}
)}
); }; export default ShareMenu