1 # frozen_string_literal: true
3 module ShareButtonsHelper
6 SHARE_BUTTONS_CONFIG = {
7 :email => "share_button_icons/email.svg",
8 :bluesky => "share_button_icons/bluesky.svg",
9 :facebook => "share_button_icons/facebook.svg",
10 :linkedin => "share_button_icons/linkedin.svg",
11 :mastodon => "share_button_icons/mastodon.svg",
12 :telegram => "share_button_icons/telegram.svg",
13 :x => "share_button_icons/x.svg"
16 # Generates a set of share buttons based on the specified options.
17 def share_buttons(title:, url:)
19 :class => "d-flex gap-1 align-items-end flex-wrap mb-3"
22 tag.button(:type => "button",
23 :class => "btn btn-secondary p-1 border-1 rounded-circle",
24 :title => I18n.t("application.share.share.title"),
26 :data => { :share_type => "native",
28 :share_url => url }) do
29 image_tag("share_button_icons/share.svg", :alt => I18n.t("application.share.share.alt"), :size => 18, :class => "d-block")
33 buttons << SHARE_BUTTONS_CONFIG.map do |site, icon|
36 :class => "rounded-circle focus-ring",
37 :title => I18n.t("application.share.#{site}.title"),
39 :data => { :share_type => site == :email ? "email" : "site" }
42 link_to generate_share_url(site, title, url), link_options do
43 image_tag(icon, :alt => I18n.t("application.share.#{site}.alt"), :size => 28)
47 safe_join(buttons, "\n")
53 def generate_share_url(site, title, url)
55 title = URI.encode_uri_component(title)
56 url = URI.encode_uri_component(url)
60 "mailto:?subject=#{title}&body=#{url}"
62 "https://x.com/intent/tweet?url=#{url}&text=#{title}"
64 "https://www.linkedin.com/sharing/share-offsite/?url=#{url}"
66 "https://www.facebook.com/sharer/sharer.php?u=#{url}&t=#{title}"
68 "https://mastodonshare.com/?text=#{title}&url=#{url}"
70 "https://t.me/share/url?url=#{url}&text=#{title}"
72 "https://bsky.app/intent/compose?text=#{title}+#{url}"
74 raise ArgumentError, "Unsupported platform: #{platform}"