Sha256: c8ad6a448a8158d886c606a74d77dbee0436f2e38bc9feb9b743fc20cb8a43c0

Contents?: true

Size: 1.81 KB

Versions: 14

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module Primer
  # Use TruncateComponent to shorten overflowing text with an ellipsis.
  class TruncateComponent < Primer::Component
    # @example Default
    #   <div class="col-2">
    #     <%= render(Primer::TruncateComponent.new(tag: :p)) { "branch-name-that-is-really-long" } %>
    #   </div>
    #
    # @example Inline
    #   <%= render(Primer::TruncateComponent.new(tag: :span, inline: true)) { "branch-name-that-is-really-long" } %>
    #
    # @example Expandable
    #   <%= render(Primer::TruncateComponent.new(tag: :span, inline: true, expandable: true)) { "branch-name-that-is-really-long" } %>
    #
    # @example Custom size
    #   <%= render(Primer::TruncateComponent.new(tag: :span, inline: true, expandable: true, max_width: 100)) { "branch-name-that-is-really-long" } %>
    #
    # @param inline [Boolean] Whether the element is inline (or inline-block).
    # @param expandable [Boolean] Whether the entire string should be revealed on hover. Can only be used in conjunction with `inline`.
    # @param max_width [Integer] Sets the max-width of the text.
    # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
    def initialize(inline: false, expandable: false, max_width: nil, **system_arguments)
      @system_arguments = system_arguments
      @system_arguments[:tag] ||= :div
      @system_arguments[:classes] = class_names(
        @system_arguments[:classes],
        "css-truncate",
        "css-truncate-overflow" => !inline,
        "css-truncate-target" => inline,
        "expandable" => inline && expandable
      )
      @system_arguments[:style] = join_style_arguments(@system_arguments[:style], "max-width: #{max_width}px;") unless max_width.nil?
    end

    def call
      render(Primer::BaseComponent.new(**@system_arguments)) { content }
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
primer_view_components-0.0.35 app/components/primer/truncate_component.rb
primer_view_components-0.0.34 app/components/primer/truncate_component.rb
primer_view_components-0.0.33 app/components/primer/truncate_component.rb
primer_view_components-0.0.32 app/components/primer/truncate_component.rb
primer_view_components-0.0.31 app/components/primer/truncate_component.rb
primer_view_components-0.0.30 app/components/primer/truncate_component.rb
primer_view_components-0.0.29 app/components/primer/truncate_component.rb
primer_view_components-0.0.28 app/components/primer/truncate_component.rb
primer_view_components-0.0.27 app/components/primer/truncate_component.rb
primer_view_components-0.0.26 app/components/primer/truncate_component.rb
primer_view_components-0.0.25 app/components/primer/truncate_component.rb
primer_view_components-0.0.24 app/components/primer/truncate_component.rb
primer_view_components-0.0.23 app/components/primer/truncate_component.rb
primer_view_components-0.0.22 app/components/primer/truncate_component.rb