Class: Yattho::ButtonComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/yattho/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_SIZE =
:medium
SIZE_MAPPINGS =
{
  :small => "btn-sm",
  DEFAULT_SIZE => ""
}.freeze
SIZE_OPTIONS =
SIZE_MAPPINGS.keys

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(scheme: DEFAULT_SCHEME, variant: nil, size: DEFAULT_SIZE, group_item: false, block: false, dropdown: false, **system_arguments) ⇒ ButtonComponent

Returns a new instance of ButtonComponent.

Examples:

Schemes

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

Sizes

<%= render(Yattho::ButtonComponent.new(size: :small)) { "Small" } %>
<%= render(Yattho::ButtonComponent.new(size: :medium)) { "Medium" } %>

Block

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

With leading visual

<%= render(Yattho::ButtonComponent.new) do |component| %>
  <% component.with_leading_visual_icon(icon: :star) %>
  Button
<% end %>

With trailing visual

<%= render(Yattho::ButtonComponent.new) do |component| %>
  <% component.with_trailing_visual_counter(count: 15) %>
  Button
<% end %>

With leading and trailing visuals

<%= render(Yattho::ButtonComponent.new) do |component| %>
  <% component.with_leading_visual_icon(icon: :star) %>
  <% component.with_trailing_visual_counter(count: 15) %>
  Button
<% end %>

With dropdown caret

<%= render(Yattho::ButtonComponent.new(dropdown: true)) do %>
  Button
<% end %>

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::ButtonComponent.new(id: "button-with-tooltip")) do |component| %>
    <% component.with_tooltip(text: "Tooltip text") %>
    Button
  <% end %>

Parameters:

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

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

  • variant (Symbol) (defaults to: nil)

    DEPRECATED. <%= one_of(Yattho::ButtonComponent::SIZE_OPTIONS) %>

  • size (Symbol) (defaults to: DEFAULT_SIZE)

    <%= one_of(Yattho::ButtonComponent::SIZE_OPTIONS) %>

  • tag (Symbol)

    (Yattho::Beta::BaseButton::DEFAULT_TAG) <%= one_of(Yattho::Beta::BaseButton::TAG_OPTIONS) %>

  • type (Symbol)

    (Yattho::Beta::BaseButton::DEFAULT_TYPE) <%= one_of(Yattho::Beta::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`.

  • dropdown (Boolean) (defaults to: false)

    Whether or not to render a dropdown caret.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/components/yattho/button_component.rb', line 132

def initialize(
  scheme: DEFAULT_SCHEME,
  variant: nil,
  size: DEFAULT_SIZE,
  group_item: false,
  block: false,
  dropdown: false,
  **system_arguments
)
  @scheme = scheme
  @dropdown = dropdown

  @system_arguments = system_arguments

  @id = @system_arguments[:id]

  @system_arguments[:classes] = class_names(
    system_arguments[:classes],
    SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_OPTIONS, scheme, DEFAULT_SCHEME)],
    SIZE_MAPPINGS[fetch_or_fallback(SIZE_OPTIONS, variant || size, DEFAULT_SIZE)],
    "btn" => !link?,
    "btn-block" => block,
    "BtnGroup-item" => group_item
  )
end