# typed: false
# frozen_string_literal: true
module Ariadne
module UI
module ClipboardCopy
# Use `ClipboardCopy` to copy element text content or input values to the clipboard.
#
# @example Default
# <%= render(Ariadne::UI::ClipboardCopy::Component.new(value: "Text to copy", aria_label: "Copy text to the system clipboard" )) %>
#
# @example With text instead of icons
# <%= render(Ariadne::UI::ClipboardCopy::Component.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::UI::ClipboardCopy::Component.new(for_id: "blob-path", aria_label: "Copy text to the system clipboard" )) %>
#
src/index.js
#
# @accessibility
# Always set an accessible label to help the user interact with the component.
class Component < Ariadne::UI::Button::Component
# @param value [Strinulus_g] Text to copy when the component is clicked.
option :value, optional: true
# @param for [String] If `value` is not provided, the element with this id will be copied.
option :for, optional: true
def initialize(**options)
super
validate!
end
accepts_html_attributes do |html_attrs|
prepend_controller(html_attrs)
prepend_action(html_attrs, "clipboard-copy->ariadne-ui-clipboard-copy#copy")
html_attrs[:value] = @value if @value.present?
html_attrs[:for] = @for if @for.present?
html_attrs[:class] = Ariadne::ViewComponents.tailwind_merger.merge([style(size:), html_attrs[:class]].join(" "))
html_attrs
end
def before_render
validate_aria_label!(html_attrs) if content.blank?
end
private def validate!
raise ArgumentError, "Must provide either `value` or `for`" if @value.blank? && @for.blank?
raise ArgumentError, "Must provide only `value` or `for`, not both" if @value.present? && @for.present?
end
def classes
style(:button, size:) + style(:size)
end
style do
base do
[
"ariadne-inline-flex",
"ariadne-items-center",
"ariadne-justify-center",
]
end
variants do
size do
xs do
[
"ariadne-gap-0.5",
"ariadne-text-xs-md",
"ariadne-rounded",
"[&>svg]:ariadne-size-3",
]
end
sm do
[
"ariadne-gap-0.5",
"ariadne-px-1",
"ariadne-text-sm-md",
"ariadne-rounded",
"[&>svg]:ariadne-size-4",
]
end
base do
["ariadne-gap-1", "ariadne-text-base", "ariadne-rounded-md"]
end
md do
[
"ariadne-gap-1",
"ariadne-text-base",
"ariadne-rounded-lg",
"[&>svg]:ariadne-size-5",
]
end
lg do
["ariadne-gap-1", "ariadne-text-lg", "ariadne-rounded-lg"]
end
end
end
end
end
end
end
end