Class: Crm::ClipboardButtonComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Crm::ClipboardButtonComponent
- 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 "
Usage:
<%# 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
- RICH_LINK_STYLE =
Default styling for rich text links
'color:#307e6b;text-decoration-style:double'
Instance Method Summary collapse
-
#call ⇒ String
Renders the clipboard copy button with the Stimulus clipboard controller data attributes.
-
#initialize(text:, mode: :icon, url: nil, link_text: nil, copy_value: nil, include_url_in_text: false, no_html_strip: false, button_class: nil, title: nil) ⇒ ClipboardButtonComponent
constructor
A new instance of ClipboardButtonComponent.
Constructor Details
#initialize(text:, mode: :icon, url: nil, link_text: nil, copy_value: nil, include_url_in_text: false, no_html_strip: false, button_class: nil, title: nil) ⇒ ClipboardButtonComponent
Returns a new instance of ClipboardButtonComponent.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/components/crm/clipboard_button_component.rb', line 37 def initialize(text:, mode: :icon, url: nil, link_text: nil, copy_value: nil, include_url_in_text: false, no_html_strip: 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 @no_html_strip = no_html_strip @button_class = || @title = title || 'Copy to clipboard' end |
Instance Method Details
#call ⇒ String
Renders the clipboard copy button with the Stimulus clipboard
controller data attributes.
52 53 54 55 56 57 58 59 60 61 |
# File 'app/components/crm/clipboard_button_component.rb', line 52 def call tag.( type: 'button', class: combined_classes, data: controller_data, title: @title ) do content.presence || end end |