SPB Git

Repository Public

Share.tsx 2.0 KB 36 lines tsx

1"use client";2import { useEffect, useState } from "react";34/** Partage : Web Share API quand elle existe, copie du lien sinon (et toujours proposée). */5export function Share({ title, url, labels }: { title: string; url: string; labels: { share: string; copy: string; copied: string } }) {6  const [canShare, setCanShare] = useState(false);7  const [copied, setCopied] = useState(false);8  useEffect(() => { setCanShare(typeof navigator !== "undefined" && typeof navigator.share === "function"); }, []);9  async function copy() {10    try { await navigator.clipboard.writeText(url); }11    catch {12      const ta = document.createElement("textarea");13      ta.value = url; ta.setAttribute("readonly", ""); ta.style.position = "fixed"; ta.style.opacity = "0";14      document.body.appendChild(ta); ta.select();15      try { document.execCommand("copy"); } finally { ta.remove(); }16    }17    setCopied(true);18    window.setTimeout(() => setCopied(false), 2500);19  }20  return (21    <div className="flex flex-wrap items-center gap-2">22      {canShare && (23        <button type="button" className="btn" onClick={() => { navigator.share({ title, url }).catch(() => {}); }}>24          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M4 12v7a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1v-7M16 6l-4-4-4 4M12 2v14" /></svg>25          {labels.share}26        </button>27      )}28      <button type="button" className="btn" onClick={copy} data-testid="copy-link">29        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M10 13a5 5 0 0 0 7.07 0l3-3a5 5 0 0 0-7.07-7.07l-1.5 1.5M14 11a5 5 0 0 0-7.07 0l-3 3a5 5 0 0 0 7.07 7.07l1.5-1.5" /></svg>30        {copied ? labels.copied : labels.copy}31      </button>32      <span role="status" aria-live="polite" className="sr-only">{copied ? labels.copied : ""}</span>33    </div>34  );35}36
Files main
Menu

Theme