Sha256: 64606ef28dbf2cc45cb1395b100590bd02465a23446218c3fae8ff2de9d19099

Contents?: true

Size: 1.66 KB

Versions: 27

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Primer
  # Use `ClipboardCopy` 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 ClipboardCopy < Primer::Component
    status :beta

    # @example Default
    #   <%= render(Primer::ClipboardCopy.new(value: "Text to copy", "aria-label": "Copy text to the system clipboard")) %>
    #
    # @example With text instead of icons
    #   <%= render(Primer::ClipboardCopy.new(value: "Text to copy")) do %>
    #     Click to copy!
    #   <% end %>
    #
    # @example Copying from an element
    #   <%= render(Primer::ClipboardCopy.new(for: "blob-path", "aria-label": "Copy text to the system clipboard")) %>
    #   <div id="blob-path">src/index.js</div>
    #
    # @param aria-label [String] String that will be read to screenreaders when the component is focused
    # @param value [String] Text to copy into the users clipboard when they click the component.
    # @param for [String] Element id from where to get the copied value.
    # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
    def initialize(value: nil, **system_arguments)
      @system_arguments = deny_tag_argument(**system_arguments)
      @value = value

      validate!

      @system_arguments[:tag] = "clipboard-copy"
      @system_arguments[:value] = value if value.present?
    end

    # :nodoc:
    def before_render
      validate_aria_label if content.blank?
    end

    private

    def validate!
      raise ArgumentError, "Must provide either `value` or `for`" if @value.nil? && @system_arguments[:for].nil?
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
primer_view_components-0.0.112 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.111 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.110 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.109 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.108 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.107 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.106 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.105 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.104 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.103 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.102 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.101 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.100 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.99 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.98 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.97 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.96 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.95 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.94 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.93 app/components/primer/clipboard_copy.rb