Class: Yattho::Truncate

Inherits:
Component
  • Object
show all
Defined in:
app/components/yattho/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

Yattho::TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

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

Constructor Details

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

Returns a new instance of Truncate.

Examples:

Default

<div class="col-2">
  <%= render(Yattho::Truncate.new(tag: :p)) { "branch-name-that-is-really-long" } %>
</div>

Inline

<%= render(Yattho::Truncate.new(tag: :span, inline: true)) { "branch-name-that-is-really-long" } %>

Expandable

<%= render(Yattho::Truncate.new(tag: :span, inline: true, expandable: true)) { "branch-name-that-is-really-long" } %>

Custom size

<%= render(Yattho::Truncate.new(tag: :span, inline: true, expandable: true, max_width: 100)) { "branch-name-that-is-really-long" } %>

With HTML content

<%= render(Yattho::Truncate.new(tag: :span, inline: true, expandable: true, max_width: 100)) do %>
  <span>branch-name-that-is-really-long</span>
<% end %>

Parameters:

  • tag (Symbol) (defaults to: DEFAULT_TAG)

    <%= one_of(Yattho::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 %>



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/components/yattho/truncate.rb', line 35

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
  )
  return if max_width.nil?

  @system_arguments[:style] =
    join_style_arguments(@system_arguments[:style], "max-width: #{max_width}px;")
end

Instance Method Details

#callObject



51
52
53
# File 'app/components/yattho/truncate.rb', line 51

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