Class: Primer::Truncate

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/truncate.rb

Overview

Use ‘Truncate` to shorten overflowing text with an ellipsis.

Constant Summary collapse

DEFAULT_TAG =
:div
TAG_OPTIONS =
[DEFAULT_TAG, :span, :p, :strong].freeze

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

Primer::TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Constants included from AttributesHelper

AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Methods included from AttributesHelper

#aria, #data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes

Constructor Details

#initialize(tag: DEFAULT_TAG, inline: false, expandable: false, max_width: nil, **system_arguments) ⇒ Truncate

Returns a new instance of Truncate.

Parameters:

  • tag (Symbol) (defaults to: DEFAULT_TAG)

    <%= one_of(Primer::Truncate::TAG_OPTIONS) %>

  • inline (Boolean) (defaults to: false)

    Whether the element is inline (or inline-block).

  • expandable (Boolean) (defaults to: false)

    Whether the entire string should be revealed on hover. Can only be used in conjunction with ‘inline`.

  • max_width (Integer) (defaults to: nil)

    Sets the max-width of the text.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/components/primer/truncate.rb', line 16

def initialize(tag: DEFAULT_TAG, inline: false, expandable: false, max_width: nil, **system_arguments)
  @system_arguments = system_arguments
  @system_arguments[:tag] = fetch_or_fallback(TAG_OPTIONS, tag, DEFAULT_TAG)
  @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

Instance Method Details

#callObject



29
30
31
# File 'app/components/primer/truncate.rb', line 29

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