Class: Primer::ButtonComponent

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

Overview

Use buttons for actions (e.g. in forms). Use links for destinations, or moving from one page to another.

Constant Summary collapse

DEFAULT_SCHEME =
:default
:link
SCHEME_MAPPINGS =
{
  DEFAULT_SCHEME => "",
  :primary => "btn-primary",
  :danger => "btn-danger",
  :outline => "btn-outline",
  :invisible => "btn-invisible",
  LINK_SCHEME => "btn-link"
}.freeze
SCHEME_OPTIONS =
SCHEME_MAPPINGS.keys
DEFAULT_VARIANT =
:medium
VARIANT_MAPPINGS =
{
  :small => "btn-sm",
  DEFAULT_VARIANT => "",
  :large => "btn-large"
}.freeze
VARIANT_OPTIONS =
VARIANT_MAPPINGS.keys
DEFAULT_TAG =
:button
TAG_OPTIONS =
[DEFAULT_TAG, :a, :summary].freeze
DEFAULT_TYPE =
:button
TYPE_OPTIONS =
[DEFAULT_TYPE, :reset, :submit].freeze

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(scheme: DEFAULT_SCHEME, variant: DEFAULT_VARIANT, tag: DEFAULT_TAG, type: DEFAULT_TYPE, group_item: false, block: false, **system_arguments) ⇒ ButtonComponent

Returns a new instance of ButtonComponent.

Examples:

Schemes

<%= render(Primer::ButtonComponent.new) { "Default" } %>
<%= render(Primer::ButtonComponent.new(scheme: :primary)) { "Primary" } %>
<%= render(Primer::ButtonComponent.new(scheme: :danger)) { "Danger" } %>
<%= render(Primer::ButtonComponent.new(scheme: :outline)) { "Outline" } %>
<%= render(Primer::ButtonComponent.new(scheme: :invisible)) { "Invisible" } %>
<%= render(Primer::ButtonComponent.new(scheme: :link)) { "Link" } %>

Variants

<%= render(Primer::ButtonComponent.new(variant: :small)) { "Small" } %>
<%= render(Primer::ButtonComponent.new(variant: :medium)) { "Medium" } %>
<%= render(Primer::ButtonComponent.new(variant: :large)) { "Large" } %>

Block

<%= render(Primer::ButtonComponent.new(block: :true)) { "Block" } %>
<%= render(Primer::ButtonComponent.new(block: :true, scheme: :primary)) { "Primary block" } %>

Parameters:

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

    <%= one_of(Primer::ButtonComponent::SCHEME_OPTIONS) %>

  • variant (Symbol) (defaults to: DEFAULT_VARIANT)

    <%= one_of(Primer::ButtonComponent::VARIANT_OPTIONS) %>

  • tag (Symbol) (defaults to: DEFAULT_TAG)

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

  • type (Symbol) (defaults to: DEFAULT_TYPE)

    <%= one_of(Primer::ButtonComponent::TYPE_OPTIONS) %>

  • group_item (Boolean) (defaults to: false)

    Whether button is part of a ButtonGroup.

  • block (Boolean) (defaults to: false)

    Whether button is full-width with ‘display: block`.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/components/primer/button_component.rb', line 57

def initialize(
  scheme: DEFAULT_SCHEME,
  variant: DEFAULT_VARIANT,
  tag: DEFAULT_TAG,
  type: DEFAULT_TYPE,
  group_item: false,
  block: false,
  **system_arguments
)
  @scheme = scheme
  @system_arguments = system_arguments
  @system_arguments[:tag] = fetch_or_fallback(TAG_OPTIONS, tag, DEFAULT_TAG)

  if @system_arguments[:tag] == :button
    @system_arguments[:type] = type
  else
    @system_arguments[:role] = :button
  end

  @system_arguments[:classes] = class_names(
    system_arguments[:classes],
    SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_OPTIONS, scheme, DEFAULT_SCHEME)],
    VARIANT_MAPPINGS[fetch_or_fallback(VARIANT_OPTIONS, variant, DEFAULT_VARIANT)],
    "btn" => !link?,
    "btn-block" => block,
    "BtnGroup-item" => group_item
  )
end

Instance Method Details

#callObject



86
87
88
# File 'app/components/primer/button_component.rb', line 86

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