Sha256: 3a9f8df09dc2ed2f82eae54dcb4423755f863a7bc6cd7e5d0d009d26c9d1e491

Contents?: true

Size: 1.64 KB

Versions: 24

Compression:

Stored size: 1.64 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", "aria-label": "Copy text to the system clipboard")) 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

    private

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

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
primer_view_components-0.0.85 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.84 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.83 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.82 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.81 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.80 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.79 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.78 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.77 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.76 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.75 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.74 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.73 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.72 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.71 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.70 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.69 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.68 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.67 app/components/primer/clipboard_copy.rb
primer_view_components-0.0.66 app/components/primer/clipboard_copy.rb