Class: Yattho::Alpha::Banner

Inherits:
Component
  • Object
show all
Defined in:
app/components/yattho/alpha/banner.rb

Overview

Use ‘Banner` to highlight important information.

Constant Summary collapse

DEFAULT_SCHEME =
:default
SCHEME_MAPPINGS =
{
  DEFAULT_SCHEME => "",
  :warning => "Banner--warning",
  :danger => "Banner--error",
  :success => "Banner--success"
}.freeze
LEGACY_SCHEME_MAPPINGS =
{
  DEFAULT_SCHEME => "",
  :warning => "flash-warn",
  :danger => "flash-error",
  :success => "flash-success"
}.freeze
DEFAULT_ICONS =
{
  default: :bell,
  warning: :alert,
  danger: :stop,
  success: :'check-circle'
}.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(full: false, full_when_narrow: false, dismissible: false, description: nil, icon: nil, scheme: DEFAULT_SCHEME, reappear: false, **system_arguments) ⇒ Banner

Returns a new instance of Banner.

Examples:

Schemes

<div style="display: grid; row-gap: 15px">
  <%= render(Yattho::Alpha::Banner.new) { "This is a banner message!" } %>
  <%= render(Yattho::Alpha::Banner.new(scheme: :warning)) { "This is a warning banner!" } %>
  <%= render(Yattho::Alpha::Banner.new(scheme: :danger)) { "This is a danger banner!" } %>
  <%= render(Yattho::Alpha::Banner.new(scheme: :success)) { "This is a success banner!" } %>
</div>

Full width

<%= render(Yattho::Alpha::Banner.new(full: true)) { "This is a full width banner!" } %>

Dismissible

<%= render(Yattho::Alpha::Banner.new(dismissible: true, reappear: true)) { "This is a dismissible banner!" } %>

Custom icon

<%= render(Yattho::Alpha::Banner.new(icon: :people)) { "This is a banner with a custom icon!" } %>

With action button

<%= render(Yattho::Alpha::Banner.new) do |component| %>
  This is a banner with an action button!
  <% component.with_action_button(size: :small) { "Take action" } %>
<% end %>

With custom action

<%= render(Yattho::Alpha::Banner.new) do |component| %>
  Comment saved!
  <% component.with_action_content do %>
    <%= render(Yattho::IconButton.new(icon: :pencil, mr: 1, "aria-label": "Edit")) %>
  <% end %>
<% end %>

Parameters:

  • full (Boolean) (defaults to: false)

    Whether the component should take up the full width of the screen.

  • full_when_narrow (Boolean) (defaults to: false)

    Whether the component should take up the full width of the screen when rendered inside smaller viewports.

  • dismissible (Boolean) (defaults to: false)

    Whether the component can be dismissed with an “x” button.

  • description (String) (defaults to: nil)

    Description text rendered underneath the message.

  • icon (Symbol) (defaults to: nil)

    The name of an <%= link_to_octicons %> icon to use. If no icon is provided, a default one will be chosen based on the scheme.

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

    <%= one_of(Yattho::Alpha::Banner::SCHEME_MAPPINGS.keys) %>

  • reappear (Boolean) (defaults to: false)

    Whether or not the flash banner should reappear after being dismissed. Only for use in test and preview environments.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/components/yattho/alpha/banner.rb', line 90

def initialize(full: false, full_when_narrow: false, dismissible: false, description: nil, icon: nil,
               scheme: DEFAULT_SCHEME, reappear: false, **system_arguments)
  @scheme = fetch_or_fallback(SCHEME_MAPPINGS.keys, scheme, DEFAULT_SCHEME)
  @icon = icon || DEFAULT_ICONS[@scheme]
  @dismissible = dismissible
  @description = description
  @reappear = reappear

  @system_arguments = deny_tag_argument(**system_arguments)
  @system_arguments[:tag] = :div
  @system_arguments[:classes] = class_names(
    @system_arguments[:classes],
    "Banner",
    "flash", # legacy
    SCHEME_MAPPINGS[@scheme],
    LEGACY_SCHEME_MAPPINGS[@scheme],
    'Banner--full': full,
    'flash-full': full, # legacy
    'Banner--full-whenNarrow': full_when_narrow
  )

  @message_arguments = {
    tag: :div,
    classes: "Banner-message"
  }

  @wrapper_arguments = {
    tag: custom_element_name
  }

  @wrapper_arguments[:data] = { reappear: @reappear } if @reappear
end