Class: SocialShareComponent

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

Overview

ViewComponent: renders the social share block.

Constant Summary collapse

LAYOUTS =

Layouts.

%i[auto desktop mobile hero].freeze
DEFAULT_PLATFORMS =

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 =

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



35
36
37
38
39
40
41
42
43
44
# File 'app/components/social_share_component.rb', line 35

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

Whether the hero layout is in use.

Returns:

  • (Boolean)

    true when the layout is :hero



60
61
62
# File 'app/components/social_share_component.rb', line 60

def hero?
  @layout == :hero
end

#platform_itemsArray<Hash>

Builds the list of share buttons to render, in platform order.

Returns:

  • (Array<Hash>)

    one entry per platform with :key, :icon, :family,
    :label, :action, and :href keys; unknown platforms are skipped



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/components/social_share_component.rb', line 67

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

Whether the desktop share bar should be rendered for this layout.

Returns:

  • (Boolean)

    true for :auto and :desktop layouts



54
55
56
# File 'app/components/social_share_component.rb', line 54

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

#show_mobile?Boolean

Whether the mobile share bar should be rendered for this layout.

Returns:

  • (Boolean)

    true for :auto and :mobile layouts



48
49
50
# File 'app/components/social_share_component.rb', line 48

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