aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/SocialShare.astro
blob: bb6c04f401ece1829456f1d6414373fa64fc7713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
---
export interface Props {
  /** Page URL (defaults to current page) */
  url?: string;
  /** Page title */
  title?: string;
}

const { url, title } = Astro.props;
const pageUrl = url ?? Astro.url.href;
const pageTitle = title ?? "";

const encodedUrl = encodeURIComponent(pageUrl);
const encodedTitle = encodeURIComponent(pageTitle);

const SHARE_LINKS = [
  {
    label: "X",
    href: `https://x.com/intent/tweet?url=${encodedUrl}&text=${encodedTitle}`,
    svg: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 4l11.733 16h4.267l-11.733-16z"/><path d="M4 20l6.768-6.768m2.46-2.46L20 4"/></svg>',
  },
  {
    label: "Facebook",
    href: `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}`,
    svg: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 2h-3a5 5 0 0 0-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 0 1 1-1h3z"/></svg>',
  },
  {
    label: "URLコピー",
    href: "#",
    svg: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>',
  },
];
---

<div class="social-share">
  <span class="social-share-label">このページを共有:</span>
  {SHARE_LINKS.map((link) => (
    <a
      href={link.href}
      target={link.label === "URLコピー" ? undefined : "_blank"}
      rel="noopener noreferrer"
      class="social-share-btn"
      title={`${link.label}で共有`}
      data-action={link.label === "URLコピー" ? "copy" : undefined}
    >
      <span set:html={link.svg} />
      {link.label}
    </a>
  ))}
</div>

<script>
  document.currentScript?.parentElement?.addEventListener("click", (e) => {
    const btn = e.target.closest?.("[data-action='copy']");
    if (btn) {
      e.preventDefault();
      navigator.clipboard.writeText(location.href).then(() => {
        btn.title = "コピーしました!";
        btn.classList.add("copied");
        setTimeout(() => {
          btn.title = "URLコピーで共有";
          btn.classList.remove("copied");
        }, 2000);
      });
    }
  });
</script>

<style>
  @layer starlight.core {
    .social-share {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      margin-bottom: 1rem;
    }
    .social-share-label {
      font-size: 0.85rem;
      color: var(--sl-color-gray-2);
    }
    .social-share-btn {
      display: flex;
      align-items: center;
      gap: 0.3rem;
      padding: 0.3rem 0.6rem;
      border-radius: 6px;
      border: 1px solid var(--sl-color-gray-5);
      color: var(--sl-color-gray-2);
      font-size: 0.8rem;
      text-decoration: none;
      transition: color 0.2s, background 0.2s, border-color 0.2s;
    }
    .social-share-btn:hover {
      color: var(--sl-color-white);
      background: var(--sl-color-gray-5);
      border-color: var(--sl-color-gray-3);
    }
    .social-share-btn.copied {
      color: var(--sl-color-green-high, #16a34a);
      border-color: var(--sl-color-green-high, #16a34a);
    }
  }
</style>