tahamajs/IE / Projects /front /src /components /common /ShareButtons.tsx
tahamajs's picture
download
raw
4.6 kB
import { useState } from "react";
import { Link2, Check, Share2, Twitter, Linkedin, Facebook } from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";
interface ShareButtonsProps {
url: string;
title: string;
variant?: "horizontal" | "vertical";
className?: string;
}
export function ShareButtons({ url, title, variant = "horizontal", className = "" }: ShareButtonsProps) {
const [copied, setCopied] = useState(false);
const [showShareOptions, setShowShareOptions] = useState(variant === "horizontal");
const copyToClipboard = async () => {
try {
await navigator.clipboard.writeText(url);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch (err) {
console.error("Failed to copy!", err);
}
};
const shareNative = async () => {
if (navigator.share) {
try {
await navigator.share({ title, url });
} catch (err) {
console.error("Native share failed", err);
}
} else {
setShowShareOptions(!showShareOptions);
}
};
const encodedUrl = encodeURIComponent(url);
const encodedTitle = encodeURIComponent(title);
const links = [
{
name: "Twitter",
icon: <Twitter size={18} />,
href: `https://twitter.com/intent/tweet?url=${encodedUrl}&text=${encodedTitle}`,
color: "hover:bg-blue-500 hover:text-white dark:hover:bg-blue-500 text-[#1DA1F2]",
},
{
name: "LinkedIn",
icon: <Linkedin size={18} />,
href: `https://www.linkedin.com/sharing/share-offsite/?url=${encodedUrl}`,
color: "hover:bg-blue-700 hover:text-white dark:hover:bg-blue-700 text-[#0077B5]",
},
{
name: "Facebook",
icon: <Facebook size={18} />,
href: `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}`,
color: "hover:bg-blue-600 hover:text-white dark:hover:bg-blue-600 text-[#1877F2]",
},
];
const containerClass = variant === "horizontal"
? "flex flex-row items-center gap-2"
: "flex flex-col items-center gap-3";
return (
<div className={`relative ${containerClass} ${className}`}>
{variant === "vertical" && (
<motion.button
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.95 }}
onClick={shareNative}
className="p-3 rounded-full bg-white/50 dark:bg-gray-800/50 backdrop-blur-md border border-gray-200/50 dark:border-gray-700/50 text-gray-700 dark:text-gray-300 shadow-sm hover:shadow-md transition-all z-10"
title="Share"
>
<Share2 size={20} />
</motion.button>
)}
<AnimatePresence>
{showShareOptions && (
<motion.div
initial={{ opacity: 0, scale: 0.9, y: variant === "vertical" ? -10 : 0, x: variant === "horizontal" ? -10 : 0 }}
animate={{ opacity: 1, scale: 1, y: 0, x: 0 }}
exit={{ opacity: 0, scale: 0.9, y: variant === "vertical" ? -10 : 0, x: variant === "horizontal" ? -10 : 0 }}
className={`${containerClass} ${variant === "vertical" ? "absolute top-14 bg-white/80 dark:bg-gray-800/80 p-2 rounded-2xl backdrop-blur-xl border border-gray-200/50 dark:border-gray-700/50 shadow-xl" : ""}`}
>
{links.map((link) => (
<motion.a
key={link.name}
href={link.href}
target="_blank"
rel="noopener noreferrer"
whileHover={{ scale: 1.15 }}
whileTap={{ scale: 0.9 }}
className={`p-2.5 rounded-xl bg-white/60 dark:bg-gray-800/60 backdrop-blur-md border border-gray-200/50 dark:border-gray-700/50 shadow-sm transition-all duration-300 ${link.color}`}
title={`Share on ${link.name}`}
>
{link.icon}
</motion.a>
))}
<motion.button
whileHover={{ scale: 1.15 }}
whileTap={{ scale: 0.9 }}
onClick={copyToClipboard}
className={`p-2.5 rounded-xl backdrop-blur-md border border-gray-200/50 dark:border-gray-700/50 shadow-sm transition-all duration-300 ${
copied
? "bg-teal-500 text-white border-teal-500"
: "bg-white/60 dark:bg-gray-800/60 text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700"
}`}
title="Copy Link"
>
{copied ? <Check size={18} /> : <Link2 size={18} />}
</motion.button>
</motion.div>
)}
</AnimatePresence>
</div>
);
}

Xet Storage Details

Size:
4.6 kB
·
Xet hash:
d7701ca5d499725a89a9c2efe7923462dd796922b3ce28815986415b91649653

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.