Class: Crm::ClipboardButtonComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/crm/clipboard_button_component.rb

Overview

Unified clipboard copy button component.
Supports icon mode (copy button) and text mode (clickable text that copies).
Includes visual feedback on click via CSS class.

Link behavior:

  • If url: is provided → automatically copies as rich HTML link
  • Plain text always copies the display text (not the URL)
  • With include_url_in_text: true → plain text includes URL: "Text "

Examples:

Icon button (copies plain text)

<%= render Crm::ClipboardButtonComponent.new(text: @text) %>

Text mode with rich link (plain text = "TTK51040", rich = link)

<%= render Crm::ClipboardButtonComponent.new(text: @ref, mode: :text, url: order_url(@order)) %>

Include URL in plain text copy ("TTK51040 https://...")

<%= render Crm::ClipboardButtonComponent.new(text: @ref, mode: :text, url: order_url(@order), include_url_in_text: true) %>

Constant Summary collapse

'color:#307e6b;text-decoration-style:double'

Instance Method Summary collapse

Constructor Details

#initialize(text:, mode: :icon, url: nil, link_text: nil, copy_value: nil, include_url_in_text: false, button_class: nil, title: nil) ⇒ ClipboardButtonComponent

Returns a new instance of ClipboardButtonComponent.

Parameters:

  • text (String)

    Text to display (and copy if mode: :text and no copy_value)

  • mode (Symbol) (defaults to: :icon)

    :icon (default) or :text

  • url (String) (defaults to: nil)

    URL for rich link (if provided, automatically copies as rich link)

  • link_text (String) (defaults to: nil)

    Display text in copied link (defaults to text)

  • copy_value (String) (defaults to: nil)

    Explicit value to copy (overrides default text/link_text)

  • include_url_in_text (Boolean) (defaults to: false)

    Include URL in plain text copy (default false)

  • button_class (String) (defaults to: nil)

    CSS class for the button

  • title (String) (defaults to: nil)

    Tooltip text



33
34
35
36
37
38
39
40
41
42
# File 'app/components/crm/clipboard_button_component.rb', line 33

def initialize(text:, mode: :icon, url: nil, link_text: nil, copy_value: nil, include_url_in_text: false, button_class: nil, title: nil)
  @text = text
  @mode = mode
  @url = url
  @link_text = link_text || text
  @copy_value = copy_value
  @include_url_in_text = include_url_in_text
  @button_class = button_class || default_button_class
  @title = title || 'Copy to clipboard'
end

Instance Method Details

#callObject



44
45
46
47
48
49
50
51
52
53
# File 'app/components/crm/clipboard_button_component.rb', line 44

def call
  tag.button(
    type: 'button',
    class: combined_classes,
    data: controller_data,
    title: @title
  ) do
    content.presence || button_content
  end
end