Class: Yattho::Beta::Link

Inherits:
Component
  • Object
show all
Defined in:
app/components/yattho/beta/link.rb

Overview

Use ‘Link` for navigating from one page to another. `Link` styles anchor tags with default blue styling and hover text-decoration.

Direct Known Subclasses

LinkComponent

Constant Summary collapse

DEFAULT_SCHEME =
:default
SCHEME_MAPPINGS =
{
  DEFAULT_SCHEME => "",
  :primary => "Link--primary",
  :secondary => "Link--secondary"
}.freeze
DEFAULT_TAG =
:a
TAG_OPTIONS =
[DEFAULT_TAG, :span].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

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(href: nil, tag: DEFAULT_TAG, scheme: DEFAULT_SCHEME, muted: false, underline: true, **system_arguments) ⇒ Link

Returns a new instance of Link.

Examples:

Default

<%= render(Yattho::Beta::Link.new(href: "#")) { "Link" } %>

Muted

<%= render(Yattho::Beta::Link.new(href: "#", muted: true)) { "Link" } %>

Schemes

<%= render(Yattho::Beta::Link.new(href: "#", scheme: :primary)) { "Primary" } %>
<%= render(Yattho::Beta::Link.new(href: "#", scheme: :secondary)) { "Secondary" } %>

Without underline

<%= render(Yattho::Beta::Link.new(href: "#", underline: false)) { "Link" } %>

With tooltip

@description
  Use tooltips sparingly and as a last resort. Consult the <%= link_to_component(Yattho::Alpha::Tooltip) %> documentation for more information.
@code
  <%= render(Yattho::Beta::Link.new(href: "#", id: "link-with-tooltip")) do |component| %>
    <% component.with_tooltip(text: "Tooltip text") %>
    Link
  <% end %>

Parameters:

  • tag (String) (defaults to: DEFAULT_TAG)

    <%= one_of(Yattho::Beta::Link::TAG_OPTIONS) %>

  • href (String) (defaults to: nil)

    URL to be used for the Link. Required if tag is ‘:a`. If the requirements are not met an error will be raised in non production environments. In production, an empty link element will be rendered.

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

    <%= one_of(Yattho::Beta::Link::SCHEME_MAPPINGS.keys) %>

  • muted (Boolean) (defaults to: false)

    Uses light gray for Link color, and blue on hover.

  • underline (Boolean) (defaults to: true)

    Whether or not to underline the link.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/components/yattho/beta/link.rb', line 64

def initialize(href: nil, tag: DEFAULT_TAG, scheme: DEFAULT_SCHEME, muted: false, underline: true,
               **system_arguments)
  @system_arguments = system_arguments

  @id = @system_arguments[:id]

  @system_arguments[:tag] = fetch_or_fallback(TAG_OPTIONS, tag, DEFAULT_TAG)
  @system_arguments[:href] = href
  @system_arguments[:classes] = class_names(
    @system_arguments[:classes],
    SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_MAPPINGS.keys, scheme, DEFAULT_SCHEME)],
    "Link" => tag == :span,
    "Link--muted" => muted,
    "no-underline" => !underline
  )
end

Instance Method Details

#before_renderObject

Raises:

  • (ArgumentError)


81
82
83
84
85
86
# File 'app/components/yattho/beta/link.rb', line 81

def before_render
  return unless @system_arguments[:tag] == :a && @system_arguments[:href].nil? && !Rails.env.production?

  raise ArgumentError,
        "href is required when using <a> tag"
end

#callObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/components/yattho/beta/link.rb', line 88

def call
  if tooltip.present?
    render Yattho::BaseComponent.new(tag: :span, position: :relative) do
      render(Yattho::BaseComponent.new(**@system_arguments)) do
        content
      end.to_s + tooltip.to_s
    end
  else
    render(Yattho::BaseComponent.new(**@system_arguments)) do
      content
    end
  end
end