Class: Primer::LinkComponent

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

Overview

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

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 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) ⇒ LinkComponent

Returns a new instance of LinkComponent.

Examples:

Default

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

Muted

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

Schemes

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

Without underline

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

Span as link

<%= render(Primer::LinkComponent.new(tag: :span)) { "Span as a link" } %>

Parameters:

  • tag (String) (defaults to: DEFAULT_TAG)

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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/components/primer/link_component.rb', line 40

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


57
58
59
# File 'app/components/primer/link_component.rb', line 57

def before_render
  raise ArgumentError, "href is required when using <a> tag" if @system_arguments[:tag] == :a && @system_arguments[:href].nil? && !Rails.env.production?
end

#callObject



53
54
55
# File 'app/components/primer/link_component.rb', line 53

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