Class: SocialShareComponent

Inherits:
ApplicationComponent show all
Defined in:
app/components/social_share_component.rb

Constant Summary collapse

LAYOUTS =
%i[auto desktop mobile hero].freeze
DEFAULT_PLATFORMS =
{
  hero:    %i[x facebook linkedin pinterest email copy],
  desktop: %i[x facebook linkedin pinterest email copy],
  mobile:  %i[native x facebook whatsapp email copy],
  auto:    %i[native x facebook whatsapp linkedin pinterest email copy]
}.freeze
PLATFORM_CONFIG =
{
  x:         { icon: 'x-twitter', family: 'fab', label: 'X' },
  facebook:  { icon: 'facebook-f', family: 'fab', label: 'Facebook' },
  linkedin:  { icon: 'linkedin-in', family: 'fab', label: 'LinkedIn' },
  pinterest: { icon: 'pinterest-p', family: 'fab', label: 'Pinterest' },
  whatsapp:  { icon: 'whatsapp', family: 'fab', label: 'WhatsApp' },
  email:     { icon: 'envelope', family: 'far', label: 'Email' },
  copy:      { icon: 'link', family: 'far', label: 'Copy link', action: :copy },
  native:    { icon: 'share-nodes', family: 'far', label: 'Share', action: :native }
}.freeze

Instance Method Summary collapse

Methods inherited from ApplicationComponent

#cms_link, #fetch_or_fallback, #image_asset_tag, #image_tag, #number_to_currency, #number_with_delimiter, #post_path, #post_url, #strip_tags

Constructor Details

#initialize(title:, url:, image_url: nil, description: nil, utm_campaign: 'social-share', layout: :auto, platforms: nil) ⇒ SocialShareComponent

Returns a new instance of SocialShareComponent.

Parameters:

  • title (String)

    The title to share

  • url (String)

    The URL to share

  • image_url (String, nil) (defaults to: nil)

    Image URL for Pinterest

  • description (String, nil) (defaults to: nil)

    Description for share text

  • utm_campaign (String) (defaults to: 'social-share')

    UTM campaign identifier

  • layout (Symbol) (defaults to: :auto)

    :auto, :desktop, :mobile, or :hero

  • platforms (Array<Symbol>, nil) (defaults to: nil)

    Override default platforms for this layout



31
32
33
34
35
36
37
38
39
40
# File 'app/components/social_share_component.rb', line 31

def initialize(title:, url:, image_url: nil, description: nil, utm_campaign: 'social-share', layout: :auto, platforms: nil)
  super()
  @title = title
  @url = url
  @image_url = image_url
  @description = description
  @utm_campaign = utm_campaign
  @layout = LAYOUTS.include?(layout) ? layout : :auto
  @platforms = platforms || DEFAULT_PLATFORMS[@layout]
end

Instance Method Details

#hero?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/components/social_share_component.rb', line 50

def hero?
  @layout == :hero
end

#platform_itemsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/components/social_share_component.rb', line 54

def platform_items
  @platforms.filter_map do |key|
    config = PLATFORM_CONFIG[key]
    next unless config

    {
      key: key,
      icon: config[:icon],
      family: config[:family],
      label: config[:label],
      action: config[:action],
      href: platform_href(key)
    }
  end
end

#show_desktop?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/components/social_share_component.rb', line 46

def show_desktop?
  @layout == :auto || @layout == :desktop
end

#show_mobile?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/components/social_share_component.rb', line 42

def show_mobile?
  @layout == :auto || @layout == :mobile
end