# frozen_string_literal: true
module Ariadne
# Use `ClipboardCopyComponent` to copy element text content or input values to the clipboard.
#
# @accessibility
# Always set an accessible label to help the user interact with the component.
class ClipboardCopyComponent < Ariadne::Component
DEFAULT_TAG = :"clipboard-copy"
DATA_CONTROLLER = "clipboard_copy_component"
DATA_ACTION = "click->clipboard_copy_component#copy"
# @example Default
# <%= render(Ariadne::ClipboardCopyComponent.new(value: "Text to copy", aria_label: "Copy text to the system clipboard" )) %>
#
# @example With text instead of icons
# <%= render(Ariadne::ClipboardCopyComponent.new(value: "Text to copy", aria_label: "Copy text to the system clipboard" )) do %>
# Click to copy!
# <% end %>
#
# @example Copying from an element
# <%= render(Ariadne::ClipboardCopyComponent.new(attributes: { for: "blob-path"}, aria_label: "Copy text to the system clipboard" )) %>
#
src/index.js
#
# @param tag [Symbol, String] The rendered tag name
# @param classes [String] <%= link_to_classes_docs %>
# @param value [String] Text to copy into the users clipboard when they click the component.
# @param aria_label [String] Text for accessibility. Can also be passed in as part of `attributes`, but it must be present.
# @param attributes [Hash] <%= link_to_attributes_docs %>
def initialize(tag: DEFAULT_TAG, value: "", aria_label: "", classes: "", attributes: {})
@attributes = attributes
@value = value
@attributes[:"aria-label"] = aria_label
validate!
@classes = classes
@tag = check_incoming_tag(DEFAULT_TAG, tag)
@attributes[:value] = value if value.present?
end
private def validate!
validate_aria_label!
raise ArgumentError, "Must provide either `value` or `for`" if @value.blank? && @attributes[:for].blank?
raise ArgumentError, "Must provide only `value` or `for`, not both" if @value.present? && @attributes[:for].present?
end
end
end