Class: Primer::ButtonComponent

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

Overview

Use ‘Button` 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

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, group_item: false, block: false, caret: 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" } %>

With icons

<%= render(Primer::ButtonComponent.new) do |c| %>
  <% c.icon(icon: :star) %>
  Button
<% end %>

With counter

<%= render(Primer::ButtonComponent.new) do |c| %>
  <% c.counter(count: 15) %>
  Button
<% end %>

With icons and counter

<%= render(Primer::ButtonComponent.new) do |c| %>
  <% c.icon(icon: :star) %>
  <% c.counter(count: 15) %>
  Button
<% end %>

With caret

<%= render(Primer::ButtonComponent.new(caret: true)) do %>
  Button
<% end %>

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)

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

  • type (Symbol)

    <%= one_of(Primer::BaseButton::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`.

  • caret (Boolean) (defaults to: false)

    Whether or not to render a caret.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/components/primer/button_component.rb', line 86

def initialize(
  scheme: DEFAULT_SCHEME,
  variant: DEFAULT_VARIANT,
  group_item: false,
  block: false,
  caret: false,
  **system_arguments
)
  @scheme = scheme
  @caret = caret

  @system_arguments = system_arguments
  @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